You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/SwiftASCII/ASCIICharacter.swift
+20-61
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,6 @@ import Foundation
9
9
/// A type containing a `Character` instance that is guaranteed to conform to ASCII encoding.
10
10
/// Offers a validating `exactly: Character` failable initializer and a `_ lossy: Character` conversion initializer.
11
11
publicstructASCIICharacter:Hashable{
12
-
13
12
/// The ASCII character returned as a `Character`
14
13
publicletcharacterValue:Character
15
14
@@ -18,28 +17,23 @@ public struct ASCIICharacter: Hashable {
18
17
19
18
/// The ASCII character encoded as raw Data
20
19
publicvarrawData:Data{
21
-
22
20
Data([asciiValue])
23
-
24
21
}
25
22
26
23
/// Returns a new `ASCIICharacter` instance if the source character is a valid ASCII character.
27
24
@inlinable
28
25
publicinit?(exactly source:Character){
29
-
30
26
guardlet getASCIIValue = source.asciiValue else{
31
27
returnnil
32
28
}
33
29
34
30
self.characterValue = source
35
31
self.asciiValue = getASCIIValue
36
-
37
32
}
38
33
39
34
/// Returns a new `ASCIICharacter` instance from the source character, converting a non-ASCII character to its closest ASCII equivalent if necessary.
40
35
@inlinable
41
36
publicinit(_ lossy:Character){
42
-
43
37
guardlet getASCIIValue = lossy.asciiValue else{
44
38
// if ASCII encoding fails, fall back to a default character instead of throwing an exception
45
39
@@ -54,14 +48,12 @@ public struct ASCIICharacter: Hashable {
54
48
55
49
self.characterValue = lossy
56
50
self.asciiValue = getASCIIValue
57
-
58
51
}
59
52
60
53
/// Returns a new `ASCIICharacter` instance if the source string contains a single character and the character is a valid ASCII character.
61
54
@_disfavoredOverload
62
55
@inlinable
63
-
publicinit?(exactly source:String){
64
-
56
+
publicinit?<S:StringProtocol>(exactly source:S){
65
57
guard source.count ==1,
66
58
let char = source.first
67
59
else{returnnil}
@@ -72,24 +64,20 @@ public struct ASCIICharacter: Hashable {
72
64
73
65
self.characterValue = char
74
66
self.asciiValue = getASCIIValue
75
-
76
67
}
77
68
78
69
/// Returns a new `ASCIICharacter` instance if the source string contains a single character, converting a non-ASCII character to its closest ASCII equivalent if necessary.
79
70
@inlinable
80
-
publicinit(_ lossy:String){
81
-
71
+
publicinit<S:StringProtocol>(_ lossy:S){
82
72
letchar:Character= lossy.first ??"?"
83
73
84
74
self.init(char)
85
-
86
75
}
87
76
88
77
/// Returns a new `ASCIICharacter` instance if the source data is a single ASCII character.
89
78
/// Returns `nil` if the source data is not a single byte or if it contains a non-ASCII character byte.
0 commit comments