Skip to content

Commit 59a1272

Browse files
committed
[XcodeGen] Pattern match by KeyPath<T, Bool>
Simpler pattern match by `is` properties. E.g. `\.isNewline`
1 parent 2d64ef2 commit 59a1272

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
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/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: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ extension Byte: Comparable {
2929
}
3030
}
3131

32-
extension Byte? {
33-
var isSpaceOrTab: Bool {
34-
self?.isSpaceOrTab == true
35-
}
36-
var isNewline: Bool {
37-
self?.isNewline == true
38-
}
39-
var isSpaceTabOrNewline: Bool {
40-
self?.isSpaceTabOrNewline == true
41-
}
42-
}
43-
4432
extension Byte {
4533
var isSpaceOrTab: Bool {
4634
self == " " || self == "\t"

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)