Skip to content

Commit 7e01603

Browse files
committed
Scan String before escaping
1 parent 209e8a9 commit 7e01603

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Sources/Swim/String+XML.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@ import Foundation
22

33
extension String {
44
func addingXMLEncoding() -> String {
5+
guard unicodeScalars.contains(where: \.needsEscaping) else {
6+
return self
7+
}
8+
59
var result = ""
610
result.reserveCapacity(count)
7-
return unicodeScalars.reduce(into: result, { $0.append($1.named_escapingIfNeeded) })
11+
return unicodeScalars.reduce(into: result, { $0.append($1.escapingIfNeeded) })
812
}
913
}
1014

1115
extension UnicodeScalar {
12-
fileprivate var named_escapingIfNeeded: String {
16+
fileprivate var needsEscaping: Bool {
17+
switch value {
18+
case ("&" as Unicode.Scalar).value, ("<" as Unicode.Scalar).value,
19+
(">" as Unicode.Scalar).value, ("\'" as Unicode.Scalar).value,
20+
("\"" as Unicode.Scalar).value:
21+
return true
22+
default:
23+
return false
24+
}
25+
}
26+
27+
fileprivate var escapingIfNeeded: String {
1328
switch value {
1429
case ("&" as Unicode.Scalar).value:
1530
return "&amp;"

0 commit comments

Comments
 (0)