Skip to content

Commit

Permalink
Merge pull request #26 from chriseidhof/linux-support
Browse files Browse the repository at this point in the history
Linux Support
  • Loading branch information
robb authored Jun 28, 2021
2 parents 522ac6f + c4920e2 commit 209e8a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Sources/Swim/String+XML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import Foundation

extension String {
func addingXMLEncoding() -> String {
withCFString { string -> NSString in
CFXMLCreateStringByEscapingEntities(nil, string, nil)
} as String
var result = ""
result.reserveCapacity(count)
return unicodeScalars.reduce(into: result, { $0.append($1.named_escapingIfNeeded) })
}
}

private func withCFString<Result>(_ body: (CFString) throws -> Result) rethrows -> Result {
try withCString { cString in
try body(CFStringCreateWithCString(nil, cString, CFStringBuiltInEncodings.UTF8.rawValue))
extension UnicodeScalar {
fileprivate var named_escapingIfNeeded: String {
switch value {
case ("&" as Unicode.Scalar).value:
return "&amp;"
case ("<" as Unicode.Scalar).value:
return "&lt;"
case (">" as Unicode.Scalar).value:
return "&gt;"
case ("\'" as Unicode.Scalar).value:
return "&apos;"
case ("\"" as Unicode.Scalar).value:
return "&quot;"
default:
return String(self)
}
}
}
7 changes: 7 additions & 0 deletions Tests/SwimTests/SwimTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ final class SwimTests: XCTestCase {
XCTAssertEqual(n.rendered, "\n3 &gt; 1")
}

func testUnicodeAndEscaping() {
let n: Node = "500 €"
var result = ""
n.write(to: &result)
XCTAssertEqual(result, "\n500 €")
}

func testRaw() {
let n: Node = Node.raw("<marquee>Hello</marquee>")
XCTAssertEqual(n.rendered, "\n<marquee>Hello</marquee>")
Expand Down

0 comments on commit 209e8a9

Please sign in to comment.