Skip to content

Commit f152ade

Browse files
committed
Extract RegexUtils.getNotFollowingEscapePattern()
1 parent 3134f40 commit f152ade

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opensolaris.opengrok.util;
25+
26+
/**
27+
* Represents a container for regex-related utility methods.
28+
*/
29+
public class RegexUtils {
30+
31+
/** Private to enforce singleton. */
32+
private RegexUtils() {
33+
}
34+
35+
/**
36+
* Gets a zero-width expression intended to follow a character match that
37+
* asserts that the character either does not follow a backslash escape or
38+
* follows an even number¹ of backslash escapes.
39+
* <p>
40+
* ¹"even number" is limited to 2,4,6 because Java look-behind is not
41+
* variable length but instead must have a definite upper bound in the
42+
* regex definition.
43+
* @return a defined expression:
44+
* <pre>
45+
* {@code
46+
* ((?<=^.)|(?<=[^\\].)|(?<=^(\\\\){1,3}.)|(?<=[^\\](\\\\){1,3}.))
47+
* }
48+
* </pre>
49+
* (Edit above and paste below [in NetBeans] for easy String escaping.)
50+
*/
51+
public static String getNotFollowingEscapePattern() {
52+
return "((?<=^.)|(?<=[^\\\\].)|(?<=^(\\\\\\\\){1,3}.)|(?<=[^\\\\](\\\\\\\\){1,3}.))";
53+
}
54+
}

src/org/opensolaris/opengrok/util/StringUtils.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,13 @@ public final class StringUtils {
3636

3737
/**
3838
* Matches an apostrophe not following a backslash escape or following an
39-
* even number¹ of backslash escapes:
40-
* <pre>
41-
* {@code
42-
* \'((?<=^.)|(?<=[^\\].)|(?<=^(\\\\){1,3}.)|(?<=[^\\](\\\\){1,3}.))
43-
* }
44-
* </pre>
45-
* (Edit above and paste below [in NetBeans] for easy String escaping.)
39+
* even number¹ of backslash escapes.
4640
* <p>
47-
* ¹"even number" is limited to 2,4,6 because Java look-behind is not
48-
* variable length but instead must have a definite upper bound in the
49-
* regex definition.
41+
* ¹See {@link RegexUtils#getNotFollowingEscapePattern()} for a caveat
42+
* about the backslash assertion.
5043
*/
5144
public static final Pattern APOS_NO_BSESC =
52-
Pattern.compile("\\'((?<=^.)|(?<=[^\\\\].)|(?<=^(\\\\\\\\){1,3}.)|(?<=[^\\\\](\\\\\\\\){1,3}.))");
45+
Pattern.compile("\\'" + RegexUtils.getNotFollowingEscapePattern());
5346

5447
/**
5548
* Matches the close of a C comment:

0 commit comments

Comments
 (0)