Support trivial regular expressions with special escaped characters

When a regular expression contains escaped characters such as the
backslash, it is actually still a literal string. So let's support the
trivially-escaped characters, too, that are documented in

	http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2013-10-17 15:07:15 -05:00
parent 466a7fc0c0
commit f55ac46602

View File

@ -35,20 +35,20 @@ public class Pattern {
private final String pattern;
protected Pattern(String pattern, int flags) {
this.pattern = pattern;
this.pattern = trivial(pattern);
this.patternFlags = flags;
if (! trivial(pattern)) {
throw new UnsupportedOperationException
("only trivial regular expressions are supported so far (" + pattern + ")");
}
}
private static boolean trivial(String pattern) {
private static String trivial(String pattern) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < pattern.length(); ++i) {
char c = pattern.charAt(i);
switch (c) {
case '\\':
if (++i == pattern.length() || (c = unescape(pattern.charAt(i))) != -1) {
break;
}
// fallthru
case '.':
case '*':
case '+':
@ -62,10 +62,32 @@ public class Pattern {
case ')':
case '^':
case '$':
return false;
throw new UnsupportedOperationException
("only trivial regular expressions are supported so far (" + pattern + ")");
}
buffer.append(c);
}
return true;
return buffer.toString();
}
private static char unescape(char c) {
switch (c) {
case '\\':
return c;
case 'a':
return 0x0007;
case 'e':
return 0x001B;
case 'f':
return 0x000C;
case 'n':
return 0x000A;
case 'r':
return 0x000D;
case 't':
return 0x0009;
}
return (char)-1;
}
public static Pattern compile(String regex) {