Created
September 26, 2019 02:26
-
-
Save atetlaw/848fb90e30e4137fcc4787ee0469c072 to your computer and use it in GitHub Desktop.
List characters in a CharacterSet (Swift 5)
This file contains 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
// From https://stackoverflow.com/questions/34772439/see-characters-in-an-nscharacterset-swift | |
extension NSCharacterSet { | |
var characters: [String] { | |
var chars = [String]() | |
for plane:UInt8 in 0...16 { | |
if self.hasMemberInPlane(plane) { | |
let p0 = UInt32(plane) << 16 | |
let p1 = (UInt32(plane) + 1) << 16 | |
for c:UTF32Char in p0..<p1 { | |
if self.longCharacterIsMember(c) { | |
var c1 = c.littleEndian | |
let s = NSString(bytes: &c1, length: 4, encoding: String.Encoding.utf32LittleEndian.rawValue)! | |
chars.append(String(s)) | |
} | |
} | |
} | |
} | |
return chars | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment