Skip to content

Commit a0bf388

Browse files
authored
UTF8-validation: speed up findInvalidRange (#75381)
Changes findInvalidRange from being O(n) to O(1).
1 parent 7c01181 commit a0bf388

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

stdlib/public/core/StringUTF8Validation.swift

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ internal func validateUTF8(_ buf: UnsafeBufferPointer<UInt8>) -> UTF8ValidationR
104104
_ = iter.next()
105105
while let cu = iter.next(), UTF8.isContinuation(cu) {
106106
endIndex += 1
107+
// Unicode's Maximal subpart of an ill-formed subsequence will yield
108+
// at most 3 bytes of error.
109+
if buf.distance(from: buf.startIndex, to: endIndex) >= 3 {
110+
break
111+
}
107112
}
108113
let illegalRange = Range(buf.startIndex...endIndex)
109114
_internalInvariant(illegalRange.clamped(to: (buf.startIndex..<buf.endIndex)) == illegalRange,

0 commit comments

Comments
 (0)