File tree Expand file tree Collapse file tree 5 files changed +36
-9
lines changed Expand file tree Collapse file tree 5 files changed +36
-9
lines changed Original file line number Diff line number Diff line change 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-
1914extension BidirectionalCollection where SubSequence == Substring {
2015 /// Matches a regex in its entirety, where the regex is created by
2116 /// the given closure.
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
10931113extension Unicode . Scalar {
You can’t perform that action at this time.
0 commit comments