|
1 | 1 | // |
2 | 2 | // ASCIICharacter.swift |
3 | | -// SwiftASCII |
4 | | -// |
5 | | -// Created by Steffan Andrews on 2021-01-29. |
6 | | -// Copyright © 2021 Steffan Andrews. All rights reserved. |
| 3 | +// SwiftASCII • https://github.com/orchetect/SwiftASCII |
7 | 4 | // |
8 | 5 |
|
9 | 6 | import Foundation |
10 | 7 |
|
11 | 8 | /// A type containing a Character instance that is guaranteed to conform to ASCII encoding. |
12 | 9 | public struct ASCIICharacter: Hashable { |
13 | | - |
14 | | - /// The ASCII character returned as a `Character` |
15 | | - public let characterValue: Character |
16 | | - |
17 | | - /// The ASCII encoding value of this character |
18 | | - public let asciiValue: UInt8 |
19 | | - |
20 | | - /// The ASCII character encoded as raw Data |
21 | | - public var rawData: Data { |
22 | | - Data([asciiValue]) |
23 | | - } |
24 | | - |
25 | | - @inlinable public init?(exactly source: Character) { |
26 | | - |
27 | | - guard let getASCIIValue = source.asciiValue else { |
28 | | - return nil |
29 | | - } |
30 | | - |
31 | | - characterValue = source |
32 | | - asciiValue = getASCIIValue |
33 | | - |
34 | | - } |
35 | | - |
36 | | - @inlinable public init(_ lossy: Character) { |
37 | | - |
38 | | - guard let getASCIIValue = lossy.asciiValue else { |
39 | | - // if ASCII encoding fails, fall back to a default character instead of throwing an exception |
40 | | - |
41 | | - var translated = String(lossy).asciiStringLossy |
42 | | - if translated.stringValue.isEmpty { translated = "?" } |
43 | | - |
44 | | - characterValue = Character(translated.stringValue) |
45 | | - asciiValue = characterValue.asciiValue ?? 0x3F |
46 | | - |
47 | | - return |
48 | | - } |
49 | | - |
50 | | - characterValue = lossy |
51 | | - asciiValue = getASCIIValue |
52 | | - |
53 | | - } |
54 | | - |
55 | | - @inlinable public init?(exactly source: String) { |
56 | | - |
57 | | - guard source.count == 1, |
58 | | - let char = source.first |
59 | | - else { return nil } |
60 | | - |
61 | | - guard let getASCIIValue = char.asciiValue else { |
62 | | - return nil |
63 | | - } |
64 | | - |
65 | | - characterValue = char |
66 | | - asciiValue = getASCIIValue |
67 | | - |
68 | | - } |
69 | | - |
70 | | - @inlinable public init(_ lossy: String) { |
71 | | - |
72 | | - let char: Character = lossy.first ?? "?" |
73 | | - |
74 | | - self.init(char) |
75 | | - |
76 | | - } |
77 | | - |
78 | | - @inlinable public init?(exactly source: Data) { |
79 | | - |
80 | | - guard source.count == 1 else { return nil } |
81 | | - |
82 | | - guard let string = String(data: source, encoding: .nonLossyASCII) else { |
83 | | - return nil |
84 | | - } |
85 | | - |
86 | | - guard let scalar = string.unicodeScalars.first else { |
87 | | - return nil |
88 | | - } |
89 | | - |
90 | | - characterValue = Character(scalar) |
91 | | - asciiValue = UInt8(ascii: scalar) |
92 | | - |
93 | | - } |
94 | | - |
95 | | - @inlinable public init?<T: BinaryInteger>(_ asciiValue: T) { |
96 | | - |
97 | | - guard let getASCIIValue = UInt8(exactly: asciiValue) else { return nil } |
98 | | - |
99 | | - self.init(exactly: Data([getASCIIValue])) |
100 | | - |
101 | | - } |
102 | | - |
| 10 | + |
| 11 | + /// The ASCII character returned as a `Character` |
| 12 | + public let characterValue: Character |
| 13 | + |
| 14 | + /// The ASCII encoding value of this character |
| 15 | + public let asciiValue: UInt8 |
| 16 | + |
| 17 | + /// The ASCII character encoded as raw Data |
| 18 | + public var rawData: Data { |
| 19 | + Data([asciiValue]) |
| 20 | + } |
| 21 | + |
| 22 | + @inlinable public init?(exactly source: Character) { |
| 23 | + |
| 24 | + guard let getASCIIValue = source.asciiValue else { |
| 25 | + return nil |
| 26 | + } |
| 27 | + |
| 28 | + characterValue = source |
| 29 | + asciiValue = getASCIIValue |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + @inlinable public init(_ lossy: Character) { |
| 34 | + |
| 35 | + guard let getASCIIValue = lossy.asciiValue else { |
| 36 | + // if ASCII encoding fails, fall back to a default character instead of throwing an exception |
| 37 | + |
| 38 | + var translated = String(lossy).asciiStringLossy |
| 39 | + if translated.stringValue.isEmpty { translated = "?" } |
| 40 | + |
| 41 | + characterValue = Character(translated.stringValue) |
| 42 | + asciiValue = characterValue.asciiValue ?? 0x3F |
| 43 | + |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + characterValue = lossy |
| 48 | + asciiValue = getASCIIValue |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + @inlinable public init?(exactly source: String) { |
| 53 | + |
| 54 | + guard source.count == 1, |
| 55 | + let char = source.first |
| 56 | + else { return nil } |
| 57 | + |
| 58 | + guard let getASCIIValue = char.asciiValue else { |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + characterValue = char |
| 63 | + asciiValue = getASCIIValue |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @inlinable public init(_ lossy: String) { |
| 68 | + |
| 69 | + let char: Character = lossy.first ?? "?" |
| 70 | + |
| 71 | + self.init(char) |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + @inlinable public init?(exactly source: Data) { |
| 76 | + |
| 77 | + guard source.count == 1 else { return nil } |
| 78 | + |
| 79 | + guard let string = String(data: source, encoding: .nonLossyASCII) else { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + guard let scalar = string.unicodeScalars.first else { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + characterValue = Character(scalar) |
| 88 | + asciiValue = UInt8(ascii: scalar) |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @inlinable public init?<T: BinaryInteger>(_ asciiValue: T) { |
| 93 | + |
| 94 | + guard let getASCIIValue = UInt8(exactly: asciiValue) else { return nil } |
| 95 | + |
| 96 | + self.init(exactly: Data([getASCIIValue])) |
| 97 | + |
| 98 | + } |
| 99 | + |
103 | 100 | } |
104 | 101 |
|
105 | 102 | extension ASCIICharacter: ExpressibleByExtendedGraphemeClusterLiteral { |
106 | | - |
107 | | - public typealias ExtendedGraphemeClusterLiteralType = Character |
108 | | - |
109 | | - public init(extendedGraphemeClusterLiteral value: Character) { |
110 | | - |
111 | | - self.init(value) |
112 | | - |
113 | | - } |
114 | | - |
| 103 | + |
| 104 | + public typealias ExtendedGraphemeClusterLiteralType = Character |
| 105 | + |
| 106 | + public init(extendedGraphemeClusterLiteral value: Character) { |
| 107 | + |
| 108 | + self.init(value) |
| 109 | + |
| 110 | + } |
| 111 | + |
115 | 112 | } |
116 | 113 |
|
117 | 114 | extension ASCIICharacter: CustomStringConvertible { |
118 | | - |
119 | | - public var description: String { |
120 | | - |
121 | | - // If not a printable character, return an empty string and don't allow any non-printable ASCII control characters through |
122 | | - |
123 | | - (32...126).contains(asciiValue) |
124 | | - ? String(characterValue) |
125 | | - : "" |
126 | | - |
127 | | - } |
128 | | - |
| 115 | + |
| 116 | + public var description: String { |
| 117 | + |
| 118 | + // If not a printable character, return an empty string and don't allow any non-printable ASCII control characters through |
| 119 | + |
| 120 | + (32...126).contains(asciiValue) |
| 121 | + ? String(characterValue) |
| 122 | + : "" |
| 123 | + |
| 124 | + } |
| 125 | + |
129 | 126 | } |
130 | 127 |
|
131 | 128 | extension ASCIICharacter: CustomDebugStringConvertible { |
132 | | - |
133 | | - public var debugDescription: String { |
134 | | - "ASCIICharacter(#\(asciiValue): \"" + description + "\")" |
135 | | - } |
136 | | - |
| 129 | + |
| 130 | + public var debugDescription: String { |
| 131 | + "ASCIICharacter(#\(asciiValue): \"" + description + "\")" |
| 132 | + } |
| 133 | + |
137 | 134 | } |
138 | 135 |
|
139 | 136 | extension ASCIICharacter: Equatable { |
140 | | - |
141 | | - // Self & Self |
142 | | - |
143 | | - public static func == (lhs: Self, rhs: Self) -> Bool { |
144 | | - lhs.asciiValue == rhs.asciiValue |
145 | | - } |
146 | | - |
147 | | - public static func != (lhs: Self, rhs: Self) -> Bool { |
148 | | - lhs.asciiValue != rhs.asciiValue |
149 | | - } |
150 | | - |
151 | | - // Self, Character |
152 | | - |
153 | | - public static func == (lhs: Self, rhs: Character) -> Bool { |
154 | | - lhs.characterValue == rhs |
155 | | - } |
156 | | - |
157 | | - public static func != (lhs: Self, rhs: Character) -> Bool { |
158 | | - lhs.characterValue != rhs |
159 | | - } |
160 | | - |
161 | | - // Character, Self |
162 | | - |
163 | | - public static func == (lhs: Character, rhs: Self) -> Bool { |
164 | | - lhs == rhs.characterValue |
165 | | - } |
166 | | - |
167 | | - public static func != (lhs: Character, rhs: Self) -> Bool { |
168 | | - lhs != rhs.characterValue |
169 | | - } |
170 | | - |
| 137 | + |
| 138 | + // Self & Self |
| 139 | + |
| 140 | + public static func == (lhs: Self, rhs: Self) -> Bool { |
| 141 | + lhs.asciiValue == rhs.asciiValue |
| 142 | + } |
| 143 | + |
| 144 | + public static func != (lhs: Self, rhs: Self) -> Bool { |
| 145 | + lhs.asciiValue != rhs.asciiValue |
| 146 | + } |
| 147 | + |
| 148 | + // Self, Character |
| 149 | + |
| 150 | + public static func == (lhs: Self, rhs: Character) -> Bool { |
| 151 | + lhs.characterValue == rhs |
| 152 | + } |
| 153 | + |
| 154 | + public static func != (lhs: Self, rhs: Character) -> Bool { |
| 155 | + lhs.characterValue != rhs |
| 156 | + } |
| 157 | + |
| 158 | + // Character, Self |
| 159 | + |
| 160 | + public static func == (lhs: Character, rhs: Self) -> Bool { |
| 161 | + lhs == rhs.characterValue |
| 162 | + } |
| 163 | + |
| 164 | + public static func != (lhs: Character, rhs: Self) -> Bool { |
| 165 | + lhs != rhs.characterValue |
| 166 | + } |
| 167 | + |
171 | 168 | } |
172 | 169 |
|
173 | 170 | extension ASCIICharacter { |
174 | | - |
175 | | - /// Convenience syntactic sugar |
176 | | - public static func exactly(_ source: Character) -> ASCIICharacter? { |
177 | | - Self(exactly: source) |
178 | | - } |
179 | | - |
180 | | - /// Convenience syntactic sugar |
181 | | - public static func lossy(_ source: Character) -> ASCIICharacter { |
182 | | - Self(source) |
183 | | - } |
184 | | - |
185 | | - /// Convenience syntactic sugar |
186 | | - public static func exactly(_ source: String) -> ASCIICharacter? { |
187 | | - Self(exactly: source) |
188 | | - } |
189 | | - |
190 | | - /// Convenience syntactic sugar |
191 | | - public static func lossy(_ source: String) -> ASCIICharacter { |
192 | | - Self(source) |
193 | | - } |
194 | | - |
195 | | - /// Convenience syntactic sugar |
196 | | - public static func exactly(_ source: Data) -> ASCIICharacter? { |
197 | | - Self(exactly: source) |
198 | | - } |
199 | | - |
200 | | - /// Convenience syntactic sugar |
201 | | - public static func exactly<T: BinaryInteger>(_ value: T) -> ASCIICharacter? { |
202 | | - Self(value) |
203 | | - } |
204 | | - |
| 171 | + |
| 172 | + /// Convenience syntactic sugar |
| 173 | + public static func exactly(_ source: Character) -> ASCIICharacter? { |
| 174 | + Self(exactly: source) |
| 175 | + } |
| 176 | + |
| 177 | + /// Convenience syntactic sugar |
| 178 | + public static func lossy(_ source: Character) -> ASCIICharacter { |
| 179 | + Self(source) |
| 180 | + } |
| 181 | + |
| 182 | + /// Convenience syntactic sugar |
| 183 | + public static func exactly(_ source: String) -> ASCIICharacter? { |
| 184 | + Self(exactly: source) |
| 185 | + } |
| 186 | + |
| 187 | + /// Convenience syntactic sugar |
| 188 | + public static func lossy(_ source: String) -> ASCIICharacter { |
| 189 | + Self(source) |
| 190 | + } |
| 191 | + |
| 192 | + /// Convenience syntactic sugar |
| 193 | + public static func exactly(_ source: Data) -> ASCIICharacter? { |
| 194 | + Self(exactly: source) |
| 195 | + } |
| 196 | + |
| 197 | + /// Convenience syntactic sugar |
| 198 | + public static func exactly<T: BinaryInteger>(_ value: T) -> ASCIICharacter? { |
| 199 | + Self(value) |
| 200 | + } |
| 201 | + |
205 | 202 | } |
0 commit comments