Files
T. R. Bernstein 6061715025
Some checks failed
CI / macOS (push) Has been cancelled
CI / linux (swift:6.0) (push) Has been cancelled
CI / linux (swift:6.1) (push) Has been cancelled
CI / linux (swift:6.2) (push) Has been cancelled
CI / windows (6.1) (push) Has been cancelled
Adapt project for Astzweig
2025-09-29 15:03:48 +02:00

25 lines
761 B
Swift

extension String {
private static let htmlEscapedCharacters: [Character: String] = [
"<": "&lt;",
">": "&gt;",
"&": "&amp;",
"\"": "&quot;",
"'": "&#39;",
]
/// HTML escape string. Replace '<', '>' and '&' with HTML escaped versions
func htmlEscape() -> String {
var newString = ""
newString.reserveCapacity(count)
// currently doing this by going through each character could speed
// this us by treating as an array of UInt8's
for c in self {
if let replacement = Self.htmlEscapedCharacters[c] {
newString += replacement
} else {
newString.append(c)
}
}
return newString
}
}