File tree Expand file tree Collapse file tree 2 files changed +35
-13
lines changed
Sources/_StringProcessing Expand file tree Collapse file tree 2 files changed +35
-13
lines changed Original file line number Diff line number Diff line change @@ -17,23 +17,24 @@ func constructExistentialOutputComponent(
1717 component: ( range: Range < String . Index > , value: Any ? ) ? ,
1818 optionalCount: Int
1919) -> Any {
20- let someCount : Int
21- var underlying : Any
2220 if let component = component {
23- underlying = component. value ?? input [ component. range]
24- someCount = optionalCount
21+ var underlying = component. value ?? input [ component. range]
22+ for _ in 0 ..< optionalCount {
23+ func wrap< T> ( _ x: T ) {
24+ underlying = Optional ( x) as Any
25+ }
26+ _openExistential ( underlying, do: wrap)
27+ }
28+ return underlying
2529 } else {
26- // Ok since we Any-box every step up the ladder
27- underlying = Optional < Any > ( nil ) as Any
28- someCount = optionalCount - 1
29- }
30- for _ in 0 ..< someCount {
31- func wrap< T> ( _ x: T ) {
32- underlying = Optional ( x) as Any
30+ precondition ( optionalCount > 0 , " Must have optional type " )
31+ func makeNil< T> ( _ x: T . Type ) -> Any {
32+ T ? . none as Any
3333 }
34- _openExistential ( underlying, do: wrap)
34+ let underlyingTy = TypeConstruction . optionalType (
35+ of: Substring . self, depth: optionalCount - 1 )
36+ return _openExistential ( underlyingTy, do: makeNil)
3537 }
36- return underlying
3738}
3839
3940@available ( SwiftStdlib 5 . 7 , * )
Original file line number Diff line number Diff line change @@ -444,6 +444,8 @@ class RegexDSLTests: XCTestCase {
444444
445445 try _testDSLCaptures (
446446 ( " abcdef2 " , ( " abcdef2 " , " f " ) ) ,
447+ ( " 2 " , ( " 2 " , nil ) ) ,
448+ ( " " , ( " " , nil ) ) ,
447449 matchType: ( Substring, Substring?? ) . self, == )
448450 {
449451 Optionally {
@@ -1222,6 +1224,25 @@ class RegexDSLTests: XCTestCase {
12221224 }
12231225 }
12241226 }
1227+
1228+ func testOptionalNesting( ) throws {
1229+ let r = Regex {
1230+ Optionally {
1231+ Optionally {
1232+ Capture {
1233+ " a "
1234+ }
1235+ }
1236+ }
1237+ }
1238+ if let _ = try r. wholeMatch ( in: " " ) !. output. 1 {
1239+ XCTFail ( " Unexpected capture match " )
1240+ }
1241+ if let _ = try r. wholeMatch ( in: " a " ) !. output. 1 { }
1242+ else {
1243+ XCTFail ( " Expected to match capture " )
1244+ }
1245+ }
12251246}
12261247
12271248extension Unicode . Scalar {
You can’t perform that action at this time.
0 commit comments