Skip to content

Commit 69eab55

Browse files
committed
Fix sub-scalar index distances in foreign UTF8 views
(cherry picked from commit 0887299)
1 parent 5624365 commit 69eab55

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

stdlib/public/core/StringBridge.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ internal func _cocoaStringUTF8Count(
9595
_ target: _CocoaString,
9696
range: Range<Int>
9797
) -> Int? {
98+
if range.isEmpty { return 0 }
9899
var count = 0
99100
let len = _stdlib_binary_CFStringGetLength(target)
100101
let converted = _swift_stdlib_CFStringGetBytes(

stdlib/public/core/StringUTF8View.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ extension String.UTF8View {
505505
// _cocoaStringUTF8Count gave us the scalar aligned count, but we still
506506
// need to compensate for sub-scalar indexing, e.g. if `i` is in the
507507
// middle of a two-byte UTF8 scalar.
508-
let refinedCount = count - (i.transcodedOffset + j.transcodedOffset)
508+
let refinedCount = (count - i.transcodedOffset) + j.transcodedOffset
509509
_internalInvariant(refinedCount == _distance(from: i, to: j))
510510
return refinedCount
511511
}

test/stdlib/Inputs/NSSlowString/NSSlowString.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (NSUInteger)length {
2222
return self.stringHolder.length;
2323
}
2424

25-
- (id)copy {
25+
- (id)copyWithZone:(NSZone *)unused {
2626
return self;
2727
}
2828

0 commit comments

Comments
 (0)