Skip to content

Commit 81efd57

Browse files
committed
Replaced String parameters and extensions with StringProtocol
1 parent e36bede commit 81efd57

File tree

7 files changed

+190
-292
lines changed

7 files changed

+190
-292
lines changed

Sources/SwiftASCII/ASCIICharacter.swift

+20-61
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Foundation
99
/// A type containing a `Character` instance that is guaranteed to conform to ASCII encoding.
1010
/// Offers a validating `exactly: Character` failable initializer and a `_ lossy: Character` conversion initializer.
1111
public struct ASCIICharacter: Hashable {
12-
1312
/// The ASCII character returned as a `Character`
1413
public let characterValue: Character
1514

@@ -18,28 +17,23 @@ public struct ASCIICharacter: Hashable {
1817

1918
/// The ASCII character encoded as raw Data
2019
public var rawData: Data {
21-
2220
Data([asciiValue])
23-
2421
}
2522

2623
/// Returns a new `ASCIICharacter` instance if the source character is a valid ASCII character.
2724
@inlinable
2825
public init?(exactly source: Character) {
29-
3026
guard let getASCIIValue = source.asciiValue else {
3127
return nil
3228
}
3329

3430
self.characterValue = source
3531
self.asciiValue = getASCIIValue
36-
3732
}
3833

3934
/// Returns a new `ASCIICharacter` instance from the source character, converting a non-ASCII character to its closest ASCII equivalent if necessary.
4035
@inlinable
4136
public init(_ lossy: Character) {
42-
4337
guard let getASCIIValue = lossy.asciiValue else {
4438
// if ASCII encoding fails, fall back to a default character instead of throwing an exception
4539

@@ -54,14 +48,12 @@ public struct ASCIICharacter: Hashable {
5448

5549
self.characterValue = lossy
5650
self.asciiValue = getASCIIValue
57-
5851
}
5952

6053
/// Returns a new `ASCIICharacter` instance if the source string contains a single character and the character is a valid ASCII character.
6154
@_disfavoredOverload
6255
@inlinable
63-
public init?(exactly source: String) {
64-
56+
public init?<S: StringProtocol>(exactly source: S) {
6557
guard source.count == 1,
6658
let char = source.first
6759
else { return nil }
@@ -72,24 +64,20 @@ public struct ASCIICharacter: Hashable {
7264

7365
self.characterValue = char
7466
self.asciiValue = getASCIIValue
75-
7667
}
7768

7869
/// 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.
7970
@inlinable
80-
public init(_ lossy: String) {
81-
71+
public init<S: StringProtocol>(_ lossy: S) {
8272
let char: Character = lossy.first ?? "?"
8373

8474
self.init(char)
85-
8675
}
8776

8877
/// Returns a new `ASCIICharacter` instance if the source data is a single ASCII character.
8978
/// Returns `nil` if the source data is not a single byte or if it contains a non-ASCII character byte.
9079
@inlinable
9180
public init?(exactly source: Data) {
92-
9381
guard source.count == 1 else { return nil }
9482

9583
guard let string = String(data: source, encoding: .nonLossyASCII) else {
@@ -102,58 +90,43 @@ public struct ASCIICharacter: Hashable {
10290

10391
self.characterValue = Character(scalar)
10492
self.asciiValue = UInt8(ascii: scalar)
105-
10693
}
10794

10895
/// Returns a new `ASCIICharacter` instance from an ASCII character number.
10996
/// Returns `nil` if the number is not within the valid ASCII table (0..<128).
11097
@inlinable
11198
public init?<T: BinaryInteger>(_ asciiValue: T) {
112-
11399
guard let getASCIIValue = UInt8(exactly: asciiValue) else { return nil }
114100

115101
self.init(exactly: Data([getASCIIValue]))
116-
117102
}
118-
119103
}
120104

121105
extension ASCIICharacter: ExpressibleByExtendedGraphemeClusterLiteral {
122-
123106
public typealias ExtendedGraphemeClusterLiteralType = Character
124107

125108
public init(extendedGraphemeClusterLiteral value: Character) {
126-
127109
self.init(value)
128-
129110
}
130-
131111
}
132112

133113
extension ASCIICharacter: CustomStringConvertible {
134-
135114
public var description: String {
136-
137115
// If not a printable character, return an empty string and don't allow any non-printable ASCII control characters through
138116

139-
(32...126).contains(asciiValue)
117+
(32 ... 126).contains(asciiValue)
140118
? String(characterValue)
141-
: ""
142-
119+
: "?"
143120
}
144-
145121
}
146122

147123
extension ASCIICharacter: CustomDebugStringConvertible {
148-
149124
public var debugDescription: String {
150125
"ASCIICharacter(#\(asciiValue): \"" + description + "\")"
151126
}
152-
153127
}
154128

155129
extension ASCIICharacter: Equatable {
156-
157130
// Self & Self
158131

159132
public static func == (lhs: Self, rhs: Self) -> Bool {
@@ -183,59 +156,45 @@ extension ASCIICharacter: Equatable {
183156
public static func != (lhs: Character, rhs: Self) -> Bool {
184157
lhs != rhs.characterValue
185158
}
186-
187159
}
188160

189161
extension ASCIICharacter: Codable {
190-
191162
public init(from decoder: Decoder) throws {
192-
193163
let container = try decoder.singleValueContainer()
194164
let string = try container.decode(String.self)
195165
guard let newInstance = Self(exactly: string) else {
196166
throw DecodingError.dataCorrupted(
197-
.init(codingPath: container.codingPath,
198-
debugDescription: "Value was not valid ASCII character number.")
167+
.init(
168+
codingPath: container.codingPath,
169+
debugDescription: "Value was not valid ASCII character number."
170+
)
199171
)
200172
}
201173
self = newInstance
202-
203174
}
204175

205176
public func encode(to encoder: Encoder) throws {
206-
207177
var container = encoder.singleValueContainer()
208178

209179
try container.encode(String(characterValue))
210-
211180
}
212-
213181
}
214182

215183
extension ASCIICharacter {
216-
217184
public static func + (lhs: ASCIICharacter, rhs: ASCIICharacter) -> ASCIIString {
218-
219185
ASCIIString([lhs, rhs])
220-
221186
}
222187

223188
public static func + (lhs: ASCIICharacter, rhs: ASCIIString) -> ASCIIString {
224-
225189
ASCIIString(lhs) + rhs
226-
227190
}
228191

229192
public static func + (lhs: ASCIIString, rhs: ASCIICharacter) -> ASCIIString {
230-
231193
lhs + ASCIIString(rhs)
232-
233194
}
234-
235195
}
236196

237197
extension ASCIICharacter {
238-
239198
/// Convenience: initialize a `ASCIICharacter` instance.
240199
public static func exactly(_ source: Character) -> ASCIICharacter? {
241200
Self(exactly: source)
@@ -247,12 +206,12 @@ extension ASCIICharacter {
247206
}
248207

249208
/// Convenience: initialize a `ASCIICharacter` instance.
250-
public static func exactly(_ source: String) -> ASCIICharacter? {
209+
public static func exactly<S: StringProtocol>(_ source: S) -> ASCIICharacter? {
251210
Self(exactly: source)
252211
}
253212

254213
/// Convenience: initialize a `ASCIICharacter` instance.
255-
public static func lossy(_ source: String) -> ASCIICharacter {
214+
public static func lossy<S: StringProtocol>(_ source: S) -> ASCIICharacter {
256215
Self(source)
257216
}
258217

@@ -265,25 +224,25 @@ extension ASCIICharacter {
265224
public static func exactly<T: BinaryInteger>(_ value: T) -> ASCIICharacter? {
266225
Self(value)
267226
}
268-
269227
}
270228

271229
extension Sequence where Element == ASCIICharacter {
272-
273230
/// Returns a new string by concatenating the elements of the sequence.
274231
public func joined() -> ASCIIString {
275-
276232
ASCIIString(self)
277-
278233
}
279234

280235
/// Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
281236
public func joined(separator: ASCIIString) -> ASCIIString {
282-
283-
let joinedStr = map { "\($0.characterValue)" }.joined(separator: separator.stringValue)
284-
let joinedData = Data(map { $0.rawData }.joined(separator: separator.rawData))
285-
return ASCIIString(guaranteedASCII: joinedStr, rawData: joinedData)
286-
237+
let joinedStr = map { "\($0.characterValue)" }
238+
.joined(separator: separator.stringValue)
239+
let joinedData = Data(
240+
map { $0.rawData }
241+
.joined(separator: separator.rawData)
242+
)
243+
return ASCIIString(
244+
guaranteedASCII: joinedStr,
245+
rawData: joinedData
246+
)
287247
}
288-
289248
}

0 commit comments

Comments
 (0)