Skip to content

Commit f3a9305

Browse files
committed
[stdlib] Simplify breadcrumbs avoidance paths in String.UTF16View
1 parent 483087a commit f3a9305

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

stdlib/public/core/StringUTF16View.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,9 @@ extension String.UTF16View: BidirectionalCollection {
201201
return _foreignIndex(i, offsetBy: n)
202202
}
203203

204-
if n.magnitude <= _StringBreadcrumbs.breadcrumbStride {
204+
if n.magnitude <= _StringBreadcrumbs.breadcrumbStride, !_guts.isASCII {
205205
// Do not use breadcrumbs if directly computing the result is expected to
206206
// be cheaper.
207-
if _guts.isASCII {
208-
return Index(
209-
_encodedOffset: i._encodedOffset + n
210-
)._scalarAligned._encodingIndependent
211-
}
212207
return _index(i, offsetBy: n)._knownUTF8
213208
}
214209

@@ -230,14 +225,9 @@ extension String.UTF16View: BidirectionalCollection {
230225
return _foreignIndex(i, offsetBy: n, limitedBy: limit)
231226
}
232227

233-
if n.magnitude <= _StringBreadcrumbs.breadcrumbStride {
228+
if n.magnitude <= _StringBreadcrumbs.breadcrumbStride, !_guts.isASCII {
234229
// Do not use breadcrumbs if directly computing the result is expected to
235230
// be cheaper.
236-
if _guts.isASCII {
237-
return (0 ..< _guts.count).index(
238-
i._encodedOffset, offsetBy: n, limitedBy: limit._encodedOffset
239-
).map { Index(_encodedOffset: $0)._scalarAligned._encodingIndependent }
240-
}
241231
return _index(i, offsetBy: n, limitedBy: limit)?._knownUTF8
242232
}
243233

@@ -278,15 +268,15 @@ extension String.UTF16View: BidirectionalCollection {
278268
}
279269

280270
let utf8Distance = end._encodedOffset - start._encodedOffset
281-
if utf8Distance.magnitude <= _StringBreadcrumbs.breadcrumbStride {
271+
if
272+
utf8Distance.magnitude <= _StringBreadcrumbs.breadcrumbStride,
273+
!_guts.isASCII
274+
{
282275
// Do not use breadcrumbs if directly computing the result is expected to
283276
// be cheaper. The conservative threshold above assumes that each UTF-16
284277
// code unit will map to a single UTF-8 code unit, i.e., the worst
285278
// possible (a.k.a. most compact) case with all ASCII scalars.
286279
// FIXME: Figure out if a more optimistic threshold would work better.
287-
if _guts.isASCII {
288-
return end._encodedOffset - start._encodedOffset
289-
}
290280
return _utf16Distance(from: start, to: end)
291281
}
292282
let lower = _nativeGetOffset(for: start)

0 commit comments

Comments
 (0)