11/*
22 This source file is part of the Swift System open source project
33
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
55 Licensed under Apache License v2.0 with Runtime Library Exception
66
77 See https://swift.org/LICENSE.txt for license information
@@ -169,24 +169,20 @@ extension SystemString: Hashable, Codable {}
169169
170170extension SystemString {
171171
172- // withSystemChars includes the null terminator
173- internal func withSystemChars< T> (
172+ internal func withNullTerminatedSystemChars< T> (
174173 _ f: ( UnsafeBufferPointer < SystemChar > ) throws -> T
175174 ) rethrows -> T {
176- try nullTerminatedStorage. withContiguousStorageIfAvailable ( f) !
175+ try nullTerminatedStorage. withUnsafeBufferPointer ( f)
177176 }
178177
178+ // withCodeUnits does not include the null terminator
179179 internal func withCodeUnits< T> (
180180 _ f: ( UnsafeBufferPointer < CInterop . PlatformUnicodeEncoding . CodeUnit > ) throws -> T
181181 ) 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 ) )
190186 }
191187 }
192188 }
@@ -202,7 +198,9 @@ extension Slice where Base == SystemString {
202198 }
203199
204200 internal var string : String {
205- withCodeUnits { String ( decoding: $0, as: CInterop . PlatformUnicodeEncoding. self) }
201+ withCodeUnits {
202+ String ( decoding: $0, as: CInterop . PlatformUnicodeEncoding. self)
203+ }
206204 }
207205
208206 internal func withPlatformString< T> (
@@ -244,7 +242,11 @@ extension SystemString: ExpressibleByStringLiteral {
244242}
245243
246244extension 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+ }
248250
249251 internal var description : String { string }
250252 internal var debugDescription : String { description. debugDescription }
@@ -283,7 +285,7 @@ extension SystemString {
283285 internal func withPlatformString< T> (
284286 _ f: ( UnsafePointer < CInterop . PlatformChar > ) throws -> T
285287 ) rethrows -> T {
286- try withSystemChars { chars in
288+ try withNullTerminatedSystemChars { chars in
287289 let length = chars. count * MemoryLayout< SystemChar> . stride
288290 return try chars. baseAddress!. withMemoryRebound (
289291 to: CInterop . PlatformChar. self,
0 commit comments