Skip to content

Commit 209e8a9

Browse files
authored
Merge pull request #26 from chriseidhof/linux-support
Linux Support
2 parents 522ac6f + c4920e2 commit 209e8a9

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Sources/Swim/String+XML.swift

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

33
extension String {
44
func addingXMLEncoding() -> String {
5-
withCFString { string -> NSString in
6-
CFXMLCreateStringByEscapingEntities(nil, string, nil)
7-
} as String
5+
var result = ""
6+
result.reserveCapacity(count)
7+
return unicodeScalars.reduce(into: result, { $0.append($1.named_escapingIfNeeded) })
88
}
9+
}
910

10-
private func withCFString<Result>(_ body: (CFString) throws -> Result) rethrows -> Result {
11-
try withCString { cString in
12-
try body(CFStringCreateWithCString(nil, cString, CFStringBuiltInEncodings.UTF8.rawValue))
11+
extension UnicodeScalar {
12+
fileprivate var named_escapingIfNeeded: String {
13+
switch value {
14+
case ("&" as Unicode.Scalar).value:
15+
return "&amp;"
16+
case ("<" as Unicode.Scalar).value:
17+
return "&lt;"
18+
case (">" as Unicode.Scalar).value:
19+
return "&gt;"
20+
case ("\'" as Unicode.Scalar).value:
21+
return "&apos;"
22+
case ("\"" as Unicode.Scalar).value:
23+
return "&quot;"
24+
default:
25+
return String(self)
1326
}
1427
}
1528
}

Tests/SwimTests/SwimTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ final class SwimTests: XCTestCase {
1616
XCTAssertEqual(n.rendered, "\n3 &gt; 1")
1717
}
1818

19+
func testUnicodeAndEscaping() {
20+
let n: Node = "500 €"
21+
var result = ""
22+
n.write(to: &result)
23+
XCTAssertEqual(result, "\n500 €")
24+
}
25+
1926
func testRaw() {
2027
let n: Node = Node.raw("<marquee>Hello</marquee>")
2128
XCTAssertEqual(n.rendered, "\n<marquee>Hello</marquee>")

0 commit comments

Comments
 (0)