File tree 5 files changed +36
-9
lines changed
5 files changed +36
-9
lines changed Original file line number Diff line number Diff line change 11
11
12
12
@_spi ( RegexBuilder) import _StringProcessing
13
13
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
-
19
14
extension BidirectionalCollection where SubSequence == Substring {
20
15
/// Matches a regex in its entirety, where the regex is created by
21
16
/// the given closure.
Original file line number Diff line number Diff line change @@ -18,8 +18,10 @@ public enum RegexComponentBuilder {
18
18
_RegexFactory ( ) . empty ( )
19
19
}
20
20
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
23
25
}
24
26
25
27
public static func buildExpression< R: RegexComponent > ( _ regex: R ) -> R {
Original file line number Diff line number Diff line change @@ -164,7 +164,7 @@ extension BidirectionalCollection where SubSequence == Substring {
164
164
public func wholeMatch< R: RegexComponent > (
165
165
of r: R
166
166
) -> Regex < R . RegexOutput > . Match ? {
167
- try ? r. regex. wholeMatch ( in: self [ ... ] . base )
167
+ try ? r. regex. wholeMatch ( in: self [ ... ] )
168
168
}
169
169
170
170
/// Checks for a match against the string, starting at its beginning.
Original file line number Diff line number Diff line change @@ -170,6 +170,16 @@ class AlgorithmsResultBuilderTests: XCTestCase {
170
170
}
171
171
172
172
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
+
173
183
let int = Capture ( OneOrMore ( . digit) ) { Int ( $0) ! }
174
184
175
185
// Test syntax
Original file line number Diff line number Diff line change @@ -1052,7 +1052,7 @@ class RegexDSLTests: XCTestCase {
1052
1052
XCTAssertEqual ( str. wholeMatch ( of: parser) ? . 1 , version)
1053
1053
}
1054
1054
}
1055
-
1055
+
1056
1056
func testZeroWidthConsumer( ) throws {
1057
1057
struct Trace : CustomConsumingRegexComponent {
1058
1058
typealias RegexOutput = Void
@@ -1088,6 +1088,26 @@ class RegexDSLTests: XCTestCase {
1088
1088
1089
1089
""" )
1090
1090
}
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
+ }
1091
1111
}
1092
1112
1093
1113
extension Unicode . Scalar {
You can’t perform that action at this time.
0 commit comments