Skip to content

Commit 2d64ef2

Browse files
committed
[XcodeGen] Make 'Byte' comform 'ExpressibleByUnicodeScalarLiteral'
Instead of comparing with 'UnicodeScalar', construct 'Byte' from literals and compare with them. Make it less evil :)
1 parent aa0e3dc commit 2d64ef2

File tree

4 files changed

+10
-34
lines changed

4 files changed

+10
-34
lines changed

utils/swift-xcodegen/Sources/SwiftXcodeGen/Ninja/NinjaBuildFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension NinjaBuildFile {
154154

155155
extension Byte {
156156
fileprivate var isNinjaVarName: Bool {
157-
switch self.scalar {
157+
switch self {
158158
case "0"..."9", "a"..."z", "A"..."Z", "_", "-":
159159
return true
160160
default:

utils/swift-xcodegen/Sources/SwiftXcodeGen/Scanner/Byte.swift

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,16 @@ struct Byte: Hashable {
1717
}
1818
}
1919

20-
// Please forgive me...
21-
func == (lhs: UnicodeScalar, rhs: Byte?) -> Bool {
22-
guard let rhs else { return false }
23-
return lhs.value == rhs.rawValue
24-
}
25-
func == (lhs: Byte?, rhs: UnicodeScalar) -> Bool {
26-
rhs == lhs
27-
}
28-
func != (lhs: UnicodeScalar, rhs: Byte?) -> Bool {
29-
!(lhs == rhs)
30-
}
31-
func != (lhs: Byte?, rhs: UnicodeScalar) -> Bool {
32-
rhs != lhs
20+
extension Byte: ExpressibleByUnicodeScalarLiteral {
21+
init(unicodeScalarLiteral value: UnicodeScalar) {
22+
self.init(UInt8(ascii: value))
23+
}
3324
}
3425

35-
func ~= (pattern: UnicodeScalar, match: Byte) -> Bool {
36-
pattern == match
37-
}
38-
func ~= (pattern: UnicodeScalar, match: Byte?) -> Bool {
39-
pattern == match
26+
extension Byte: Comparable {
27+
static func < (lhs: Self, rhs: Self) -> Bool {
28+
lhs.rawValue < rhs.rawValue
29+
}
4030
}
4131

4232
extension Byte? {
@@ -61,14 +51,4 @@ extension Byte {
6151
var isSpaceTabOrNewline: Bool {
6252
isSpaceOrTab || isNewline
6353
}
64-
init(ascii scalar: UnicodeScalar) {
65-
assert(scalar.isASCII)
66-
self.rawValue = UInt8(scalar.value)
67-
}
68-
var scalar: UnicodeScalar {
69-
UnicodeScalar(UInt32(rawValue))!
70-
}
71-
var char: Character {
72-
.init(scalar)
73-
}
7454
}

utils/swift-xcodegen/Sources/SwiftXcodeGen/Scanner/ByteScanner.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ struct ByteScanner {
7777
tryEat(where: { $0 == byte })
7878
}
7979

80-
mutating func tryEat(_ c: UnicodeScalar) -> Bool {
81-
tryEat(where: { $0 == c })
82-
}
83-
8480
mutating func tryEat<S: Sequence>(_ seq: S) -> Bool where S.Element == UInt8 {
8581
let start = cursor
8682
for byte in seq {

utils/swift-xcodegen/Sources/SwiftXcodeGen/Utils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension String {
7878
let result = scanner.consumeWhole { consumer in
7979
switch consumer.peek {
8080
case "\\", "\"":
81-
consumer.append(Byte(ascii: "\\"))
81+
consumer.append("\\")
8282
case " ", "$": // $ is potentially a variable reference
8383
needsQuotes = true
8484
default:

0 commit comments

Comments
 (0)