Skip to content

Commit b0f0290

Browse files
authored
Merge pull request #60831 from Azoy/substring-problems
[5.7] Fix Substring.removeSubrange for entire substring
2 parents d42b0d5 + 8c185fc commit b0f0290

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

stdlib/public/core/StringGutsRangeReplaceable.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,13 @@ extension _StringGuts {
483483
let newUTF8Count =
484484
oldUTF8Count + newUTF8Subrange.count - oldUTF8SubrangeCount
485485

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+
}
489493

490494
startIndex = String.Index(
491495
encodedOffset: utf8StartOffset,
@@ -522,9 +526,14 @@ extension _StringGuts {
522526
// needs to look ahead by more than one Unicode scalar.)
523527
let oldStride = startIndex.characterStride ?? 0
524528
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+
528537
var newStart = String.Index(
529538
encodedOffset: newBounds.lowerBound,
530539
characterStride: newStride

0 commit comments

Comments
 (0)