LANG-830 FastDateParser could use \Q \E to quote regexes

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1390954 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-27 11:50:52 +00:00
parent e5ae84a6fc
commit 6ecb0a30c3
2 changed files with 20 additions and 14 deletions

View File

@ -24,6 +24,7 @@
<release version="3.2" date="TBA" description="Next release">
<action issue="LANG-832" type="fix">FastDateParser does not handle unterminated quotes correctly</action>
<action issue="LANG-831" type="fix">FastDateParser does not handle white-space properly</action>
<action issue="LANG-830" type="fix">FastDateParser could use \Q \E to quote regexes</action>
<action issue="LANG-828" type="fix">FastDateParser does not handle non-Gregorian calendars properly</action>
<action issue="LANG-826" type="fix">FastDateParser does not handle non-ASCII digits correctly</action>
<action issue="LANG-825" type="add">Create StrBuilder APIs similar to String.format(String, Object...)</action>

View File

@ -297,6 +297,7 @@ public class FastDateParser implements DateParser, Serializable {
* @return The <code>StringBuilder</code>
*/
private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) {
regex.append("\\Q");
for(int i= 0; i<value.length(); ++i) {
char c= value.charAt(i);
switch(c) {
@ -308,24 +309,28 @@ public class FastDateParser implements DateParser, Serializable {
c= value.charAt(i);
}
break;
case '?':
case '[':
case ']':
case '(':
case ')':
case '{':
case '}':
case '\\':
case '|':
case '*':
case '+':
case '^':
case '$':
case '.':
regex.append('\\');
if(++i==value.length()) {
break;
}
/*
* If we have found \E, we replace it with \E\\E\Q, i.e. we stop the quoting,
* quote the \ in \E, then restart the quoting.
*
* Otherwise we just output the two characters.
* In each case the initial \ needs to be output and the final char is done at the end
*/
regex.append(c); // we always want the original \
c = value.charAt(i); // Is it followed by E ?
if (c == 'E') { // \E detected
regex.append("E\\\\E\\"); // see comment above
c = 'Q'; // appended below
}
break;
}
regex.append(c);
}
regex.append("\\E");
return regex;
}