@@ -51,33 +51,12 @@ extension PropertyList {
51
51
writePlistRepresentation ( to: & writer)
52
52
return Data ( writer. bytes)
53
53
}
54
-
55
- /// Escapes the string for plist.
56
- /// Finds the instances of quote (") and backward slash (\) and prepends
57
- /// the escape character backward slash (\).
58
- static func escape( string: String ) -> String {
59
- func needsEscape( _ char: UInt8 ) -> Bool {
60
- return char == UInt8 ( ascii: " \\ " ) || char == UInt8 ( ascii: " \" " )
61
- }
62
-
63
- guard let pos = string. utf8. firstIndex ( where: needsEscape) else {
64
- return string
65
- }
66
- var newString = String ( string [ ..< pos] )
67
- for char in string. utf8 [ pos... ] {
68
- if needsEscape ( char) {
69
- newString += " \\ "
70
- }
71
- newString += String ( UnicodeScalar ( char) )
72
- }
73
- return newString
74
- }
75
54
}
76
55
77
56
fileprivate extension PropertyList {
78
57
struct UTF8Writer {
79
- var level : Int = 0
80
- var bytes : [ UInt8 ] = [ ]
58
+ private ( set ) var level : Int = 0
59
+ private ( set ) var bytes : [ UInt8 ] = [ ]
81
60
init ( ) {
82
61
self += " // !$*UTF8*$! \n "
83
62
}
@@ -88,6 +67,10 @@ fileprivate extension PropertyList {
88
67
level -= 1
89
68
}
90
69
70
+ mutating func append( _ byte: UInt8 ) {
71
+ bytes. append ( byte)
72
+ }
73
+
91
74
static func += ( writer: inout UTF8Writer , str: StaticString ) {
92
75
str. withUTF8Buffer { utf8 in
93
76
writer. bytes += utf8
@@ -104,6 +87,17 @@ fileprivate extension PropertyList {
104
87
self += " "
105
88
}
106
89
}
90
+
91
+ /// Appends the given string, with instances of quote (") and backward slash
92
+ /// (\) characters escaped with a backslash.
93
+ mutating func appendEscaped( _ string: String ) {
94
+ for char in string. utf8 {
95
+ if char == UInt8 ( ascii: " \\ " ) || char == UInt8 ( ascii: " \" " ) {
96
+ append ( UInt8 ( ascii: " \\ " ) )
97
+ }
98
+ append ( char)
99
+ }
100
+ }
107
101
}
108
102
109
103
/// Private function to generate OPENSTEP-style plist representation.
@@ -116,7 +110,7 @@ fileprivate extension PropertyList {
116
110
117
111
case . string( let string) :
118
112
writer += " \" "
119
- writer += PropertyList . escape ( string : string)
113
+ writer. appendEscaped ( string)
120
114
writer += " \" "
121
115
122
116
case . array( let array) :
0 commit comments