Skip to content

Commit e37cf13

Browse files
authored
Merge pull request #534 from hamishknight/more-5.7-cherry-picks
2 parents 857b6f3 + 4b5d1f3 commit e37cf13

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

Sources/RegexBuilder/Algorithms.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
@_spi(RegexBuilder) import _StringProcessing
1313

14-
// FIXME(rdar://92459215): We should be using 'some RegexComponent' instead of
15-
// <R: RegexComponent> for the methods below that don't impose any additional
16-
// requirements on the type. Currently the generic parameter is needed to work
17-
// around a compiler issue.
18-
1914
extension BidirectionalCollection where SubSequence == Substring {
2015
/// Matches a regex in its entirety, where the regex is created by
2116
/// the given closure.

Sources/RegexBuilder/Builder.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public enum RegexComponentBuilder {
1818
_RegexFactory().empty()
1919
}
2020

21-
public static func buildPartialBlock<R: RegexComponent>(first: R ) -> R {
22-
first
21+
public static func buildPartialBlock<R: RegexComponent>(
22+
first component: R
23+
) -> Regex<R.RegexOutput> {
24+
component.regex
2325
}
2426

2527
public static func buildExpression<R: RegexComponent>(_ regex: R) -> R {

Sources/_StringProcessing/Regex/Match.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ extension BidirectionalCollection where SubSequence == Substring {
164164
public func wholeMatch<R: RegexComponent>(
165165
of r: R
166166
) -> Regex<R.RegexOutput>.Match? {
167-
try? r.regex.wholeMatch(in: self[...].base)
167+
try? r.regex.wholeMatch(in: self[...])
168168
}
169169

170170
/// Checks for a match against the string, starting at its beginning.

Tests/RegexBuilderTests/AlgorithmsTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ class AlgorithmsResultBuilderTests: XCTestCase {
170170
}
171171

172172
func testMatches() throws {
173+
do {
174+
let regex = Regex { OneOrMore(.any) }
175+
XCTAssertEqual("abc".wholeMatch(of: regex)!.0, "abc")
176+
XCTAssertEqual("abc".prefixMatch(of: regex)!.0, "abc")
177+
XCTAssertEqual("abc".firstMatch(of: regex)!.0, "abc")
178+
XCTAssertEqual("abc".suffix(1).wholeMatch(of: regex)!.0, "c")
179+
XCTAssertEqual("abc".suffix(1).prefixMatch(of: regex)!.0, "c")
180+
XCTAssertEqual("abc".suffix(1).firstMatch(of: regex)!.0, "c")
181+
}
182+
173183
let int = Capture(OneOrMore(.digit)) { Int($0)! }
174184

175185
// Test syntax

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ class RegexDSLTests: XCTestCase {
10521052
XCTAssertEqual(str.wholeMatch(of: parser)?.1, version)
10531053
}
10541054
}
1055-
1055+
10561056
func testZeroWidthConsumer() throws {
10571057
struct Trace: CustomConsumingRegexComponent {
10581058
typealias RegexOutput = Void
@@ -1088,6 +1088,26 @@ class RegexDSLTests: XCTestCase {
10881088
10891089
""")
10901090
}
1091+
1092+
func testRegexComponentBuilderResultType() {
1093+
// Test that the user can declare a closure or computed property marked with
1094+
// `@RegexComponentBuilder` with `Regex` as the result type.
1095+
@RegexComponentBuilder
1096+
var unaryWithSingleNonRegex: Regex<Substring> {
1097+
OneOrMore("a")
1098+
}
1099+
@RegexComponentBuilder
1100+
var multiComponent: Regex<Substring> {
1101+
OneOrMore("a")
1102+
"b"
1103+
}
1104+
struct MyCustomRegex: RegexComponent {
1105+
@RegexComponentBuilder
1106+
var regex: Regex<Substring> {
1107+
OneOrMore("a")
1108+
}
1109+
}
1110+
}
10911111
}
10921112

10931113
extension Unicode.Scalar {

0 commit comments

Comments
 (0)