@@ -25,100 +25,100 @@ import Foundation
2525/// Immutability of character sets is implemented at the Scheme library level.
2626///
2727public final class CharSet : Reference {
28-
28+
2929 /// Native character set
3030 public private( set) var chars : Set < UniChar >
31-
31+
3232 /// Is this character set immutable? Immutability is not enforced via this class.
3333 public let immutable : Bool
34-
34+
3535 /// Name of this reference type
3636 public override var typeDescription : String {
3737 return " char-set "
3838 }
39-
39+
4040 public override init ( ) {
4141 self . chars = [ ]
4242 self . immutable = false
4343 }
44-
44+
4545 public init ( immutable: Bool ) {
4646 self . chars = [ ]
4747 self . immutable = immutable
4848 }
49-
49+
5050 public init ( copy cs: CharSet , immutable: Bool = false ) {
5151 self . chars = cs. chars
5252 self . immutable = immutable
5353 }
54-
54+
5555 public convenience init ( charsIn str: NSString , immutable: Bool = false ) {
5656 self . init ( immutable: immutable)
5757 self . chars. reserveCapacity ( str. length)
5858 for i in 0 ..< str. length {
5959 self . chars. insert ( str. character ( at: i) )
6060 }
6161 }
62-
62+
6363 public convenience init ( charsIn str: String , immutable: Bool = false ) {
6464 self . init ( immutable: immutable)
6565 for ch in str. utf16 {
6666 self . chars. insert ( ch)
6767 }
6868 }
69-
69+
7070 public func contains( _ ch: UniChar ) -> Bool {
7171 return self . chars. contains ( ch)
7272 }
73-
73+
7474 public func insert( _ ch: UniChar ) {
7575 self . chars. insert ( ch)
7676 }
77-
77+
7878 public func insert( charsIn str: NSString ) {
7979 for i in 0 ..< str. length {
8080 self . chars. insert ( str. character ( at: i) )
8181 }
8282 }
83-
83+
8484 public func insert( charsIn str: String ) {
8585 for ch in str. utf16 {
8686 self . chars. insert ( ch)
8787 }
8888 }
89-
89+
9090 public func remove( _ ch: UniChar ) {
9191 self . chars. remove ( ch)
9292 }
93-
93+
9494 public func remove( charsIn str: NSString ) {
9595 for i in 0 ..< str. length {
9696 self . chars. remove ( str. character ( at: i) )
9797 }
9898 }
99-
99+
100100 public func remove( charsIn str: String ) {
101101 for ch in str. utf16 {
102102 self . chars. remove ( ch)
103103 }
104104 }
105-
105+
106106 public func formUnion( _ cs: CharSet ) {
107107 self . chars. formUnion ( cs. chars)
108108 }
109-
109+
110110 public func formIntersection( _ cs: CharSet ) {
111111 self . chars. formIntersection ( cs. chars)
112112 }
113-
113+
114114 public func formSymmetricDifference( _ cs: CharSet ) {
115115 self . chars. formSymmetricDifference ( cs. chars)
116116 }
117-
117+
118118 public func subtract( _ cs: CharSet ) {
119119 self . chars. subtract ( cs. chars)
120120 }
121-
121+
122122 public var inverted : CharSet {
123123 let res = CharSet ( )
124124 for ch in 0 ... UInt16 . max {
@@ -128,7 +128,7 @@ public final class CharSet: Reference {
128128 }
129129 return res
130130 }
131-
131+
132132 public func invert( ) {
133133 for ch in 0 ... UInt16 . max {
134134 if self . chars. contains ( ch) {
@@ -138,21 +138,21 @@ public final class CharSet: Reference {
138138 }
139139 }
140140 }
141-
141+
142142 public var isEmpty : Bool {
143143 return self . chars. isEmpty
144144 }
145-
145+
146146 public var count : Int {
147147 return self . chars. count
148148 }
149-
149+
150150 public func forEach( f: ( UniChar ) -> Void ) {
151151 for ch in self . chars {
152152 f ( ch)
153153 }
154154 }
155-
155+
156156 public var array : [ UniChar ] {
157157 var res : [ UniChar ] = [ ]
158158 res. reserveCapacity ( self . chars. count)
@@ -161,7 +161,7 @@ public final class CharSet: Reference {
161161 }
162162 return res
163163 }
164-
164+
165165 public var first : UniChar ? {
166166 for i in 0 ... UInt16 . max {
167167 if self . chars. contains ( i) {
@@ -170,7 +170,7 @@ public final class CharSet: Reference {
170170 }
171171 return nil
172172 }
173-
173+
174174 public func next( _ ch: UniChar ) -> UniChar ? {
175175 if ch == UInt16 . max {
176176 return nil
@@ -182,99 +182,99 @@ public final class CharSet: Reference {
182182 }
183183 return nil
184184 }
185-
185+
186186 public var charSetHashValue : Int {
187187 return self . chars. hashValue
188188 }
189-
189+
190190 public func isEqual( to cs: CharSet ) -> Bool {
191191 return self . chars == cs. chars
192192 }
193-
193+
194194 public func isSubset( of cs: CharSet ) -> Bool {
195195 return self . chars. isSubset ( of: cs. chars)
196196 }
197-
197+
198198 public func isDisjoint( with cs: CharSet ) -> Bool {
199199 return self . chars. isDisjoint ( with: cs. chars)
200200 }
201-
201+
202202 // Definiton of default character sets
203-
203+
204204 public class func lowercaseLetters( ) -> CharSet {
205- return CharSet . mapCharacterSet ( nscs : CharacterSet . lowercaseLetters as NSCharacterSet )
205+ return CharSet . convert ( cs : . lowercaseLetters)
206206 }
207-
207+
208208 public class func uppercaseLetters( ) -> CharSet {
209- return CharSet . mapCharacterSet ( nscs : CharacterSet . uppercaseLetters as NSCharacterSet )
209+ return CharSet . convert ( cs : . uppercaseLetters)
210210 }
211-
211+
212212 public class func titlecaseLetters( ) -> CharSet {
213- return CharSet . mapCharacterSet ( nscs : CharacterSet . capitalizedLetters as NSCharacterSet )
213+ return CharSet . convert ( cs : . capitalizedLetters)
214214 }
215-
215+
216216 public class func letters( ) -> CharSet {
217- return CharSet . mapCharacterSet ( nscs : CharacterSet . letters as NSCharacterSet )
217+ return CharSet . convert ( cs : . letters)
218218 }
219-
219+
220220 public class func digits( ) -> CharSet {
221- return CharSet . mapCharacterSet ( nscs : CharacterSet . decimalDigits as NSCharacterSet )
221+ return CharSet . convert ( cs : . decimalDigits)
222222 }
223-
223+
224224 public class func lettersAndDigits( ) -> CharSet {
225- return CharSet . mapCharacterSet ( nscs : CharacterSet . alphanumerics as NSCharacterSet )
225+ return CharSet . convert ( cs : . alphanumerics)
226226 }
227-
227+
228228 public class func graphics( ) -> CharSet {
229- return CharSet . mapCharacterSet ( nscs : CharacterSet . symbols as NSCharacterSet ,
230- target: CharSet . mapCharacterSet ( nscs : CharacterSet . punctuationCharacters as NSCharacterSet ,
231- target: CharSet . mapCharacterSet ( nscs : CharacterSet . alphanumerics as NSCharacterSet ) ) )
229+ return CharSet . convert ( cs : . symbols,
230+ target: CharSet . convert ( cs : . punctuationCharacters,
231+ target: CharSet . convert ( cs : . alphanumerics) ) )
232232 }
233-
233+
234234 public class func printing( ) -> CharSet {
235- return CharSet . mapCharacterSet ( nscs: CharacterSet . whitespacesAndNewlines as NSCharacterSet ,
236- target: CharSet . graphics ( ) )
235+ return CharSet . convert ( cs: . whitespacesAndNewlines, target: CharSet . graphics ( ) )
237236 }
238-
237+
239238 public class func whitespaces( ) -> CharSet {
240- return CharSet . mapCharacterSet ( nscs : CharacterSet . whitespacesAndNewlines as NSCharacterSet )
239+ return CharSet . convert ( cs : . whitespacesAndNewlines)
241240 }
242-
241+
243242 public class func blanks( ) -> CharSet {
244- return CharSet . mapCharacterSet ( nscs : CharacterSet . whitespaces as NSCharacterSet )
243+ return CharSet . convert ( cs : . whitespaces)
245244 }
246-
245+
247246 public class func controls( ) -> CharSet {
248- return CharSet . mapCharacterSet ( nscs : CharacterSet . controlCharacters as NSCharacterSet )
247+ return CharSet . convert ( cs : . controlCharacters)
249248 }
250-
249+
251250 public class func punctuations( ) -> CharSet {
252- return CharSet . mapCharacterSet ( nscs : CharacterSet . punctuationCharacters as NSCharacterSet )
251+ return CharSet . convert ( cs : . punctuationCharacters)
253252 }
254-
253+
255254 public class func symbols( ) -> CharSet {
256- return CharSet . mapCharacterSet ( nscs : CharacterSet . symbols as NSCharacterSet )
255+ return CharSet . convert ( cs : . symbols)
257256 }
258-
257+
259258 public class func hexdigits( ) -> CharSet {
260259 return CharSet ( charsIn: " 0123456789ABCDEFabcdef " , immutable: true )
261260 }
262-
261+
263262 public class func ascii( ) -> CharSet {
264263 let cs = CharSet ( immutable: true )
265264 for ch : UInt16 in 0 ..< 128 {
266265 cs. insert ( ch)
267266 }
268267 return cs
269268 }
270-
269+
271270 public class func full( ) -> CharSet {
272271 let cs = CharSet ( immutable: true )
273272 cs. invert ( )
274273 return cs
275274 }
276-
277- public class func mapCharacterSet( nscs: NSCharacterSet , target: CharSet ? = nil ) -> CharSet {
275+
276+ private class func convert( cs: CharacterSet , target: CharSet ? = nil ) -> CharSet {
277+ let nscs = cs as NSCharacterSet
278278 let res = target == nil ? CharSet ( immutable: true ) : target!
279279 for ch in 0 ... UInt16 . max {
280280 if nscs. characterIsMember ( ch) {
0 commit comments