Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an escape function, bound to RE2::QuoteMeta

  • Loading branch information...
commit 62f60e8a3acde9b638aec37698bd183ff96bf503 1 parent fe96a16
Bryan O'Sullivan bos authored
Showing with 20 additions and 3 deletions.
  1. +18 −3 _re2.cc
  2. +2 −0  re2.py
21 _re2.cc
View
@@ -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 */
};
2  re2.py
View
@@ -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."
Please sign in to comment.
Something went wrong with that request. Please try again.