Escaping.text and Escaping.attribute rebuilt a value that needed escaping one character at a time: result += String(character) for every unescaped character, plus the escaped form for each marker. Re-escaping a text node that holds a single '&' rebuilt the whole value grapheme by grapheme, and the per-character String construction and append dominated the serialize path for entity-bearing content. Copy each run between escapable characters in a single append and rewrite only the markers. The no-escaping-needed fast path (return the value unchanged) is untouched. The output is byte-identical: matching is per-Character so multibyte graphemes copy whole, and the escaped forms are the same. Measured serialize ~7% faster on the 20k-item generated corpus via a same-state A/B (~3.0x to ~2.8x slower than libxml2). Verified by EscapingBulkCopyTests (a per-character-oracle differential with multibyte cases), the Apache Xalan gold-output and C14N canonicalization suites, the full suite, the WASM build, and an adversarial review that included a 400k-input fuzz.
80 lines
3.6 KiB
Swift
80 lines
3.6 KiB
Swift
import Testing
|
|
@testable import PureXML
|
|
|
|
/// `Escaping.text` and `Escaping.attribute` copy the verbatim runs between
|
|
/// escapable characters in bulk instead of appending each character one at a
|
|
/// time. These pin the escaped output against the per-character reference for
|
|
/// markers at the start, end, and adjacent; long verbatim runs around them;
|
|
/// multibyte runs (the copied run spans whole graphemes); both quote styles;
|
|
/// and ASCII-only mode (where a non-ASCII character escapes and an ASCII one is
|
|
/// copied with its run).
|
|
@Suite("Escaping bulk-copy equivalence")
|
|
struct EscapingBulkCopyTests {
|
|
private typealias Escaping = PureXML.Emitting.Escaping
|
|
|
|
/// The per-character escaping the bulk path replaced, kept here as the oracle.
|
|
private func referenceText(_ value: String, asciiOnly: Bool, escapeCarriageReturn: Bool) -> String {
|
|
var result = ""
|
|
for character in value {
|
|
switch character {
|
|
case "&": result += "&"
|
|
case "<": result += "<"
|
|
case ">": result += ">"
|
|
case "\r" where escapeCarriageReturn: result += "
"
|
|
default:
|
|
if asciiOnly {
|
|
for scalar in character.unicodeScalars {
|
|
result += scalar.value > 0x7F ? "&#x\(String(scalar.value, radix: 16, uppercase: true));" : String(scalar)
|
|
}
|
|
} else {
|
|
result += String(character)
|
|
}
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
private static let texts = [
|
|
"plain text no markup",
|
|
"a & b < c > d",
|
|
"&<>", "&start", "end&", "&&&",
|
|
"café & thé", "e\u{0301} < x", "\u{1F600}&\u{1F601}",
|
|
"tab\tnewline\ncr\r", "naïve résumé",
|
|
String(repeating: "x", count: 300) + "&" + String(repeating: "y", count: 300),
|
|
]
|
|
|
|
@Test("text bulk-escape matches the per-character reference")
|
|
func test_textEquivalence() {
|
|
for value in Self.texts {
|
|
for asciiOnly in [false, true] {
|
|
for escapeCR in [false, true] {
|
|
#expect(
|
|
Escaping.text(value, asciiOnly: asciiOnly, escapeCarriageReturn: escapeCR)
|
|
== referenceText(value, asciiOnly: asciiOnly, escapeCarriageReturn: escapeCR),
|
|
"text mismatch for \(value.debugDescription) ascii=\(asciiOnly) cr=\(escapeCR)",
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test("attribute bulk-escape escapes quotes, markup, and whitespace")
|
|
func test_attributeEquivalence() {
|
|
#expect(Escaping.attribute(#"a "b" c"#, quote: "\"") == "a "b" c")
|
|
#expect(Escaping.attribute("a 'b' c", quote: "'") == "a 'b' c")
|
|
#expect(Escaping.attribute(#"a "b" 'c'"#, quote: "\"") == #"a "b" 'c'"#)
|
|
#expect(Escaping.attribute("x & y < z", quote: "\"") == "x & y < z")
|
|
#expect(Escaping.attribute("tab\tnl\ncr\r", quote: "\"") == "tab	nl cr ")
|
|
#expect(Escaping.attribute("café & \"q\"", quote: "\"") == "café & "q"")
|
|
#expect(Escaping.attribute("no escapes here", quote: "\"") == "no escapes here")
|
|
}
|
|
|
|
@Test("a document with re-escaped content round-trips identically")
|
|
func test_documentRoundTrip() throws {
|
|
let document = #"<r a="x & "y"">t & u < v</r>"#
|
|
let tree = try PureXML.parseTree(document)
|
|
let again = try PureXML.parseTree(PureXML.serialize(tree.node))
|
|
#expect(PureXML.serialize(tree.node) == PureXML.serialize(again.node))
|
|
}
|
|
}
|