Skip to content

UTF8-validation: speed up findInvalidRange #75381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stdlib/public/core/StringUTF8Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ internal func validateUTF8(_ buf: UnsafeBufferPointer<UInt8>) -> UTF8ValidationR
_ = iter.next()
while let cu = iter.next(), UTF8.isContinuation(cu) {
endIndex += 1
// Unicode's Maximal subpart of an ill-formed subsequence will yield
// at most 3 bytes of error.
if buf.distance(from: buf.startIndex, to: endIndex) >= 3 {
break
}
}
let illegalRange = Range(buf.startIndex...endIndex)
_internalInvariant(illegalRange.clamped(to: (buf.startIndex..<buf.endIndex)) == illegalRange,
Expand Down