Skip to content

Commit d2a0ac8

Browse files
committed
Bug fix for negative offsets and limits
1 parent 381e8ce commit d2a0ac8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

stdlib/public/core/StringUTF16View.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ extension String.UTF16View: BidirectionalCollection {
109109
let iOffset = _getOffset(for: i)
110110
let limitOffset = _getOffset(for: limit)
111111

112-
if _slowPath(limitOffset < iOffset + n) {
113-
// If distance > 0, limit has no effect if it is less than i. Likewise,
114-
// if distance < 0, limit has no effect if it is greater than i.
115-
if n > 0 && limitOffset >= iOffset { return nil }
116-
if n < 0 && limitOffset <= iOffset { return nil }
112+
// If distance < 0, limit has no effect if it is greater than i.
113+
if _slowPath(n < 0 && limit <= i && limitOffset > iOffset + n) {
114+
return nil
115+
}
116+
// If distance > 0, limit has no effect if it is less than i. Likewise,
117+
if _slowPath(n >= 0 && limit >= i && limitOffset < iOffset + n) {
118+
return nil
117119
}
118120

119121
let result = _getIndex(for: iOffset + n)

0 commit comments

Comments
 (0)