Skip to content

Instantly share code, notes, and snippets.

@vls
Created February 21, 2012 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vls/1873993 to your computer and use it in GitHub Desktop.
Save vls/1873993 to your computer and use it in GitHub Desktop.
pyre2 modification
cdef inline _re2.StringPiece* _alloc_sp(self, char* cstring, Py_ssize_t& size):
cdef _re2.StringPiece * sp
sp = new _re2.StringPiece(cstring, size)
return sp
cdef inline void _dealloc_sp(self, _re2.StringPiece* sp):
del sp
cdef inline _my_match(self, _re2.StringPiece sp_zero, int pos, int size, _re2.re2_Anchor anchoring, _re2.StringPiece* submatch, int nsubmatch):
return self.re_pattern.Match(sp_zero, <int>pos, <int>size, anchoring, submatch, nsubmatch)
cpdef search(self, string, int pos=0, int endpos=-1):
cdef Py_ssize_t size
cdef int result
cdef char * cstring
cdef int encoded = 0
cdef _re2.StringPiece * sp
cdef _re2.re2_Anchor anchoring
anchoring = _re2.UNANCHORED
#if hasattr(string, 'tostring'):
# string = string.tostring()
string = unicode_to_bytestring(string, &encoded)
if pystring_to_bytestring(string, &cstring, &size) == -1:
raise TypeError("expected string or buffer")
if endpos >= 0 and endpos <= pos:
return None
if endpos >= 0 and endpos < size:
size = endpos
if pos > size:
return None
sp = self._alloc_sp(cstring, size)
result = self._my_match(sp[0], pos, size, anchoring, NULL, 0)
#self._dealloc_sp(sp)
if result == 0:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment