Keep the reader on its byte fast path through dispatch and name/attribute scanning so plain-ASCII documents avoid the per-Character String machinery (String.Iterator.next, string comparison, small-string materialization, bridge-object ARC) that a profile indicted as the parse bottleneck. The root cause was that a single buffered lookahead Character defeated every existing byte path, each of which requires an empty buffer, and the content, markup, and attribute dispatch each peeked a Character before scanning. Add a non-buffering peekByte for dispatch, a byte-mode skipSpace, an all-ASCII takeASCIIName, and an attributeRunBytes bulk scanner (with the 3.3.3 whitespace-to-space transform, deferring any carriage return to the Character path so CRLF is not double-counted). Detect a text run's closing '<' via peekByte instead of buffering it, which had cascaded the next element's whole dispatch and name scan onto the slow path. Skip reference decoding for ampersand-free text. The Character path remains the exact fallback for a streaming source, spliced entity text, and any non-ASCII byte; the two paths share scanContentMarkup so they cannot diverge. Measured parse 20.1x -> 9.5x slower than libxml2 on the 20k-item generated corpus, roughly a 53 percent wall-clock reduction. A differential oracle pins the byte and Character paths to identical event streams and error marks over an adversarial corpus, alongside boundary tests. Verified against the full local suite, the Apache Xalan XSLT gold-output corpus, the W3C XML conformance corpora, the RELAX NG spec suite, and the WASM build. No spec behavior changed.
36 lines
1.3 KiB
Swift
36 lines
1.3 KiB
Swift
extension StringProtocol {
|
|
/// Trims leading and trailing XML whitespace without Foundation.
|
|
func trimmingXMLWhitespace() -> String {
|
|
var slice = self[...]
|
|
while let first = slice.first, first.isXMLWhitespace {
|
|
slice = slice.dropFirst()
|
|
}
|
|
while let last = slice.last, last.isXMLWhitespace {
|
|
slice = slice.dropLast()
|
|
}
|
|
return String(slice)
|
|
}
|
|
}
|
|
|
|
extension Character {
|
|
private typealias XML = PureXML.Parsing.XMLCharacter
|
|
|
|
/// XML S production: space, tab, carriage return, line feed.
|
|
var isXMLWhitespace: Bool {
|
|
unicodeScalars.count == 1 && unicodeScalars.first.map(XML.isWhitespace) == true
|
|
}
|
|
|
|
/// Whether this character may start an XML name. A grapheme qualifies when its
|
|
/// first scalar is a NameStartChar and any trailing scalars (combining marks)
|
|
/// are NameChar.
|
|
var isXMLNameStart: Bool {
|
|
guard let first = unicodeScalars.first, XML.isNameStart(first) else { return false }
|
|
return unicodeScalars.dropFirst().allSatisfy(XML.isNameChar)
|
|
}
|
|
|
|
/// Whether this character may continue an XML name (every scalar a NameChar).
|
|
var isXMLNameContinuation: Bool {
|
|
!unicodeScalars.isEmpty && unicodeScalars.allSatisfy(XML.isNameChar)
|
|
}
|
|
}
|