@keehun/

CharacterSet Article Demo

Swift

No description

fork
loading
Files
  • main.swift
  • main

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8382748251826733","path":"main.swift","file":{"path":"main.swift","content":{"asEncoding":{"base64":"aW1wb3J0IEZvdW5kYXRpb24KCgoKLy8gTUFSSzogLSBTbmlwcGV0IDEKCmxldCBzdHJpbmdUb0NoZWNrMTogU3RyaW5nID0gIjQyIgpsZXQgbnVtYmVyczE6IENoYXJhY3RlclNldCA9IENoYXJhY3RlclNldC5kZWNpbWFsRGlnaXRzCmxldCBzdHJpbmdJc0FOdW1iZXIxOiBCb29sID0gc3RyaW5nVG9DaGVjazEucmFuZ2VPZkNoYXJhY3Rlcihmcm9tOiBudW1iZXJzMS5pbnZlcnRlZCkgPT0gbmlsCi8vLyByZXR1cm5zOiBgdHJ1ZWAKCgoKLy8gTUFSSzogLSBTbmlwcGV0IDIKCnByaW50KENoYXJhY3RlclNldC5kZWNpbWFsRGlnaXRzKQovLy8gPENGQ2hhcmFjdGVyU2V0IFByZWRlZmluZWQgRGVjaW1hbERpZ2l0IFNldD4KCgoKLy8gTUFSSzogLSBTbmlwcGV0IDMKCmxldCBudW1iZXJzMzogTlNDaGFyYWN0ZXJTZXQgPSBOU0NoYXJhY3RlclNldC5kZWNpbWFsRGlnaXRzIGFzIE5TQ2hhcmFjdGVyU2V0CmxldCBjaGFyYWN0ZXIzOiBVSW50MzIgPSBVSW50MzIoNDgpIC8vLyBVKzAwNDggaXMgIjAiCm51bWJlcnMzLmxvbmdDaGFyYWN0ZXJJc01lbWJlcihjaGFyYWN0ZXIzKSAvLy8gcmV0dXJucyBgdHJ1ZWAKCgoKLy8gTUFSSzogLSBTbmlwcGV0IDQKCmZ1bmMgaW52YWxpZENoYXJhY3RlcnNFeGlzdDQoaW5wdXQ0OiBTdHJpbmcpIC0+IEJvb2wgewogICAgbGV0IG51bWJlcnM0ID0gQ2hhcmFjdGVyU2V0LmRlY2ltYWxEaWdpd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AgICAgICAgICAgICAgICAgICAgIGNoYXJhY3RlcnMuYXBwZW5kKFN0cmluZyh1bmljb2RlQ2hhcmFjdGVyKSkKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gY2hhcmFjdGVycwogICAgfQp9CgpsZXQgbnVtYmVyczY6IE5TQ2hhcmFjdGVyU2V0ID0gTlNDaGFyYWN0ZXJTZXQuZGVjaW1hbERpZ2l0cyBhcyBOU0NoYXJhY3RlclNldApmb3IgY2hhcmFjdGVyIGluIG51bWJlcnM2LmNoYXJhY3RlcnMgewogICAgcHJpbnQoY2hhcmFjdGVyKQp9Cg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import Foundation



// MARK: - Snippet 1

let stringToCheck1: String = "42"
let numbers1: CharacterSet = CharacterSet.decimalDigits
let stringIsANumber1: Bool = stringToCheck1.rangeOfCharacter(from: numbers1.inverted) == nil
/// returns: `true`



// MARK: - Snippet 2

print(CharacterSet.decimalDigits)
/// <CFCharacterSet Predefined DecimalDigit Set>



// MARK: - Snippet 3

let numbers3: NSCharacterSet = NSCharacterSet.decimalDigits as NSCharacterSet
let character3: UInt32 = UInt32(48) /// U+0048 is "0"
numbers3.longCharacterIsMember(character3) /// returns `true`



// MARK: - Snippet 4

func invalidCharactersExist4(input4: String) -> Bool {
    let numbers4 = CharacterSet.decimalDigits
    let rangeOfInvalidCharacters4 = input4.rangeOfCharacter(from: numbers4.inverted)
    return rangeOfInvalidCharacters4 != nil
}

let string1 = "qwerty12345"
let string2 = "qwerty"
let string3 = "12345"

print("\"\(string1)\" contains invalid characters: \(invalidCharactersExist4(input4: string1))")
/// "qwerty12345" contains invalid characters: true

print("\"\(string2)\" contains invalid characters: \(invalidCharactersExist4(input4: string2))")
/// "qwerty" contains invalid characters: true

print("\"\(string3)\" contains invalid characters: \(invalidCharactersExist4(input4: string3))")
/// "12345" contains invalid characters: false



// MARK: - Snippet 5

let string4 = "᧐᪂᧐"
print("\"\(string4)\" contains invalid characters: \(invalidCharactersExist4(input4: string4))")
/// "᧐᪂᧐" contains invalid characters: false



// MARK: - Snippet 6

extension NSCharacterSet {

    var characters:[String] {
        /// An array to hold all the found characters
        var characters: [String] = []

        /// Iterate over the 17 Unicode planes (0..16)
        for plane:UInt8 in 0..<17 {
            /// Iterating over all potential code points of each plane could be expensive as there
            /// can be as many as 2^16 code points per plane. Therefore, only search through a plane
            /// that has a character within the set.
            if self.hasMemberInPlane(plane) {

                /// Define the lower end of the plane (i.e. U+FFFF for beginning of Plane 0)
                let planeStart = UInt32(plane) << 16
                /// Define the lower end of the next plane (i.e. U+1FFFF for beginning of Plane 1)
                let nextPlaneStart = (UInt32(plane) + 1) << 16

                /// Iterate over all possible UTF32 characters from the beginning of the current
                /// plane until the next plane.
                for char:UInt32 in planeStart..<nextPlaneStart {

                    /// Test if the character being iterated over is part of this `NSCharacterSet`
                    if self.longCharacterIsMember(char) {

                        /// Convert `UTF32Char` (a typealiased `UInt32`) into a `UnicodeScalar`.
                        /// Otherwise, converting `UTF32Char` directly to `String` would turn it
                        /// into a decimal representation of the code point, not the character.
                        if let unicodeCharacter = UnicodeScalar(char) {
                            characters.append(String(unicodeCharacter))
                        }
                    }
                }
            }
        }
        return characters
    }
}

let numbers6: NSCharacterSet = NSCharacterSet.decimalDigits as NSCharacterSet
for character in numbers6.characters {
    print(character)
}