File tree 3 files changed +31
-9
lines changed
Sources/_StringProcessing/RegexDSL
3 files changed +31
-9
lines changed Original file line number Diff line number Diff line change @@ -87,7 +87,13 @@ extension RegexProtocol {
87
87
guard let result = executor. execute ( input: input) else {
88
88
return nil
89
89
}
90
- return RegexMatch ( range: result. range, captures: ( ) as! Capture )
90
+ let convertedCapture : Capture
91
+ if Capture . self == DynamicCaptures . self {
92
+ convertedCapture = DynamicCaptures . tuple ( [ ] ) as! Capture
93
+ } else {
94
+ convertedCapture = ( ) as! Capture
95
+ }
96
+ return RegexMatch ( range: result. range, captures: convertedCapture)
91
97
}
92
98
}
93
99
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ public enum DynamicCaptures: Equatable {
12
12
indirect case optional( DynamicCaptures ? )
13
13
indirect case array( [ DynamicCaptures ] )
14
14
15
+ public static var empty : Self {
16
+ . tuple( [ ] )
17
+ }
18
+
15
19
internal init ( _ capture: Capture ) {
16
20
switch capture {
17
21
case . atom( let atom) :
Original file line number Diff line number Diff line change @@ -184,13 +184,25 @@ class RegexDSLTests: XCTestCase {
184
184
}
185
185
186
186
func testDynamicCaptures( ) throws {
187
- let regex = try Regex ( #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"# )
188
- let line = """
189
- A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM COMBINING MARK TUKWENTIS
190
- """
191
- let captures = try XCTUnwrap ( line. match ( regex) ? . captures)
192
- XCTAssertEqual (
193
- captures,
194
- . tuple( [ . substring( " A6F0 " ) , . optional( . substring( " A6F1 " ) ) , . substring( " Extend " ) ] ) )
187
+ do {
188
+ let regex = try Regex ( " aabcc. " )
189
+ let line = " aabccd "
190
+ let captures = try XCTUnwrap ( line. match ( regex) ? . captures)
191
+ XCTAssertEqual ( captures, . empty)
192
+ }
193
+ do {
194
+ let regex = try Regex ( #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"# )
195
+ let line = """
196
+ A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM \
197
+ COMBINING MARK TUKWENTIS
198
+ """
199
+ let captures = try XCTUnwrap ( line. match ( regex) ? . captures)
200
+ XCTAssertEqual (
201
+ captures,
202
+ . tuple( [
203
+ . substring( " A6F0 " ) ,
204
+ . optional( . substring( " A6F1 " ) ) ,
205
+ . substring( " Extend " ) ] ) )
206
+ }
195
207
}
196
208
}
You can’t perform that action at this time.
0 commit comments