Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…dation into main
  • Loading branch information
ishall1 committed Jun 15, 2021
2 parents 00a5b30 + 78f2ce5 commit af017e8
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 15 deletions.
4 changes: 0 additions & 4 deletions CoreFoundation/Collections.subproj/CFTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ void CFTreePrependChild(CFTreeRef tree, CFTreeRef newChild) {
}

void CFTreeAppendChild(CFTreeRef tree, CFTreeRef newChild) {
CFAllocatorRef allocator;
__CFGenericValidateType(tree, CFTreeGetTypeID());
__CFGenericValidateType(newChild, CFTreeGetTypeID());
CFAssert1(NULL == newChild->_parent, __kCFLogAssertion, "%s(): must remove newChild from previous parent first", __PRETTY_FUNCTION__);
Expand All @@ -316,7 +315,6 @@ void CFTreeAppendChild(CFTreeRef tree, CFTreeRef newChild) {
HALT;
}
CFRetain(newChild);
allocator = CFGetAllocator(tree);
*((void **)&newChild->_parent) = tree;
newChild->_sibling = NULL;
if (!tree->_child) {
Expand All @@ -328,14 +326,12 @@ void CFTreeAppendChild(CFTreeRef tree, CFTreeRef newChild) {
}

void CFTreeInsertSibling(CFTreeRef tree, CFTreeRef newSibling) {
CFAllocatorRef allocator;
__CFGenericValidateType(tree, CFTreeGetTypeID());
__CFGenericValidateType(newSibling, CFTreeGetTypeID());
CFAssert1(NULL != tree->_parent, __kCFLogAssertion, "%s(): tree must have a parent", __PRETTY_FUNCTION__);
CFAssert1(NULL == newSibling->_parent, __kCFLogAssertion, "%s(): must remove newSibling from previous parent first", __PRETTY_FUNCTION__);
CFAssert1(NULL == newSibling->_sibling, __kCFLogAssertion, "%s(): must remove newSibling from previous parent first", __PRETTY_FUNCTION__);
CFRetain(newSibling);
allocator = CFGetAllocator(tree);
*((void **)&newSibling->_parent) = tree->_parent;
*((void **)&newSibling->_sibling) = tree->_sibling;
*((void **)&tree->_sibling) = newSibling;
Expand Down
4 changes: 2 additions & 2 deletions CoreFoundation/URL.subproj/CFURL.c
Original file line number Diff line number Diff line change
Expand Up @@ -4786,7 +4786,7 @@ CFURLRef CFURLCreateCopyAppendingPathComponent(CFAllocatorRef allocator, CFURLRe
#endif

CFMutableStringRef newString;
CFStringRef newComp;
CFStringRef newComp = NULL;
CFRange pathRg;
if (!(url->_flags & HAS_PATH)) {
result = NULL;
Expand Down Expand Up @@ -4815,9 +4815,9 @@ CFURLRef CFURLCreateCopyAppendingPathComponent(CFAllocatorRef allocator, CFURLRe
if (isDirectory) {
CFStringInsert(newString, pathRg.location + pathRg.length + CFStringGetLength(newComp), CFSTR("/"));
}
CFRelease(newComp);
result = _CFURLCreateWithArbitraryString(allocator, newString, url->_base);
}
if (newComp) CFRelease(newComp);
CFRelease(newString);
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Foundation/DataProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ extension DataProtocol {
}

public func firstRange<D: DataProtocol, R: RangeExpression>(of data: D, in range: R) -> Range<Index>? where R.Bound == Index {
guard !data.isEmpty else {
return nil
}
let r = range.relative(to: self)
let rangeCount = distance(from: r.lowerBound, to: r.upperBound)
if rangeCount < data.count {
Expand Down Expand Up @@ -166,6 +169,9 @@ extension DataProtocol {
}

public func lastRange<D: DataProtocol, R: RangeExpression>(of data: D, in range: R) -> Range<Index>? where R.Bound == Index {
guard !data.isEmpty else {
return nil
}
let r = range.relative(to: self)
let rangeCount = distance(from: r.lowerBound, to: r.upperBound)
if rangeCount < data.count {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,14 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
// anyway.
buffer.baseAddress!.advanced(by: outputIndex).copyMemory(from: &outputBytes, byteCount: 4)
outputIndex += 4
bytesLeft = dataBuffer.count - inputIndex

if lineLength != 0 {
// Add required EOL markers.
currentLineCount += 4
assert(currentLineCount <= lineLength)

if currentLineCount == lineLength {
if currentLineCount == lineLength && bytesLeft > 0 {
buffer[outputIndex] = separatorByte1
outputIndex += 1

Expand All @@ -916,7 +918,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
currentLineCount = 0
}
}
bytesLeft = dataBuffer.count - inputIndex
}

// Return the number of ASCII bytes written to the buffer
Expand Down
6 changes: 3 additions & 3 deletions Sources/Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,9 @@ open class Process: NSObject {
process._terminationReason = .exit
}

// Set the running flag to false
process.isRunning = false

// If a termination handler has been set, invoke it on a background thread

if let terminationHandler = process.terminationHandler {
Expand All @@ -839,9 +842,6 @@ open class Process: NSObject {
thread.start()
}

// Set the running flag to false
process.isRunning = false

// Invalidate the source and wake up the run loop, if it's available

CFRunLoopSourceInvalidate(process.runLoopSource)
Expand Down
13 changes: 9 additions & 4 deletions Sources/Foundation/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,26 @@ open class ProcessInfo: NSObject {
}

// These are internal access for testing
static func countCoreIds(cores: Substring) -> Int {
static func countCoreIds(cores: Substring) -> Int? {
let ids = cores.split(separator: "-", maxSplits: 1)
guard let first = ids.first.flatMap({ Int($0, radix: 10) }),
let last = ids.last.flatMap({ Int($0, radix: 10) }),
last >= first
else { preconditionFailure("cpuset format is incorrect") }
else {
return nil
}
return 1 + last - first
}

static func coreCount(cpuset cpusetPath: String) -> Int? {
guard let cpuset = try? firstLineOfFile(path: cpusetPath).split(separator: ","),
!cpuset.isEmpty
else { return nil }

return cpuset.map(countCoreIds).reduce(0, +)
if let first = cpuset.first, let count = countCoreIds(cores: first) {
return count
} else {
return nil
}
}

static func coreCount(quota quotaPath: String, period periodPath: String) -> Int? {
Expand Down
25 changes: 25 additions & 0 deletions Sources/Tools/plutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@ add_executable(plutil
main.swift)
target_link_libraries(plutil PRIVATE
Foundation)

# On ELF platforms, remove the absolute rpath to the host toolchain's stdlib,
# then add it back temporarily as a BUILD_RPATH just for the tests.
if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
target_link_options(plutil PRIVATE "SHELL:-no-toolchain-stdlib-rpath")

string(REPLACE " " ";" ARGS_LIST ${CMAKE_Swift_FLAGS})
execute_process(
COMMAND ${CMAKE_Swift_COMPILER} ${ARGS_LIST} -print-target-info
OUTPUT_VARIABLE output
ERROR_VARIABLE error_output
RESULT_VARIABLE result
)
if(NOT ${result} EQUAL 0)
message(FATAL_ERROR "Error getting target info with\n"
" `${CMAKE_Swift_COMPILER} ${CMAKE_Swift_FLAGS} -print-target-info`\n"
"Error:\n"
" ${error_output}")
endif()

string(REGEX MATCH "\"runtimeLibraryPaths\": \\[\n\ +\"([^\"]+)\""
path ${output})
set_target_properties(plutil PROPERTIES BUILD_RPATH ${CMAKE_MATCH_1})
endif()

set_target_properties(plutil PROPERTIES
INSTALL_RPATH "$ORIGIN/../lib/swift/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")

Expand Down
44 changes: 44 additions & 0 deletions Tests/Foundation/Tests/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class TestNSData: LoopbackServerTest {
("testCopyBytes", testCopyBytes),
("testCustomDeallocator", testCustomDeallocator),
("testDataInSet", testDataInSet),
("testFirstRangeEmptyData", testFirstRangeEmptyData),
("testLastRangeEmptyData", testLastRangeEmptyData),
("testEquality", testEquality),
("testGenericAlgorithms", testGenericAlgorithms),
("testInitializationWithArray", testInitializationWithArray),
Expand Down Expand Up @@ -244,6 +246,8 @@ class TestNSData: LoopbackServerTest {
("test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn", test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn),
("test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed", test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed),
("test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth", test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth),
("test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine", test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine),
("test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine", test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine),
("test_base64EncodedStringGetsEncodedText", test_base64EncodedStringGetsEncodedText),
("test_initializeWithBase64EncodedStringGetsDecodedData", test_initializeWithBase64EncodedStringGetsDecodedData),
("test_base64DecodeWithPadding1", test_base64DecodeWithPadding1),
Expand Down Expand Up @@ -814,6 +818,36 @@ class TestNSData: LoopbackServerTest {
XCTAssertEqual(encodedTextResult, encodedText)
}

func test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine() {

XCTAssertEqual(
Data(repeating: 0, count: 48).base64EncodedString(options: .lineLength64Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"each 3 byte is converted into 4 characterss. 48 / 3 * 4 <= 64, therefore result should not have line separator."
)

XCTAssertEqual(
Data(repeating: 0, count: 57).base64EncodedString(options: .lineLength76Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"each 3 byte is converted into 4 characterss. 57 / 3 * 4 <= 76, therefore result should not have line separator."
)
}

func test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine() {

XCTAssertEqual(
Data(repeating: 0, count: 49).base64EncodedString(options: .lineLength64Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
"each 3 byte is converted into 4 characterss. 49 / 3 * 4 > 64, therefore result should have lines with separator."
)

XCTAssertEqual(
Data(repeating: 0, count: 58).base64EncodedString(options: .lineLength76Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
"each 3 byte is converted into 4 characterss. 58 / 3 * 4 > 76, therefore result should have lines with separator."
)
}

func test_base64EncodedStringGetsEncodedText() {
let plainText = "Revocate animos, maestumque timorem mittite: forsan et haec olim meminisse iuvabit."
let encodedText = "UmV2b2NhdGUgYW5pbW9zLCBtYWVzdHVtcXVlIHRpbW9yZW0gbWl0dGl0ZTogZm9yc2FuIGV0IGhhZWMgb2xpbSBtZW1pbmlzc2UgaXV2YWJpdC4="
Expand Down Expand Up @@ -1196,6 +1230,16 @@ extension TestNSData {
XCTAssertEqual(s.count, 2, "Expected only two entries in the Set")
}

func testFirstRangeEmptyData() {
let d = Data([1, 2, 3])
XCTAssertNil(d.firstRange(of: Data()))
}

func testLastRangeEmptyData() {
let d = Data([1, 2, 3])
XCTAssertNil(d.lastRange(of: Data()))
}

func testReplaceSubrange() {
var hello = dataFrom("Hello")
let world = dataFrom("World")
Expand Down

0 comments on commit af017e8

Please sign in to comment.