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"> <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-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-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-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-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> <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> * @return The <code>StringBuilder</code>
*/ */
private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) { private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) {
regex.append("\\Q");
for(int i= 0; i<value.length(); ++i) { for(int i= 0; i<value.length(); ++i) {
char c= value.charAt(i); char c= value.charAt(i);
switch(c) { switch(c) {
@ -308,24 +309,28 @@ public class FastDateParser implements DateParser, Serializable {
c= value.charAt(i); c= value.charAt(i);
} }
break; break;
case '?':
case '[':
case ']':
case '(':
case ')':
case '{':
case '}':
case '\\': case '\\':
case '|': if(++i==value.length()) {
case '*': break;
case '+': }
case '^': /*
case '$': * If we have found \E, we replace it with \E\\E\Q, i.e. we stop the quoting,
case '.': * quote the \ in \E, then restart the quoting.
regex.append('\\'); *
* 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(c);
} }
regex.append("\\E");
return regex; return regex;
} }