1
1
/*
2
2
This source file is part of the Swift System open source project
3
3
4
- Copyright (c) 2020 Apple Inc. and the Swift System project authors
4
+ Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors
5
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
6
7
7
See https://swift.org/LICENSE.txt for license information
@@ -169,24 +169,20 @@ extension SystemString: Hashable, Codable {}
169
169
170
170
extension SystemString {
171
171
172
- // withSystemChars includes the null terminator
173
- internal func withSystemChars< T> (
172
+ internal func withNullTerminatedSystemChars< T> (
174
173
_ f: ( UnsafeBufferPointer < SystemChar > ) throws -> T
175
174
) rethrows -> T {
176
- try nullTerminatedStorage. withContiguousStorageIfAvailable ( f) !
175
+ try nullTerminatedStorage. withUnsafeBufferPointer ( f)
177
176
}
178
177
178
+ // withCodeUnits does not include the null terminator
179
179
internal func withCodeUnits< T> (
180
180
_ f: ( UnsafeBufferPointer < CInterop . PlatformUnicodeEncoding . CodeUnit > ) throws -> T
181
181
) rethrows -> T {
182
- try withSystemChars { chars in
183
- let length = chars. count * MemoryLayout< SystemChar> . stride
184
- let count = length / MemoryLayout< CInterop . PlatformUnicodeEncoding. CodeUnit> . stride
185
- return try chars. baseAddress!. withMemoryRebound (
186
- to: CInterop . PlatformUnicodeEncoding. CodeUnit. self,
187
- capacity: count
188
- ) { pointer in
189
- try f ( UnsafeBufferPointer ( start: pointer, count: count) )
182
+ try withNullTerminatedSystemChars {
183
+ try $0. withMemoryRebound ( to: CInterop . PlatformUnicodeEncoding. CodeUnit. self) {
184
+ assert ( $0. last == . zero)
185
+ return try f ( . init( start: $0. baseAddress, count: $0. count&- 1 ) )
190
186
}
191
187
}
192
188
}
@@ -202,7 +198,9 @@ extension Slice where Base == SystemString {
202
198
}
203
199
204
200
internal var string : String {
205
- withCodeUnits { String ( decoding: $0, as: CInterop . PlatformUnicodeEncoding. self) }
201
+ withCodeUnits {
202
+ String ( decoding: $0, as: CInterop . PlatformUnicodeEncoding. self)
203
+ }
206
204
}
207
205
208
206
internal func withPlatformString< T> (
@@ -244,7 +242,11 @@ extension SystemString: ExpressibleByStringLiteral {
244
242
}
245
243
246
244
extension SystemString : CustomStringConvertible , CustomDebugStringConvertible {
247
- internal var string : String { String ( decoding: self ) }
245
+ internal var string : String {
246
+ self . withCodeUnits {
247
+ String ( decoding: $0, as: CInterop . PlatformUnicodeEncoding. self)
248
+ }
249
+ }
248
250
249
251
internal var description : String { string }
250
252
internal var debugDescription : String { description. debugDescription }
@@ -283,7 +285,7 @@ extension SystemString {
283
285
internal func withPlatformString< T> (
284
286
_ f: ( UnsafePointer < CInterop . PlatformChar > ) throws -> T
285
287
) rethrows -> T {
286
- try withSystemChars { chars in
288
+ try withNullTerminatedSystemChars { chars in
287
289
let length = chars. count * MemoryLayout< SystemChar> . stride
288
290
return try chars. baseAddress!. withMemoryRebound (
289
291
to: CInterop . PlatformChar. self,
0 commit comments