Skip to content

Commit 0887299

Browse files
committed
Fix sub-scalar index distances in foreign UTF8 views
1 parent c5fc715 commit 0887299

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
@@ -210,6 +210,7 @@ internal func _cocoaStringUTF8Count(
210210
_ target: _CocoaString,
211211
range: Range<Int>
212212
) -> Int? {
213+
if range.isEmpty { return 0 }
213214
return _NSStringUTF8Count(_objc(target), range: range)
214215
}
215216

stdlib/public/core/StringUTF8View.swift

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

test/stdlib/Inputs/NSSlowString/NSSlowString.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ - (NSUInteger)length {
3131
return self.stringHolder.length;
3232
}
3333

34-
- (id)copy {
34+
- (id)copyWithZone:(NSZone *)unused {
3535
return self;
3636
}
3737

0 commit comments

Comments
 (0)