@@ -354,7 +354,8 @@ extension Substring: StringProtocol {
354
354
// `Substring`'s bounds do not fall on grapheme boundaries in `base`.
355
355
356
356
// TODO: known-ASCII and single-scalar-grapheme fast path, etc.
357
- var i = _validateInclusiveScalarIndex ( i)
357
+ var i = _roundDownToNearestCharacter (
358
+ _validateInclusiveScalarIndex ( i) )
358
359
if distance >= 0 {
359
360
for _ in stride ( from: 0 , to: distance, by: 1 ) {
360
361
_precondition ( i < endIndex, " String index is out of bounds " )
@@ -390,7 +391,7 @@ extension Substring: StringProtocol {
390
391
let limit = _wholeGuts. ensureMatchingEncoding ( limit)
391
392
let start = _wholeGuts. ensureMatchingEncoding ( i)
392
393
393
- var i = _validateInclusiveScalarIndex ( i)
394
+ var i = _roundDownToNearestCharacter ( _validateInclusiveScalarIndex ( i) )
394
395
if distance >= 0 {
395
396
for _ in stride ( from: 0 , to: distance, by: 1 ) {
396
397
guard limit < start || i < limit else { return nil }
@@ -422,8 +423,10 @@ extension Substring: StringProtocol {
422
423
// grapheme breaks -- swapping `start` and `end` may change the magnitude of
423
424
// the result.
424
425
425
- let start = _validateInclusiveScalarIndex ( start)
426
- let end = _validateInclusiveScalarIndex ( end)
426
+ let start = _roundDownToNearestCharacter (
427
+ _validateInclusiveScalarIndex ( start) )
428
+ let end = _roundDownToNearestCharacter (
429
+ _validateInclusiveScalarIndex ( end) )
427
430
428
431
// TODO: known-ASCII and single-scalar-grapheme fast path, etc.
429
432
0 commit comments