Skip to content

Commit 136cdee

Browse files
authored
Merge pull request #79000 from rintaro/xcodegen-byte
[XcodeGen] 'Byte' comparison and pattern matching
2 parents 4016e25 + 59a1272 commit 136cdee

File tree

6 files changed

+22
-51
lines changed

6 files changed

+22
-51
lines changed

utils/swift-xcodegen/Sources/SwiftXcodeGen/Command/CommandParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fileprivate extension ByteScanner {
130130
// Consume the element, stopping at the first space.
131131
return try consume(using: { consumer in
132132
switch consumer.peek {
133-
case let c where c.isSpaceOrTab:
133+
case \.isSpaceOrTab:
134134
return false
135135
case "\"":
136136
try consumer.consumeStringLiteral()

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/Ninja/NinjaParser.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ fileprivate extension ByteScanner {
4646
// Ninja uses '$' as the escape character.
4747
if c == "$" {
4848
switch consumer.peek(ahead: 1) {
49-
case let c? where c.isSpaceOrTab:
50-
fallthrough
51-
case "$", ":":
49+
case "$", ":", \.isSpaceOrTab:
5250
// Skip the '$' and take the unescaped character.
5351
consumer.skip()
5452
return consumer.eat()
55-
case let c? where c.isNewline:
53+
case \.isNewline:
5654
// This is a line continuation, skip the newline, and strip any
5755
// following space.
5856
consumer.skip(untilAfter: \.isNewline)
@@ -160,7 +158,7 @@ extension NinjaParser.Lexer {
160158
private mutating func consumeElement() -> String? {
161159
input.consumeUnescaped(while: { char in
162160
switch char {
163-
case let c where c.isNinjaOperator || c.isSpaceTabOrNewline:
161+
case \.isNinjaOperator, \.isSpaceTabOrNewline:
164162
false
165163
default:
166164
true

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

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,15 @@ 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
33-
}
34-
35-
func ~= (pattern: UnicodeScalar, match: Byte) -> Bool {
36-
pattern == match
37-
}
38-
func ~= (pattern: UnicodeScalar, match: Byte?) -> Bool {
39-
pattern == match
20+
extension Byte: ExpressibleByUnicodeScalarLiteral {
21+
init(unicodeScalarLiteral value: UnicodeScalar) {
22+
self.init(UInt8(ascii: value))
23+
}
4024
}
4125

42-
extension Byte? {
43-
var isSpaceOrTab: Bool {
44-
self?.isSpaceOrTab == true
45-
}
46-
var isNewline: Bool {
47-
self?.isNewline == true
48-
}
49-
var isSpaceTabOrNewline: Bool {
50-
self?.isSpaceTabOrNewline == true
26+
extension Byte: Comparable {
27+
static func < (lhs: Self, rhs: Self) -> Bool {
28+
lhs.rawValue < rhs.rawValue
5129
}
5230
}
5331

@@ -61,14 +39,4 @@ extension Byte {
6139
var isSpaceTabOrNewline: Bool {
6240
isSpaceOrTab || isNewline
6341
}
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-
}
7442
}

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: 10 additions & 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:
@@ -132,3 +132,12 @@ extension String {
132132
}
133133
}
134134
}
135+
136+
/// Pattern match by `is` property. E.g. `case \.isNewline: ...`
137+
func ~= <T>(keyPath: KeyPath<T, Bool>, subject: T) -> Bool {
138+
return subject[keyPath: keyPath]
139+
}
140+
141+
func ~= <T>(keyPath: KeyPath<T, Bool>, subject: T?) -> Bool {
142+
return subject?[keyPath: keyPath] == true
143+
}

0 commit comments

Comments
 (0)