File tree Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -723,7 +723,8 @@ extension String {
723
723
encodedAs targetEncoding: TargetEncoding . Type ,
724
724
_ body: ( UnsafePointer < TargetEncoding . CodeUnit > ) throws -> Result
725
725
) rethrows -> Result {
726
- if targetEncoding == UTF8 . self {
726
+ if targetEncoding == UTF8 . self ||
727
+ ( targetEncoding == Unicode . ASCII. self && _guts. isASCII) {
727
728
return try unsafe self. withCString {
728
729
( cPtr: UnsafePointer < CChar > ) -> Result in
729
730
_internalInvariant ( UInt8 . self == TargetEncoding . CodeUnit. self)
Original file line number Diff line number Diff line change @@ -245,6 +245,12 @@ extension _StringGuts {
245
245
internal func _slowWithCString< Result> (
246
246
_ body: ( UnsafePointer < Int8 > ) throws -> Result
247
247
) rethrows -> Result {
248
+ if _object. isFastZeroTerminated {
249
+ // This branch looks unreachable, but can be reached via `withCString`
250
+ // in binaries that haven't been recompiled since the termination flag
251
+ // was added to _StringObject. Retry the fast path if so.
252
+ return try withCString ( body)
253
+ }
248
254
_internalInvariant ( !_object. isFastZeroTerminated)
249
255
return try String ( self ) . utf8CString. withUnsafeBufferPointer {
250
256
let ptr = unsafe $0. baseAddress . _unsafelyUnwrappedUnchecked
You can’t perform that action at this time.
0 commit comments