Skip to content

Commit b4c74a2

Browse files
committed
fix: resolve quantifier overwrite bug in withOptional syntactic sugar
1 parent 59a41ff commit b4c74a2

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ String priceRegex = anywhere()
7070
)
7171
.shake();
7272

73-
// Result: Cost: \$[0-9]+(?:(?:\\.[0-9]{2}))?
73+
// Result: Cost: \$[0-9]+(?:\.[0-9]{2})?
7474
```
7575

7676
## API Overview

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
allprojects {
22
group = 'com.mirkoddd'
3-
version = '1.1.0'
3+
version = '1.1.1'
44

55
repositories {
66
mavenCentral()

sift-core/src/main/java/com/mirkoddd/sift/SiftBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ public ConnectorStep any() {
107107
// --- CONNECTORS ---
108108
@Override
109109
public ConnectorStep withOptional(SiftPattern pattern) {
110-
return this.optional().followedBy(pattern);
110+
return this.followedBy().optional().followedBy(pattern);
111111
}
112112

113113
@Override
114114
public ConnectorStep withOptional(char character) {
115-
return this.optional().followedBy(character);
115+
return this.followedBy().optional().followedBy(character);
116116
}
117117

118118
@Override

sift-core/src/test/java/com/mirkoddd/sift/SiftTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,30 @@ void wordBoundaries() {
334334
assertRegexDoesNotMatch(regex, "scatter");
335335
}
336336
}
337+
338+
@Test
339+
@DisplayName("Bugfix: withOptional syntactic sugar should not overwrite previous quantifiers")
340+
void syntacticSugarQuantifierBug() {
341+
String regex = anywhere()
342+
.oneOrMore().digits()
343+
.withOptional(literal("A"))
344+
.shake();
345+
346+
// before fix: [0-9]?(?:A)?
347+
assertEquals("[0-9]+(?:A)?", regex);
348+
349+
//from README.md
350+
String priceRegex = anywhere()
351+
.followedBy(literal("Cost: $"))
352+
.followedBy().oneOrMore().digits()
353+
.withOptional(
354+
anywhere().followedBy('.').followedBy().exactly(2).digits()
355+
)
356+
.shake();
357+
358+
359+
assertEquals("Cost: \\$[0-9]+(?:\\.[0-9]{2})?", priceRegex);
360+
assertRegexMatches(priceRegex, "Cost: $10");
361+
assertRegexMatches(priceRegex, "Cost: $10.99");
362+
}
337363
}

0 commit comments

Comments
 (0)