Permalink
Browse files

Add an escape function, bound to RE2::QuoteMeta

  • Loading branch information...
1 parent fe96a16 commit 62f60e8a3acde9b638aec37698bd183ff96bf503 @bos bos committed May 21, 2012
Showing with 20 additions and 3 deletions.
  1. +18 −3 _re2.cc
  2. +2 −0 re2.py
View
21 _re2.cc
@@ -704,10 +704,25 @@ _compile(PyObject* self, PyObject* args, PyObject* kwds)
return create_regexp(pattern);
}
+static PyObject*
+escape(PyObject* self, PyObject* args)
+{
+ char *str;
+ Py_ssize_t len;
+
+ if (!PyArg_ParseTuple(args, "s#:escape", &str, &len)) {
+ return NULL;
+ }
+
+ std::string esc(RE2::QuoteMeta(StringPiece(str, (int) len)));
+
+ return PyString_FromStringAndSize(esc.c_str(), esc.size());
+}
+
static PyMethodDef methods[] = {
- {"_compile", (PyCFunction)_compile, METH_VARARGS | METH_KEYWORDS,
- NULL
- },
+ {"_compile", (PyCFunction)_compile, METH_VARARGS | METH_KEYWORDS, NULL},
+ {"escape", (PyCFunction)escape, METH_VARARGS,
+ "Escape all potentially meaningful regexp characters."},
{NULL} /* Sentinel */
};
View
2 re2.py
@@ -34,6 +34,7 @@ class error(Exception):
__all__ = [
"error",
+ "escape",
"compile",
"search",
"match",
@@ -42,6 +43,7 @@ class error(Exception):
# Module-private compilation function, for future caching, other enhancements
_compile = _re2._compile
+escape = _re2.escape
def compile(pattern):
"Compile a regular expression pattern, returning a pattern object."

0 comments on commit 62f60e8

Please sign in to comment.