Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
aehlke committed Aug 2, 2024
1 parent 7df0685 commit a690c8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
12 changes: 5 additions & 7 deletions Sources/CharacterReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ public final class CharacterReader {
return String(scalar)
}

public func consumeToAny(_ chars: Set<UnicodeScalar>) -> String {
public func consumeToAny(_ chars: Set<Unicode.Scalar.UTF8View.Element>) -> String {
let start = pos

let utf8CodeUnits = Set(chars.flatMap { $0.utf8 })

while pos < input.endIndex {
let utf8Byte = input[pos]
if utf8CodeUnits.contains(utf8Byte) {
if chars.contains(utf8Byte) {
break
}
input.formIndex(after: &pos)
Expand Down Expand Up @@ -276,19 +274,19 @@ public final class CharacterReader {
}
}

static let dataTerminators: Set<UnicodeScalar> = [.Ampersand, .LessThan, TokeniserStateVars.nullScalr]
static let dataTerminators = Set([.Ampersand, .LessThan, TokeniserStateVars.nullScalr].flatMap { $0.utf8 })

public func consumeData() -> String {
return consumeToAny(CharacterReader.dataTerminators)
}

static let tagNameTerminators: Set<UnicodeScalar> = [.BackslashT, .BackslashN, .BackslashR, .BackslashF, .Space, .Slash, .GreaterThan, TokeniserStateVars.nullScalr]
static let tagNameTerminators = Set([.BackslashT, .BackslashN, .BackslashR, .BackslashF, .Space, .Slash, .GreaterThan, TokeniserStateVars.nullScalr].flatMap { $0.utf8 })

public func consumeTagName() -> String {
return consumeToAny(CharacterReader.tagNameTerminators)
}

public func consumeToAnySorted(_ chars: Set<UnicodeScalar>) -> String {
public func consumeToAnySorted(_ chars: Set<Unicode.Scalar.UTF8View.Element>) -> String {
return consumeToAny(chars)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Entities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Entities {
return left.value != right.value
}

private static let codeDelims: Set<UnicodeScalar> = Set([",", ";"])
private static let codeDelims = Set([",", ";"].flatMap { $0.utf8 })

init(string: String, size: Int, id: Int) {

Expand Down
14 changes: 7 additions & 7 deletions Sources/TokeniserState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ protocol TokeniserStateProtocol {
public class TokeniserStateVars {
public static let nullScalr: UnicodeScalar = "\u{0000}"

static let attributeSingleValueChars = Set(["'", UnicodeScalar.Ampersand, nullScalr])
static let attributeDoubleValueChars = Set(["\"", UnicodeScalar.Ampersand, nullScalr])
static let attributeNameChars = Set([UnicodeScalar.BackslashT, "\n", "\r", UnicodeScalar.BackslashF, " ", "/", "=", ">", nullScalr, "\"", "'", UnicodeScalar.LessThan])
static let attributeValueUnquoted = Set([UnicodeScalar.BackslashT, "\n", "\r", UnicodeScalar.BackslashF, " ", UnicodeScalar.Ampersand, ">", nullScalr, "\"", "'", UnicodeScalar.LessThan, "=", "`"])
static let attributeSingleValueChars = Set(["'", UnicodeScalar.Ampersand, nullScalr].flatMap { $0.utf8 })
static let attributeDoubleValueChars = Set(["\"", UnicodeScalar.Ampersand, nullScalr].flatMap { $0.utf8 })
static let attributeNameChars = Set([UnicodeScalar.BackslashT, "\n", "\r", UnicodeScalar.BackslashF, " ", "/", "=", ">", nullScalr, "\"", "'", UnicodeScalar.LessThan].flatMap { $0.utf8 })
static let attributeValueUnquoted = Set([UnicodeScalar.BackslashT, "\n", "\r", UnicodeScalar.BackslashF, " ", UnicodeScalar.Ampersand, ">", nullScalr, "\"", "'", UnicodeScalar.LessThan, "=", "`"].flatMap { $0.utf8 })

static let dataDefaultStopChars: Set<UnicodeScalar> = [UnicodeScalar.Ampersand, UnicodeScalar.LessThan, TokeniserStateVars.nullScalr]
static let commentDefaultStopChars: Set<UnicodeScalar> = ["-", TokeniserStateVars.nullScalr]
static let readDataDefaultStopChars: Set<UnicodeScalar> = [UnicodeScalar.LessThan, TokeniserStateVars.nullScalr]
static let dataDefaultStopChars = Set([UnicodeScalar.Ampersand, UnicodeScalar.LessThan, TokeniserStateVars.nullScalr].flatMap { $0.utf8 })
static let commentDefaultStopChars = Set(["-", TokeniserStateVars.nullScalr].flatMap { $0.utf8 })
static let readDataDefaultStopChars = Set([UnicodeScalar.LessThan, TokeniserStateVars.nullScalr].flatMap { $0.utf8 })


static let replacementChar: UnicodeScalar = Tokeniser.replacementChar
Expand Down

0 comments on commit a690c8a

Please sign in to comment.