Skip to content

Commit 755712a

Browse files
committed
[stdlib] StringGuts.replaceSubrange: Fast path for replacing with a fast substring
If the replacement collection is a fast UTF-8 substring, we can simply access its backing store directly — we don’t need to use a circuituous lazy algorithm.
1 parent dc69903 commit 755712a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

stdlib/public/core/StringGutsRangeReplaceable.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,19 @@ extension _StringGuts {
307307
) -> Range<Int>
308308
where C: Collection, C.Iterator.Element == Character {
309309
if isUniqueNative {
310-
if let replStr = newElements as? String, replStr._guts.isFastUTF8 {
311-
return replStr._guts.withFastUTF8 {
312-
uniqueNativeReplaceSubrange(
313-
bounds, with: $0, isASCII: replStr._guts.isASCII)
310+
if let repl = newElements as? String {
311+
if repl._guts.isFastUTF8 {
312+
return repl._guts.withFastUTF8 {
313+
uniqueNativeReplaceSubrange(
314+
bounds, with: $0, isASCII: repl._guts.isASCII)
315+
}
316+
}
317+
} else if let repl = newElements as? Substring {
318+
if repl._wholeGuts.isFastUTF8 {
319+
return repl._wholeGuts.withFastUTF8(range: repl._offsetRange) {
320+
uniqueNativeReplaceSubrange(
321+
bounds, with: $0, isASCII: repl._wholeGuts.isASCII)
322+
}
314323
}
315324
}
316325
return uniqueNativeReplaceSubrange(

0 commit comments

Comments
 (0)