Skip to content

Commit

Permalink
Character class occupy 1 position so cannot be merged with another ch…
Browse files Browse the repository at this point in the history
…aracter class

---
 Signed-off-by: Peter Kriens <[email protected]>

Signed-off-by: Peter Kriens <[email protected]>
  • Loading branch information
pkriens committed Jan 25, 2024
1 parent 98e3200 commit c576d21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
3 changes: 0 additions & 3 deletions aQute.libg/src/aQute/libg/re/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -1635,9 +1635,6 @@ public CharacterClass not() {

@Override
public Optional<RE> merge(RE other) {
if (other instanceof CharacterClass cc) {
return Optional.of(new CharacterClass(literal + cc.literal));
}
return Optional.empty();
}

Expand Down
48 changes: 47 additions & 1 deletion aQute.libg/test/aQute/libg/re/RETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import static aQute.libg.re.Catalog.ahead;
import static aQute.libg.re.Catalog.atomic;
import static aQute.libg.re.Catalog.back;
import static aQute.libg.re.Catalog.backslash;
import static aQute.libg.re.Catalog.behind;
import static aQute.libg.re.Catalog.capture;
import static aQute.libg.re.Catalog.cc;
import static aQute.libg.re.Catalog.comma;
import static aQute.libg.re.Catalog.dotall;
import static aQute.libg.re.Catalog.dquote;
import static aQute.libg.re.Catalog.endOfInput;
import static aQute.libg.re.Catalog.g;
import static aQute.libg.re.Catalog.if_;
import static aQute.libg.re.Catalog.list;
Expand Down Expand Up @@ -72,6 +74,30 @@ public class RETest {
ids.add("ωAngularFrequency"); // Omega symbol
}

@Test
public void testComplicatedLineDetect() {
RE NORMAL_LINE_P = g(behind(backslash.not()), nl, ahead(endOfInput).not());
RE BACKSLASHED_LINE_P = g(backslash, nl, ahead(endOfInput).not());

assertThat(NORMAL_LINE_P.findIn(WITH_NORMAL_LINES)).isPresent();
assertThat(NORMAL_LINE_P.findIn(WITH_ESCAPED_LINES)).isNotPresent();
assertThat(NORMAL_LINE_P.findIn(WITH_MIXED_LINES)).isPresent();

assertThat(BACKSLASHED_LINE_P.findIn(WITH_NORMAL_LINES)).isNotPresent();
assertThat(BACKSLASHED_LINE_P.findIn(WITH_ESCAPED_LINES)).isPresent();
assertThat(BACKSLASHED_LINE_P.findIn(WITH_MIXED_LINES)).isPresent();

assertThat(NORMAL_LINE_P.append(WITH_NORMAL_LINES, m -> "X")).isEqualTo("line 1Xline 2Xline 3Xline 4\n");
assertThat(NORMAL_LINE_P.append(WITH_ESCAPED_LINES, m -> "X")).isEqualTo(WITH_ESCAPED_LINES);
assertThat(NORMAL_LINE_P.append(WITH_MIXED_LINES, m -> "X")).isEqualTo("line 1\\\nline 2Xline 3\\\nline 3\n");

assertThat(BACKSLASHED_LINE_P.append(WITH_NORMAL_LINES, m -> "X")).isEqualTo(WITH_NORMAL_LINES);
assertThat(BACKSLASHED_LINE_P.append(WITH_ESCAPED_LINES, m -> "X"))
.isEqualTo("line 1Xline 2Xline 3Xline 3\\\n");
assertThat(BACKSLASHED_LINE_P.append(WITH_MIXED_LINES, m -> "X"))
.isEqualTo("line 1Xline 2\n" + "line 3Xline 3\n");
}

@Test
public void test() {
@SuppressWarnings("unused")
Expand Down Expand Up @@ -482,7 +508,7 @@ class Internet extends Catalog {
assertThat(x.email.findAllIn("bla bla [email protected] bal la a [email protected] and more nonsense")
.count()).isEqualTo(2);

assertThat(Catalog.nl.findAllIn("line 1\rline 2\nline 3\r\nline 4\n")
assertThat(nl.findAllIn("line 1\rline 2\nline 3\r\nline 4\n")
.count()).isEqualTo(4);
assertThat(word.findAllIn(
"The quick brown fox jumped over the lazy dog. However, somewhere on the horizon there was light: 'A ship came into the harbour.'")
Expand Down Expand Up @@ -531,4 +557,24 @@ public void lookbehind() {
.count()).isEqualTo(3);
}

final static String WITH_NORMAL_LINES = """
line 1
line 2
line 3
line 4
""";

final static String WITH_ESCAPED_LINES = """
line 1\\
line 2\\
line 3\\
line 3\\
""";
final static String WITH_MIXED_LINES = """
line 1\\
line 2
line 3\\
line 3
""";

}

0 comments on commit c576d21

Please sign in to comment.