Skip to content

Commit e65698c

Browse files
committed
StringGuts: Apply ARC hacks to Substring as well.
In comparisons with Substrings, also do the ARC hacks we did with String.
1 parent d284163 commit e65698c

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

stdlib/public/core/StringComparable.swift

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,31 @@ extension _StringGuts {
8989
let right = _StringGuts(
9090
object: _StringObject(rawBits: rightBits.0),
9191
otherBits: rightBits.1)
92-
return _compareDeterministicUnicodeCollation(left, to: right)
92+
return _compareDeterministicUnicodeCollation(
93+
left, 0..<left.count, to: right, 0..<right.count)
9394
}
94-
95-
/// Compares two slices of strings with the Unicode Collation Algorithm.
96-
@_inlineable
97-
public // @testable
95+
@inline(never)
96+
@effects(readonly)
97+
public
9898
static func _compareDeterministicUnicodeCollation(
99-
_ left: _StringGuts, to right: _StringGuts) -> Int {
99+
_leftUnsafeStringGutsBitPattern leftBits: (UInt, UInt),
100+
_ leftRange: Range<Int>,
101+
_rightUnsafeStringGutsBitPattern rightBits: (UInt, UInt),
102+
_ rightRange: Range<Int>
103+
) -> Int {
104+
let left = _StringGuts(
105+
object: _StringObject(rawBits: leftBits.0),
106+
otherBits: leftBits.1)
107+
let right = _StringGuts(
108+
object: _StringObject(rawBits: rightBits.0),
109+
otherBits: rightBits.1)
100110
return _compareDeterministicUnicodeCollation(
101-
left, 0..<left.count, to: right, 0..<right.count)
111+
left, leftRange, to: right, rightRange)
102112
}
103113

104114
/// Compares two slices of strings with the Unicode Collation Algorithm.
105115
@inline(never) // Hide the CF/ICU dependency
116+
@effects(readonly)
106117
public // @testable
107118
static func _compareDeterministicUnicodeCollation(
108119
_ left: _StringGuts, _ leftRange: Range<Int>,
@@ -206,6 +217,8 @@ extension _StringGuts {
206217
_ left: _StringGuts, _ leftRange: Range<Int>,
207218
to right: _StringGuts, _ rightRange: Range<Int>
208219
) -> Int {
220+
defer { _fixLifetime(left) }
221+
defer { _fixLifetime(right) }
209222
#if _runtime(_ObjC)
210223
// We only want to perform this optimization on objc runtimes. Elsewhere,
211224
// we will make it follow the unicode collation algorithm even for ASCII.
@@ -216,25 +229,23 @@ extension _StringGuts {
216229
let leftASCII = left._unmanagedASCIIView[leftRange]
217230
let rightASCII = right._unmanagedASCIIView[rightRange]
218231
let result = leftASCII.compareASCII(to: rightASCII)
219-
_fixLifetime(left)
220-
_fixLifetime(right)
221232
return result
222233
}
223-
return _compareDeterministicUnicodeCollation(
224-
left, leftRange,
225-
to: right, rightRange)
226-
#else
227-
return _compareDeterministicUnicodeCollation(
228-
left, leftRange,
229-
to: right, rightRange)
230234
#endif
231-
}
235+
let leftBits = (left._object.rawBits, left._otherBits)
236+
let rightBits = (right._object.rawBits, right._otherBits)
237+
return _compareDeterministicUnicodeCollation(
238+
_leftUnsafeStringGutsBitPattern: leftBits, leftRange,
239+
_rightUnsafeStringGutsBitPattern: rightBits, rightRange)
240+
}
232241

233242
@_inlineable
234243
@_versioned
235244
internal static func compare(
236245
_ left: _StringGuts, to right: _StringGuts
237246
) -> Int {
247+
defer { _fixLifetime(left) }
248+
defer { _fixLifetime(right) }
238249
#if _runtime(_ObjC)
239250
// We only want to perform this optimization on objc runtimes. Elsewhere,
240251
// we will make it follow the unicode collation algorithm even for ASCII.
@@ -245,8 +256,6 @@ extension _StringGuts {
245256
let leftASCII = left._unmanagedASCIIView
246257
let rightASCII = right._unmanagedASCIIView
247258
let result = leftASCII.compareASCII(to: rightASCII)
248-
_fixLifetime(left)
249-
_fixLifetime(right)
250259
return result
251260
}
252261
#endif

0 commit comments

Comments
 (0)