Skip to content

Commit

Permalink
Improve NEWLINE pattern
Browse files Browse the repository at this point in the history
Old one could fail depending on previous matching activity, possibly
because of the use of \G (which I should have explained better in a
comment, back when I thought I knew why I was doing it). The documented
behavior of ^ $ and \z and reluctant quantifiers make for a simpler
and more dependable version.

Addresses issue #455.
  • Loading branch information
jcflack committed Sep 5, 2023
1 parent 7de3b99 commit 6a64e91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private Lexicals() { } // do not instantiate
* engine, letting it handle the details.
*/
public static final Pattern NEWLINE = Pattern.compile(
"(?ms:$(?:(?<!^).|(?<=\\G).){1,2}+)"
"(?ms:$.{1,2}?(?:^|\\z))" // fewest of 1,2 chars between $ and ^ (or \z)
);

/** White space <em>except</em> newline, for any Java-recognized newline.
Expand Down
18 changes: 18 additions & 0 deletions pljava-api/src/test/java/LexicalsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import static
org.postgresql.pljava.sqlgen.Lexicals.ISO_AND_PG_IDENTIFIER_CAPTURING;
import static
org.postgresql.pljava.sqlgen.Lexicals.NEWLINE;
import static
org.postgresql.pljava.sqlgen.Lexicals.SEPARATOR;
import static
Expand All @@ -45,6 +47,22 @@ public class LexicalsTest extends TestCase
{
public LexicalsTest(String name) { super(name); }

public void testNewline() throws Exception
{
Matcher m = NEWLINE.matcher("abcd\nefgh");
m.region(4, 9);
assertTrue("newline 0", m.lookingAt());
assertTrue("newline 1", m.lookingAt());

m.reset("abcd\r\nefgh").region(4, 10);
assertTrue("newline 2", m.lookingAt());
assertEquals("\r\n", m.group());

m.reset("abcd\n\refgh").region(4, 10);
assertTrue("newline 3", m.lookingAt());
assertEquals("\n", m.group());
}

public void testSeparator() throws Exception
{
Pattern allTheRest = Pattern.compile(".*", Pattern.DOTALL);
Expand Down

0 comments on commit 6a64e91

Please sign in to comment.