@@ -483,9 +483,13 @@ extension _StringGuts {
483
483
let newUTF8Count =
484
484
oldUTF8Count + newUTF8Subrange. count - oldUTF8SubrangeCount
485
485
486
- // Get the character stride in the entire string, not just the substring.
487
- // (Characters in a substring may end beyond the bounds of it.)
488
- let newStride = _opaqueCharacterStride ( startingAt: utf8StartOffset)
486
+ var newStride = 0
487
+
488
+ if !newUTF8Subrange. isEmpty {
489
+ // Get the character stride in the entire string, not just the substring.
490
+ // (Characters in a substring may end beyond the bounds of it.)
491
+ newStride = _opaqueCharacterStride ( startingAt: utf8StartOffset)
492
+ }
489
493
490
494
startIndex = String . Index (
491
495
encodedOffset: utf8StartOffset,
@@ -522,9 +526,14 @@ extension _StringGuts {
522
526
// needs to look ahead by more than one Unicode scalar.)
523
527
let oldStride = startIndex. characterStride ?? 0
524
528
if oldRange. lowerBound <= oldBounds. lowerBound &+ oldStride {
525
- // Get the character stride in the entire string, not just the substring.
526
- // (Characters in a substring may end beyond the bounds of it.)
527
- let newStride = _opaqueCharacterStride ( startingAt: newBounds. lowerBound)
529
+ var newStride = 0
530
+
531
+ if !newBounds. isEmpty {
532
+ // Get the character stride in the entire string, not just the substring.
533
+ // (Characters in a substring may end beyond the bounds of it.)
534
+ newStride = _opaqueCharacterStride ( startingAt: newBounds. lowerBound)
535
+ }
536
+
528
537
var newStart = String . Index (
529
538
encodedOffset: newBounds. lowerBound,
530
539
characterStride: newStride
0 commit comments