SOLR-1615: fix StrParser quoted string backslash escaping, add java-style unicode escaping

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@886155 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2009-12-02 14:28:38 +00:00
parent 883aff729a
commit d71fd08b71
3 changed files with 54 additions and 3 deletions

View File

@ -104,6 +104,10 @@ Bug Fixes
* SOLR-1601: Schema browser does not indicate presence of charFilter. (koji) * SOLR-1601: Schema browser does not indicate presence of charFilter. (koji)
* SOLR-1615: Backslash escaping did not work in quoted strings
for local param arguments. (Wojtek Piaseczny, yonik)
Other Changes Other Changes
---------------------- ----------------------

View File

@ -625,14 +625,32 @@ public class QueryParsing {
} }
char ch = val.charAt(pos); char ch = val.charAt(pos);
if (ch=='\\') { if (ch=='\\') {
ch = pos<end ? val.charAt(pos++) : 0;
} else if (ch==delim) {
pos++; pos++;
return sb.toString(); if (pos>=end) break;
ch = val.charAt(pos);
switch(ch) {
case 'n' : ch='\n'; break;
case 't' : ch='\t'; break;
case 'r' : ch='\r'; break;
case 'b' : ch='\b'; break;
case 'f' : ch='\f'; break;
case 'u' :
if (pos+4 >= end) {
throw new ParseException("bad unicode escape \\uxxxx at pos" + (val_start-1) + " str='"+val+"'");
}
ch = (char)Integer.parseInt(val.substring(pos+1, pos+5), 16);
pos += 4;
break;
}
} else if (ch==delim) {
pos++; // skip over the quote
break;
} }
sb.append(ch); sb.append(ch);
pos++; pos++;
} }
return sb.toString();
} }
// next non-whitespace char // next non-whitespace char

View File

@ -46,6 +46,7 @@ public class TestQueryTypes extends AbstractSolrTestCase {
assertU(adoc("id","6", "v_f","8983")); assertU(adoc("id","6", "v_f","8983"));
assertU(adoc("id","7", "v_f","1.5")); assertU(adoc("id","7", "v_f","1.5"));
assertU(adoc("id","8", "v_ti","5")); assertU(adoc("id","8", "v_ti","5"));
assertU(adoc("id","9", "v_s","internal\"quote"));
Object[] arr = new Object[] { Object[] arr = new Object[] {
"id",999.0 "id",999.0
@ -125,6 +126,34 @@ public class TestQueryTypes extends AbstractSolrTestCase {
,"//result[@numFound='0']" ,"//result[@numFound='0']"
); );
//
// test escapes in quoted strings
//
// the control... unescaped queries looking for internal"quote
assertQ(req("q","{!raw f=v_s}internal\"quote")
,"//result[@numFound='1']"
);
// test that single quoted string needs no escape
assertQ(req("q","{!raw f=v_s v='internal\"quote'}")
,"//result[@numFound='1']"
);
// but it's OK if the escape is done
assertQ(req("q","{!raw f=v_s v='internal\\\"quote'}")
,"//result[@numFound='1']"
);
// test unicode escape
assertQ(req("q","{!raw f=v_s v=\"internal\\u0022quote\"}")
,"//result[@numFound='1']"
);
// inside a quoted string, internal"quote needs to be escaped
assertQ(req("q","{!raw f=v_s v=\"internal\\\"quote\"}")
,"//result[@numFound='1']"
);
assertQ("test custom plugin query", assertQ("test custom plugin query",
req("q","{!foo f=v_t}hello") req("q","{!foo f=v_t}hello")