Permalink
Browse files

Support \Q..\E quoting in simpleRegex parser SERVER-2862

  • Loading branch information...
1 parent e9dc703 commit 19639446c1716e9b1e5db8c21c8266a1c27d2c94 @RedBeard0531 RedBeard0531 committed Mar 29, 2011
Showing with 26 additions and 2 deletions.
  1. +26 −2 db/queryutil.cpp
View
@@ -79,16 +79,30 @@ namespace mongo {
return r; //breaking here fails with /^a?/
}
else if (c == '\\') {
- // slash followed by non-alphanumeric represents the following char
c = *(regex++);
- if ((c >= 'A' && c <= 'Z') ||
+ if (c == 'Q'){
+ // \Q...\E quotes everything inside
+ while (*regex) {
+ c = (*regex++);
+ if (c == '\\' && (*regex == 'E')){
+ regex++; //skip the 'E'
+ break; // go back to start of outer loop
+ }
+ else {
+ ss << c; // character should match itself
+ }
+ }
+ }
+ else if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '0') ||
(c == '\0')) {
+ // don't know what to do with these
r = ss.str();
break;
}
else {
+ // slash followed by non-alphanumeric represents the following char
ss << c;
}
}
@@ -1153,6 +1167,16 @@ namespace mongo {
BSONObj o = b.done();
assert( simpleRegex(o.firstElement()) == "foo #" );
}
+ {
+ assert( simpleRegex("^\\Qasdf\\E", "", NULL) == "asdf" );
+ assert( simpleRegex("^\\Qasdf\\E.*", "", NULL) == "asdf" );
+ assert( simpleRegex("^\\Qasdf", "", NULL) == "asdf" ); // PCRE supports this
+ assert( simpleRegex("^\\Qasdf\\\\E", "", NULL) == "asdf\\" );
+ assert( simpleRegex("^\\Qas.*df\\E", "", NULL) == "as.*df" );
+ assert( simpleRegex("^\\Qas\\Q[df\\E", "", NULL) == "as\\Q[df" );
+ assert( simpleRegex("^\\Qas\\E\\\\E\\Q$df\\E", "", NULL) == "as\\E$df" ); // quoted string containing \E
+ }
+
}
} simple_regex_unittest;

0 comments on commit 1963944

Please sign in to comment.