Skip to content

Commit ec6729a

Browse files
committed
[String] Assertion logic and isASCII bug fix.
Fix bugs in assertion logic and properly update the isASCII bit on RRC. RRC tests added.
1 parent 1939d16 commit ec6729a

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

stdlib/public/core/StringStorage.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ extension _StringStorage {
446446
) {
447447
_sanityCheck(lower <= upper)
448448
let replCount = replacement.count
449-
_sanityCheck((upper - lower) + replCount <= unusedCapacity)
449+
_sanityCheck(replCount - (upper - lower) <= unusedCapacity)
450450

451451
// Position the tail
452452
let lowerPtr = mutableStart + lower
@@ -459,9 +459,8 @@ extension _StringStorage {
459459
mutating: replacement.baseAddress._unsafelyUnwrappedUnchecked),
460460
count: replCount)
461461

462-
_postRRCAdjust(
463-
newCount: lower + replCount + tailCount,
464-
newIsASCII: self.isASCII && isASCII)
462+
let isASCII = self.isASCII && _allASCII(replacement)
463+
_postRRCAdjust(newCount: lower + replCount + tailCount, newIsASCII: isASCII)
465464
}
466465

467466

@@ -473,15 +472,15 @@ extension _StringStorage {
473472
replacementCount replCount: Int
474473
) where C.Element == UInt8 {
475474
_sanityCheck(lower <= upper)
476-
_sanityCheck((upper - lower) + replCount <= unusedCapacity)
475+
_sanityCheck(replCount - (upper - lower) <= unusedCapacity)
477476

478477
// Position the tail
479478
let lowerPtr = mutableStart + lower
480479
let tailCount = _slideTail(
481480
src: mutableStart + upper, dst: lowerPtr + replCount)
482481

483482
// Copy in the contents
484-
var isASCII = true
483+
var isASCII = self.isASCII
485484
var srcCount = 0
486485
for cu in replacement {
487486
if cu >= 0x80 { isASCII = false }
@@ -491,8 +490,7 @@ extension _StringStorage {
491490
_sanityCheck(srcCount == replCount)
492491

493492
_postRRCAdjust(
494-
newCount: lower + replCount + tailCount,
495-
newIsASCII: self.isASCII && isASCII)
493+
newCount: lower + replCount + tailCount, newIsASCII: isASCII)
496494
}
497495
}
498496

validation-test/stdlib/String.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,24 @@ StringTests.test("UnicodeScalarViewReplace") {
10471047
}
10481048
}
10491049

1050+
StringTests.test("StringRRC") {
1051+
let narrow = "01234567890"
1052+
let wide = "ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪ"
1053+
for s1 in [narrow, wide] {
1054+
for s2 in [narrow, wide] {
1055+
let doubleS2 = Array(String(makeStringGuts(s2 + s2)).utf16)
1056+
checkRangeReplaceable(
1057+
{ () -> String in String(makeStringGuts(s1)) },
1058+
{ String(decoding: doubleS2[0..<$0], as: UTF16.self) }
1059+
)
1060+
checkRangeReplaceable(
1061+
{ String(makeStringGuts(s1)) },
1062+
{ Array(String(makeStringGuts(s2 + s2)))[0..<$0] }
1063+
)
1064+
}
1065+
}
1066+
}
1067+
10501068
StringTests.test("reserveCapacity") {
10511069
var s = ""
10521070
let id0 = s.bufferID

0 commit comments

Comments
 (0)