Last active
April 26, 2021 16:15
-
-
Save sujinnaljin/90534e522960de142f756ea8de0a3ef5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //https://stackoverflow.com/questions/15741631/nsarray-from-nscharacterset/15742659#15742659 | |
| extension CharacterSet { | |
| func characters() -> [Character] { | |
| // A Unicode scalar is any Unicode code point in the range U+0000 to U+D7FF inclusive or U+E000 to U+10FFFF inclusive. | |
| return codePoints().compactMap { UnicodeScalar($0) }.map { Character($0) } | |
| } | |
| func codePoints() -> [Int] { | |
| var result: [Int] = [] | |
| var plane = 0 | |
| // following documentation at https://developer.apple.com/documentation/foundation/nscharacterset/1417719-bitmaprepresentation | |
| for (i, w) in bitmapRepresentation.enumerated() { | |
| let k = i % 0x2001 | |
| if k == 0x2000 { | |
| // plane index byte | |
| plane = Int(w) << 13 | |
| continue | |
| } | |
| let base = (plane + k) << 3 | |
| for j in 0 ..< 8 where w & 1 << j != 0 { | |
| result.append(base + j) | |
| } | |
| } | |
| return result | |
| } | |
| } | |
| let encodingOptions: [CharacterSet] = [.urlUserAllowed, | |
| .urlPasswordAllowed, | |
| .urlFragmentAllowed, | |
| .urlQueryAllowed, | |
| .urlPathAllowed, | |
| .urlHostAllowed] | |
| encodingOptions.forEach { (encodingOption) in | |
| let chars = | |
| encodingOption.characters() | |
| .filter { !$0.isLetter } | |
| .filter { !$0.isNumber } | |
| print(chars.count) // 1733 | |
| print(chars) // ["A", "B", "C", ... "] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment