Esempio n. 1
0
 def make_ctx(self, w_string, pos=0, endpos=sys.maxint, flags=0):
     """Make a StrMatchContext, BufMatchContext or a UnicodeMatchContext for
     searching in the given w_string object."""
     space = self.space
     length, unicodestr, string, buf = self.getstring(w_string)
     if pos < 0:
         pos = 0
     elif pos > length:
         pos = length
     if endpos < pos:
         endpos = pos
     elif endpos > length:
         endpos = length
     flags = self.flags | flags
     #
     if unicodestr is not None:
         if self.is_known_bytes():
             raise oefmt(
                 space.w_TypeError,
                 "can't use a bytes pattern on a string-like "
                 "object")
         return rsre_core.UnicodeMatchContext(unicodestr, pos, endpos,
                                              flags)
     else:
         if self.is_known_unicode():
             raise oefmt(
                 space.w_TypeError,
                 "can't use a string pattern on a bytes-like "
                 "object")
         if string is not None:
             return rsre_core.StrMatchContext(string, pos, endpos, flags)
         else:
             return rsre_core.BufMatchContext(buf, pos, endpos, flags)
Esempio n. 2
0
 def make_ctx(self, s, start, end):
     self.ensure_compiled()
     start, end = rsre_core._adjust(start, end, len(s))
     if isinstance(s, unicode):
         return rsre_core.UnicodeMatchContext(s, start, end, self.flags)
     assert isinstance(s, str)
     return rsre_core.StrMatchContext(s, start, end, self.flags)
Esempio n. 3
0
 def make_ctx(self, w_string, pos=0, endpos=sys.maxint):
     """Make a StrMatchContext, BufMatchContext or a UnicodeMatchContext for
     searching in the given w_string object."""
     space = self.space
     if pos < 0:
         pos = 0
     if endpos < pos:
         endpos = pos
     if space.isinstance_w(w_string, space.w_unicode):
         unicodestr = space.unicode_w(w_string)
         if pos > len(unicodestr):
             pos = len(unicodestr)
         if endpos > len(unicodestr):
             endpos = len(unicodestr)
         return rsre_core.UnicodeMatchContext(self.code, unicodestr, pos,
                                              endpos, self.flags)
     elif space.isinstance_w(w_string, space.w_str):
         str = space.str_w(w_string)
         if pos > len(str):
             pos = len(str)
         if endpos > len(str):
             endpos = len(str)
         return rsre_core.StrMatchContext(self.code, str, pos, endpos,
                                          self.flags)
     else:
         buf = space.readbuf_w(w_string)
         size = buf.getlength()
         assert size >= 0
         if pos > size:
             pos = size
         if endpos > size:
             endpos = size
         return rsre_core.BufMatchContext(self.code, buf, pos, endpos,
                                          self.flags)
Esempio n. 4
0
 def make_ctx(self, w_string, pos=0, endpos=sys.maxint):
     """Make a StrMatchContext, BufMatchContext or a Utf8MatchContext for
     searching in the given w_string object."""
     space = self.space
     if pos < 0:
         pos = 0
     if endpos < pos:
         endpos = pos
     if space.isinstance_w(w_string, space.w_unicode):
         if self.is_known_bytes():
             raise oefmt(
                 space.w_TypeError,
                 "can't use a bytes pattern on a string-like "
                 "object")
         w_unicode_obj = space.convert_arg_to_w_unicode(w_string)
         utf8str = w_unicode_obj._utf8
         length = w_unicode_obj._len()
         if pos <= 0:
             bytepos = 0
         elif pos >= length:
             bytepos = len(utf8str)
         else:
             bytepos = w_unicode_obj._index_to_byte(pos)
         if endpos >= length:
             endbytepos = len(utf8str)
         else:
             endbytepos = w_unicode_obj._index_to_byte(endpos)
         if w_unicode_obj.is_ascii():
             ctx = UnicodeAsciiMatchContext(utf8str, bytepos, endbytepos)
         else:
             ctx = rsre_utf8.Utf8MatchContext(utf8str, bytepos, endbytepos)
             # we store the w_string on the ctx too, for
             # W_SRE_Match.bytepos_to_charindex()
             ctx.w_unicode_obj = w_unicode_obj
         return ctx
     elif self.is_known_unicode():
         raise oefmt(space.w_TypeError,
                     "can't use a string pattern on a bytes-like "
                     "object")
     elif space.isinstance_w(w_string, space.w_bytes):
         string = space.bytes_w(w_string)
         length = len(string)
         if pos > length:
             pos = length
         if endpos > length:
             endpos = length
         return rsre_core.StrMatchContext(string, pos, endpos)
     else:
         buf = space.readbuf_w(w_string)
         size = buf.getlength()
         assert size >= 0
         if pos > size:
             pos = size
         if endpos > size:
             endpos = size
         return rsre_core.BufMatchContext(buf, pos, endpos)
Esempio n. 5
0
    def matches(self, s, pos):
        if not we_are_translated():
            m = self.re.match(s, pos)
            return Match(*m.span(0)) if m is not None else None
        else:
            assert pos >= 0
            ctx = rsre_core.StrMatchContext(s, pos, len(s))

            matched = rsre_core.match_context(ctx, self._pattern)
            if matched:
                return Match(ctx.match_start, ctx.match_end)
            else:
                return None
Esempio n. 6
0
 def make_ctx(self, s, offset=0):
     assert offset >= 0
     endpos = len(s)
     return rsre_core.StrMatchContext(self.code, s, offset, endpos,
                                      self.flags)
Esempio n. 7
0
 def _make_str_match_context(self, str, pos, endpos):
     # for tests to override
     return rsre_core.StrMatchContext(str, pos, endpos)
Esempio n. 8
0
def regexp_match(cache, re, string):
    pos = 0
    endpos = len(string)
    code, flags, _, _, _, _ = regexp.compile(cache, re)
    return (rsre_core.StrMatchContext(string, pos, endpos,
                                      flags), rsre_core.CompiledPattern(code))
Esempio n. 9
0
 def make_ctx(self, s, offset=0):
     assert offset >= 0
     endpos = len(s)
     return (rsre_core.StrMatchContext(s, offset, endpos, self.flags),
             rsre_core.CompiledPattern(self.code))