diff --git a/Sources/System/FilePath/FilePathComponents.swift b/Sources/System/FilePath/FilePathComponents.swift index 6b8a940..b304a0a 100644 --- a/Sources/System/FilePath/FilePathComponents.swift +++ b/Sources/System/FilePath/FilePathComponents.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -146,7 +146,7 @@ extension _StrSlice { internal func _withSystemChars( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try _storage.withSystemChars { + try _storage.withNullTerminatedSystemChars { try f(UnsafeBufferPointer(rebasing: $0[_range])) } } diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index 01de8e8..57abc62 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -197,24 +197,20 @@ extension SystemString: Hashable, Codable { extension SystemString { - // withSystemChars includes the null terminator - internal func withSystemChars( + internal func withNullTerminatedSystemChars( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try nullTerminatedStorage.withContiguousStorageIfAvailable(f)! + try nullTerminatedStorage.withUnsafeBufferPointer(f) } + // withCodeUnits does not include the null terminator internal func withCodeUnits( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try withSystemChars { chars in - let length = chars.count * MemoryLayout.stride - let count = length / MemoryLayout.stride - return try chars.baseAddress!.withMemoryRebound( - to: CInterop.PlatformUnicodeEncoding.CodeUnit.self, - capacity: count - ) { pointer in - try f(UnsafeBufferPointer(start: pointer, count: count)) + try withNullTerminatedSystemChars { + try $0.withMemoryRebound(to: CInterop.PlatformUnicodeEncoding.CodeUnit.self) { + assert($0.last == .zero) + return try f(.init(start: $0.baseAddress, count: $0.count&-1)) } } } @@ -230,7 +226,9 @@ extension Slice where Base == SystemString { } internal var string: String { - withCodeUnits { String(decoding: $0, as: CInterop.PlatformUnicodeEncoding.self) } + withCodeUnits { + String(decoding: $0, as: CInterop.PlatformUnicodeEncoding.self) + } } internal func withPlatformString( @@ -272,7 +270,11 @@ extension SystemString: ExpressibleByStringLiteral { } extension SystemString: CustomStringConvertible, CustomDebugStringConvertible { - internal var string: String { String(decoding: self) } + internal var string: String { + self.withCodeUnits { + String(decoding: $0, as: CInterop.PlatformUnicodeEncoding.self) + } + } internal var description: String { string } internal var debugDescription: String { description.debugDescription } @@ -311,7 +313,7 @@ extension SystemString { internal func withPlatformString( _ f: (UnsafePointer) throws -> T ) rethrows -> T { - try withSystemChars { chars in + try withNullTerminatedSystemChars { chars in let length = chars.count * MemoryLayout.stride return try chars.baseAddress!.withMemoryRebound( to: CInterop.PlatformChar.self, diff --git a/Tests/SystemTests/SystemStringTests.swift b/Tests/SystemTests/SystemStringTests.swift index a189171..48ca83c 100644 --- a/Tests/SystemTests/SystemStringTests.swift +++ b/Tests/SystemTests/SystemStringTests.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -101,7 +101,7 @@ struct StringTest: TestCase { // Test null insertion let rawChars = raw.lazy.map { SystemChar($0) } expectEqual(sysRaw, SystemString(rawChars.dropLast())) - sysRaw.withSystemChars { // TODO: assuming we want null in withSysChars + sysRaw.withNullTerminatedSystemChars { expectEqualSequence(rawChars, $0, "rawChars") } @@ -254,6 +254,11 @@ final class SystemStringTest: XCTestCase { XCTAssert(str == "abcd") } + func testStringProperty() { + let source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63] + let str = SystemString(platformString: source) + XCTAssertEqual(str.string, str[...].string) + } } extension SystemStringTest {