File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -2,14 +2,29 @@ import Foundation
22
33extension 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
1115extension 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 " & "
You can’t perform that action at this time.
0 commit comments