File tree Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -18,9 +18,31 @@ internal func _stringCompare(
18
18
_ lhs: _StringGuts , _ rhs: _StringGuts , expecting: _StringComparisonResult
19
19
) -> Bool {
20
20
if lhs. rawBits == rhs. rawBits { return expecting == . equal }
21
+ return _stringCompareWithSmolCheck ( lhs, rhs, expecting: expecting)
22
+ }
23
+
24
+ @usableFromInline
25
+ @_effects ( readonly)
26
+ internal func _stringCompareWithSmolCheck(
27
+ _ lhs: _StringGuts , _ rhs: _StringGuts , expecting: _StringComparisonResult
28
+ ) -> Bool {
29
+ // ASCII small-string fast-path:
30
+ if lhs. isSmallASCII && rhs. isSmallASCII {
31
+ let lhsRaw = lhs. asSmall. _storage
32
+ let rhsRaw = rhs. asSmall. _storage
33
+
34
+ if lhsRaw. 0 != rhsRaw. 0 {
35
+ return _lexicographicalCompare (
36
+ lhsRaw. 0 . byteSwapped, rhsRaw. 0 . byteSwapped, expecting: expecting)
37
+ }
38
+ return _lexicographicalCompare (
39
+ lhsRaw. 1 . byteSwapped, rhsRaw. 1 . byteSwapped, expecting: expecting)
40
+ }
41
+
21
42
return _stringCompareInternal ( lhs, rhs, expecting: expecting)
22
43
}
23
44
45
+ @inline ( never) // Keep `_stringCompareWithSmolCheck` fast-path fast
24
46
@usableFromInline
25
47
@_effects ( readonly)
26
48
internal func _stringCompareInternal(
Original file line number Diff line number Diff line change @@ -169,5 +169,16 @@ extension String {
169
169
) -> String {
170
170
return String . _fromCodeUnits ( utf16, encoding: UTF16 . self, repair: true ) !. 0
171
171
}
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
+ }
172
183
}
173
184
Original file line number Diff line number Diff line change @@ -90,6 +90,10 @@ extension _StringGuts {
90
90
@inline ( __always) get { return _object. isSmall }
91
91
}
92
92
93
+ internal var isSmallASCII : Bool {
94
+ @inline ( __always) get { return _object. isSmall && _object. smallIsASCII }
95
+ }
96
+
93
97
@inlinable
94
98
internal var asSmall : _SmallString {
95
99
@inline ( __always) get { return _SmallString ( _object) }
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ extension String {
20
20
/// - Complexity: O(*n*), where *n* is the length of `substring`.
21
21
@inlinable
22
22
public init ( _ substring: __shared Substring) {
23
- self = substring . _withUTF8 { return String . _uncheckedFromUTF8 ( $0 ) }
23
+ self = String . _fromSubstring ( substring )
24
24
}
25
25
}
26
26
You can’t perform that action at this time.
0 commit comments