Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Sources/_StringProcessing/Regex/DSLList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ extension DSLList {
case .concatenation(let count):
var position = position + 1
if findLast {
for _ in 0..<(count - 1) {
for _ in (0..<count).dropLast() {
skipNode(&position)
position += 1
}
Expand Down Expand Up @@ -165,17 +165,21 @@ extension DSLList {
// Remove the postfix node and fix up any parent concatenations
other.nodes.remove(at: postfixIndex)
var i = postfixIndex - 1
Loop:

while i >= 0 {
switch other.nodes[i] {
case .concatenation(let count):
other.nodes[i] = .concatenation(count - 1)
break Loop
// Omit a concatenation entirely if it would have zero children.
if count == 1 {
other.nodes[i] = .empty
} else {
other.nodes[i] = .concatenation(count - 1)
}
case .limitCaptureNesting, .ignoreCapturesInTypedOutput:
other.nodes.remove(at: i)
i -= 1
default:
break Loop
return
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,29 @@ class RegexDSLTests: XCTestCase {
""")
}

func testManyConcatenatedComponents() throws {
let r = Regex {
#/a\n/#
Regex<Substring>(verbatim: "/some/path")
#/\n/#
#/b\n/#
Regex<Substring>(verbatim: "/some/path")
#/\n/#
#/c\n/#
Regex<Substring>(verbatim: "/some/path")
#/\n/#
#/d\n/#
Regex<Substring>(verbatim: "/some/path")
#/\n/#
#/e\n/#
Regex<Substring>(verbatim: "/some/path")
#/\n/#
#/f/#
}
let input = "a\n/some/path\nb\n/some/path\nc\n/some/path\nd\n/some/path\ne\n/some/path\nf"
XCTAssertNotNil(try r.wholeMatch(in: input))
}

func testRegexComponentBuilderResultType() {
// Test that the user can declare a closure or computed property marked with
// `@RegexComponentBuilder` with `Regex` as the result type.
Expand Down
Loading