Skip to content

Commit 188e1f6

Browse files
committed
Fix an issue with 'DynamicCaptures' where there is no captures.
1 parent 2637691 commit 188e1f6

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

Diff for: Sources/_StringProcessing/RegexDSL/Core.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ extension RegexProtocol {
8787
guard let result = executor.execute(input: input) else {
8888
return nil
8989
}
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)
9197
}
9298
}
9399

Diff for: Sources/_StringProcessing/RegexDSL/DynamicCaptures.swift

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public enum DynamicCaptures: Equatable {
1212
indirect case optional(DynamicCaptures?)
1313
indirect case array([DynamicCaptures])
1414

15+
public static var empty: Self {
16+
.tuple([])
17+
}
18+
1519
internal init(_ capture: Capture) {
1620
switch capture {
1721
case .atom(let atom):

Diff for: Tests/RegexDSLTests/RegexDSLTests.swift

+20-8
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,25 @@ class RegexDSLTests: XCTestCase {
184184
}
185185

186186
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+
}
195207
}
196208
}

0 commit comments

Comments
 (0)