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(
17
17
component: ( range: Range < String . Index > , value: Any ? ) ? ,
18
18
optionalCount: Int
19
19
) -> Any {
20
- let someCount : Int
21
- var underlying : Any
22
20
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
25
29
} 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
33
33
}
34
- _openExistential ( underlying, do: wrap)
34
+ let underlyingTy = TypeConstruction . optionalType (
35
+ of: Substring . self, depth: optionalCount - 1 )
36
+ return _openExistential ( underlyingTy, do: makeNil)
35
37
}
36
- return underlying
37
38
}
38
39
39
40
@available ( SwiftStdlib 5 . 7 , * )
Original file line number Diff line number Diff line change @@ -438,6 +438,8 @@ class RegexDSLTests: XCTestCase {
438
438
439
439
try _testDSLCaptures (
440
440
( " abcdef2 " , ( " abcdef2 " , " f " ) ) ,
441
+ ( " 2 " , ( " 2 " , nil ) ) ,
442
+ ( " " , ( " " , nil ) ) ,
441
443
matchType: ( Substring, Substring?? ) . self, == )
442
444
{
443
445
Optionally {
@@ -1216,6 +1218,25 @@ class RegexDSLTests: XCTestCase {
1216
1218
}
1217
1219
}
1218
1220
}
1221
+
1222
+ func testOptionalNesting( ) throws {
1223
+ let r = Regex {
1224
+ Optionally {
1225
+ Optionally {
1226
+ Capture {
1227
+ " a "
1228
+ }
1229
+ }
1230
+ }
1231
+ }
1232
+ if let _ = try r. wholeMatch ( in: " " ) !. output. 1 {
1233
+ XCTFail ( " Unexpected capture match " )
1234
+ }
1235
+ if let _ = try r. wholeMatch ( in: " a " ) !. output. 1 { }
1236
+ else {
1237
+ XCTFail ( " Expected to match capture " )
1238
+ }
1239
+ }
1219
1240
}
1220
1241
1221
1242
extension Unicode . Scalar {
You can’t perform that action at this time.
0 commit comments