Skip to content

Commit d82f906

Browse files
author
Jacob Hearst
committed
Try fixing reverse quant. Add some unit tests
1 parent b24cc58 commit d82f906

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Sources/_StringProcessing/Engine/MEReverseQuantify.swift

+8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ extension Processor {
5858
signalFailure()
5959
return false
6060
}
61+
6162
currentPosition = previous
63+
64+
// If we've reached the start of the string but still have more trips, fail
65+
if currentPosition == start, trips < payload.minTrips {
66+
signalFailure()
67+
return false
68+
}
69+
6270
trips += 1
6371
}
6472

Tests/RegexTests/MatchTests.swift

+31-1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ func firstMatchTests(
298298
enableTracing: Bool = false,
299299
dumpAST: Bool = false,
300300
xfail: Bool = false,
301+
validateOptimizations: Bool = true,
301302
semanticLevel: RegexSemanticLevel = .graphemeCluster,
302303
file: StaticString = #filePath,
303304
line: UInt = #line
@@ -311,6 +312,7 @@ func firstMatchTests(
311312
enableTracing: enableTracing,
312313
dumpAST: dumpAST,
313314
xfail: xfail,
315+
validateOptimizations: validateOptimizations,
314316
semanticLevel: semanticLevel,
315317
file: file,
316318
line: line)
@@ -1586,7 +1588,7 @@ extension RegexTests {
15861588

15871589
// TODO: Why is a match not found when unoptimized?
15881590
firstMatchTest(
1589-
#"\d{3}(?<=USD\d{3})"#, input: "Price: USD100", match: "100")
1591+
#"\d{3}(?<=USD\d{3})"#, input: "Price: USD100", match: "100", validateOptimizations: false)
15901592

15911593
firstMatchTest(
15921594
#"(?<!USD)\d+"#, input: "Price: JYP100", match: "100")
@@ -1598,6 +1600,34 @@ extension RegexTests {
15981600

15991601
firstMatchTest(
16001602
#"\d{3}(?<!USD\d{3})"#, input: "Price: JYP100", match: "100")
1603+
1604+
firstMatchTest(#"(?<=abc)def"#, input: "abcdefg", match: "def", validateOptimizations: false)
1605+
firstMatchTests(
1606+
#"(?<=az|b|c)def"#,
1607+
("azdefg", "def"),
1608+
("bdefg", "def"),
1609+
("cdefg", "def"),
1610+
("123defg", nil),
1611+
validateOptimizations: false
1612+
)
1613+
1614+
// FIXME: quickMatch and thoroughMatch have different results
1615+
firstMatchTest(
1616+
#"(?<=\d{1,3}-.{1,3}-\d{1,3})suffix"#,
1617+
input: "123-_+/-789suffix",
1618+
match: "suffix",
1619+
validateOptimizations: false
1620+
)
1621+
1622+
firstMatchTests(
1623+
#"(?<=^\d{1,3})abc"#,
1624+
("123abc", "abc"),
1625+
("12abc", "abc"),
1626+
("1abc", "abc"),
1627+
("1234abc", nil), // FIXME: Shouldn't match but does because `^` assertions are broken
1628+
("z123abc", nil), // FIXME: Same as above
1629+
validateOptimizations: false
1630+
)
16011631
}
16021632

16031633
func testMatchAnchors() throws {

0 commit comments

Comments
 (0)