Skip to content

Commit 255c17a

Browse files
committed
[String] String-from-whole-Substring fast-path.
Add in a fast-path for Strings created from Substring which covers the entire String. Put String-from-Substring behind a non-inlinable resilience barrier for future flexibility.
1 parent f898abe commit 255c17a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

stdlib/public/core/StringCreate.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,16 @@ extension String {
169169
) -> String {
170170
return String._fromCodeUnits(utf16, encoding: UTF16.self, repair: true)!.0
171171
}
172+
173+
@usableFromInline
174+
internal static func _fromSubstring(
175+
_ substring: __shared Substring
176+
) -> String {
177+
if substring._offsetRange == substring._wholeString._offsetRange {
178+
return substring._wholeString
179+
}
180+
181+
return substring._withUTF8 { return String._uncheckedFromUTF8($0) }
182+
}
172183
}
173184

stdlib/public/core/Substring.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension String {
2020
/// - Complexity: O(*n*), where *n* is the length of `substring`.
2121
@inlinable
2222
public init(_ substring: __shared Substring) {
23-
self = substring._withUTF8 { return String._uncheckedFromUTF8($0) }
23+
self = String._fromSubstring(substring)
2424
}
2525
}
2626

0 commit comments

Comments
 (0)