Skip to content

Commit 9ea9936

Browse files
authored
Avoid long expression type checks (#657)
These changes remove several seconds of type-checking time from the RegexBuilder test cases, bringing all expressions under 150ms (on the tested computer).
1 parent 923cf5e commit 9ea9936

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

Tests/RegexBuilderTests/AlgorithmsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import RegexBuilder
1616
@available(SwiftStdlib 5.7, *)
1717
class RegexConsumerTests: XCTestCase {
1818
func testMatches() {
19-
let regex = Capture(OneOrMore(.digit)) { 2 * Int($0)! }
19+
let regex = Capture<(Substring, Int)>(OneOrMore(.digit)) { 2 * Int($0)! }
2020
let str = "foo 160 bar 99 baz"
2121
XCTAssertEqual(str.matches(of: regex).map(\.output.1), [320, 198])
2222
}

Tests/RegexBuilderTests/CustomTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private struct IntParser: CustomConsumingRegexComponent {
6464
guard index != bounds.upperBound else { return nil }
6565

6666
let r = Regex {
67-
Capture(OneOrMore(.digit)) { Int($0) }
67+
Capture<(Substring, Int?)>(OneOrMore(.digit)) { Int($0) }
6868
}
6969

7070
guard let match = input[index..<bounds.upperBound].prefixMatch(of: r),
@@ -694,7 +694,7 @@ class CustomRegexComponentTests: XCTestCase {
694694
OneOrMore {
695695
CharacterClass("A"..."Z")
696696
OneOrMore(CharacterClass("a"..."z"))
697-
Capture(Repeat(.digit, count: 2)) { Int($0) }
697+
Capture<(Substring, Int?)>(Repeat(.digit, count: 2)) { Int($0) }
698698
}
699699
}
700700

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ class RegexDSLTests: XCTestCase {
10911091
OneOrMore("a")
10921092
Capture {
10931093
TryCapture("b", transform: { Int($0) })
1094-
ZeroOrMore(
1094+
ZeroOrMore<(Substring, Double?)>(
10951095
TryCapture("c", transform: { Double($0) })
10961096
)
10971097
Optionally("e")
@@ -1542,12 +1542,12 @@ class RegexDSLTests: XCTestCase {
15421542
in bounds: Range<String.Index>
15431543
) throws -> (upperBound: String.Index, output: SemanticVersion)? {
15441544
let regex = Regex {
1545-
TryCapture(OneOrMore(.digit)) { Int($0) }
1545+
TryCapture<(Substring, Int)>(OneOrMore(.digit)) { Int($0) }
15461546
"."
1547-
TryCapture(OneOrMore(.digit)) { Int($0) }
1547+
TryCapture<(Substring, Int)>(OneOrMore(.digit)) { Int($0) }
15481548
Optionally {
15491549
"."
1550-
TryCapture(OneOrMore(.digit)) { Int($0) }
1550+
TryCapture<(Substring, Int)>(OneOrMore(.digit)) { Int($0) }
15511551
}
15521552
Optionally {
15531553
"-"
@@ -1876,7 +1876,7 @@ extension RegexDSLTests {
18761876
":"
18771877
regexWithTooManyCaptures
18781878
":"
1879-
TryCapture(OneOrMore(.word)) { Int($0) }
1879+
TryCapture<(Substring, Int)>(OneOrMore(.word)) { Int($0) }
18801880
#/:(\d+):/#
18811881
}
18821882
XCTAssert(type(of: dslWithTooManyCaptures).self

0 commit comments

Comments
 (0)