Skip to content

Instantly share code, notes, and snippets.

@atetlaw
Created September 26, 2019 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atetlaw/848fb90e30e4137fcc4787ee0469c072 to your computer and use it in GitHub Desktop.
Save atetlaw/848fb90e30e4137fcc4787ee0469c072 to your computer and use it in GitHub Desktop.
List characters in a CharacterSet (Swift 5)
// 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