Exemple #1
0
 def test_code1(self):
     r_code1 = get_code(r'[abc][def][ghi]')
     res = rsre_core.search(r_code1, "fooahedixxx")
     assert res is None
     res = rsre_core.search(r_code1, "fooahcdixxx")
     assert res is not None
     assert res.span() == (5, 8)
Exemple #2
0
 def test_code1(self):
     r_code1 = get_code(r'[abc][def][ghi]')
     res = rsre_core.search(r_code1, "fooahedixxx")
     assert res is None
     res = rsre_core.search(r_code1, "fooahcdixxx")
     assert res is not None
     assert res.span() == (5, 8)
Exemple #3
0
def main(n):
    assert n >= 0
    pattern = [n] * n
    string = chr(n) * n
    rsre_core.search(pattern, string)
    #
    unicodestr = unichr(n) * n
    ctx = rsre_core.UnicodeMatchContext(pattern, unicodestr, 0,
                                        len(unicodestr), 0)
    rsre_core.search_context(ctx)
    #
    return 0
Exemple #4
0
def main(n):
    assert n >= 0
    pattern = [n] * n
    string = chr(n) * n
    rsre_core.search(pattern, string)
    #
    unicodestr = unichr(n) * n
    ctx = rsre_core.UnicodeMatchContext(pattern, unicodestr,
                                        0, len(unicodestr), 0)
    rsre_core.search_context(ctx)
    #
    return 0
Exemple #5
0
 def split(self, string, maxsplit=0):
     splitlist = []
     start = 0
     n = 0
     last = 0
     while not maxsplit or n < maxsplit:
         match = rsre_core.search(self._code,
                                  string,
                                  start,
                                  flags=self.flags)
         if match is None:
             break
         if match.match_start == match.match_end:  # zero-width match
             if match.match_start == len(string):  # at end of string
                 break
             start = match.match_end + 1
             continue
         splitlist.append(string[last:match.match_start])
         # add groups (if any)
         if self.groups:
             match1 = self._make_match(match)
             splitlist.extend(match1.groups(None))
         n += 1
         last = start = match.match_end
     splitlist.append(string[last:])
     return splitlist
Exemple #6
0
def run_external(t, use_search):
    from rpython.rlib.rsre.test.re_tests import SUCCEED, FAIL, SYNTAX_ERROR
    pattern, s, outcome = t[:3]
    if len(t) == 5:
        repl, expected = t[3:5]
    else:
        assert len(t) == 3
    print 'trying:', t
    try:
        obj = get_code(pattern)
    except re.error:
        if outcome == SYNTAX_ERROR:
            return  # Expected a syntax error
        raise
    if outcome == SYNTAX_ERROR:
        raise Exception("this should have been a syntax error")
    #
    if use_search:
        result = rsre_core.search(obj, s)
    else:
        # Emulate a poor man's search() with repeated match()s
        for i in range(len(s)+1):
            result = rsre_core.match(obj, s, start=i)
            if result:
                break
    #
    if outcome == FAIL:
        if result is not None:
            raise Exception("succeeded incorrectly")
    elif outcome == SUCCEED:
        if result is None:
            raise Exception("failed incorrectly")
        # Matched, as expected, so now we compute the
        # result string and compare it to our expected result.
        start, end = result.span(0)
        vardict={'found': result.group(0),
                 'groups': result.group(),
                 }#'flags': result.re.flags}
        for i in range(1, 100):
            try:
                gi = result.group(i)
                # Special hack because else the string concat fails:
                if gi is None:
                    gi = "None"
            except IndexError:
                gi = "Error"
            vardict['g%d' % i] = gi
        #for i in result.re.groupindex.keys():
        #    try:
        #        gi = result.group(i)
        #        if gi is None:
        #            gi = "None"
        #    except IndexError:
        #        gi = "Error"
        #    vardict[i] = gi
        repl = eval(repl, vardict)
        if repl != expected:
            raise Exception("grouping error: %r should be %r" % (repl,
                                                                 expected))
Exemple #7
0
def entrypoint2(r, string, repeat):
    r = array2list(r)
    string = hlstr(string)
    match = None
    for i in range(repeat):
        match = rsre_core.search(r, string)
    if match is None:
        return -1
    else:
        return match.match_start
Exemple #8
0
def entrypoint2(r, string, repeat):
    r = array2list(r)
    string = hlstr(string)
    match = None
    for i in range(repeat):
        match = rsre_core.search(r, string)
    if match is None:
        return -1
    else:
        return match.match_start
Exemple #9
0
def search_in_file(filename):
    data = read(filename)
    p = 0
    while True:
        res = rsre_core.search(r_code1, data, p)
        if res is None:
            break
        matchstart, matchstop = res.span(1)
        assert 0 <= matchstart <= matchstop
        print '%s: %s' % (filename, data[matchstart:matchstop])
        p = res.span(0)[1]
Exemple #10
0
def search_in_file(filename):
    data = read(filename)
    p = 0
    while True:
        res = rsre_core.search(r_code1, data, p)
        if res is None:
            break
        matchstart, matchstop = res.span(1)
        assert 0 <= matchstart <= matchstop
        print '%s: %s' % (filename, data[matchstart:matchstop])
        p = res.span(0)[1]
Exemple #11
0
    def subn(self, repl, string, count=0):
        "NOT_RPYTHON"
        filter = repl
        if not callable(repl) and "\\" in repl:
            # handle non-literal strings; hand it over to the template compiler
            filter = re._subx(self, repl)
        start = 0
        sublist = []
        force_unicode = (isinstance(string, unicode)
                         or isinstance(repl, unicode))
        n = last_pos = 0
        while not count or n < count:
            match = rsre_core.search(self._code,
                                     string,
                                     start,
                                     flags=self.flags)
            if match is None:
                break
            if last_pos < match.match_start:
                sublist.append(string[last_pos:match.match_start])
            if not (last_pos == match.match_start == match.match_end
                    and n > 0):
                # the above ignores empty matches on latest position
                if callable(filter):
                    piece = filter(self._make_match(match))
                else:
                    piece = filter
                sublist.append(piece)
                last_pos = match.match_end
                n += 1
            elif last_pos >= len(string):
                break  # empty match at the end: finished
            #
            start = match.match_end
            if start == match.match_start:
                start += 1

        if last_pos < len(string):
            sublist.append(string[last_pos:])

        if n == 0:
            # not just an optimization -- see test_sub_unicode
            return string, n

        if force_unicode:
            item = u"".join(sublist)
        else:
            item = "".join(sublist)
        return item, n
Exemple #12
0
 def test_empty_search(self):
     r_code, r = get_code_and_re(r'')
     for j in range(-2, 6):
         for i in range(-2, 6):
             match = r.search('abc', i, j)
             res = rsre_core.search(r_code, 'abc', i, j)
             jk = min(max(j, 0), 3)
             ik = min(max(i, 0), 3)
             if ik <= jk:
                 assert match is not None
                 assert match.span() == (ik, ik)
                 assert res is not None
                 assert res.match_start == ik and res.match_end == ik
             else:
                 assert match is None
                 assert res is None
Exemple #13
0
 def match_port(self, w_port):
     self.ensure_compiled()
     if isinstance(w_port, values.W_StringInputPort):
         # fast path
         ctx = rsre_core.search(self.code, w_port.str, start=w_port.ptr)
         if not ctx:
             return None
         start, end = ctx.span(0) # the whole match
         w_port.ptr = end
         return _extract_result(ctx, self.groupcount)
     buf = PortBuffer(w_port)
     ctx = rsre_core.BufMatchContext(self.code, buf, 0, buf.getlength(), 0)
     matched = rsre_core.search_context(ctx)
     if not matched:
         return None
     return _extract_result(ctx, self.groupcount)
Exemple #14
0
 def test_empty_search(self):
     r_code, r = get_code_and_re(r'')
     for j in range(-2, 6):
         for i in range(-2, 6):
             match = r.search('abc', i, j)
             res = rsre_core.search(r_code, 'abc', i, j)
             jk = min(max(j, 0), 3)
             ik = min(max(i, 0), 3)
             if ik <= jk:
                 assert match is not None
                 assert match.span() == (ik, ik)
                 assert res is not None
                 assert res.match_start == ik and res.match_end == ik
             else:
                 assert match is None
                 assert res is None
Exemple #15
0
 def match_port(self, w_port):
     self.ensure_compiled()
     if isinstance(w_port, values.W_StringInputPort):
         # fast path
         ctx = rsre_core.search(self.code, w_port.str, start=w_port.ptr)
         if not ctx:
             return None
         start, end = ctx.span(0) # the whole match
         w_port.ptr = end
         return _extract_result(ctx, self.groupcount)
     buf = PortBuffer(w_port)
     ctx = rsre_core.BufMatchContext(self.code, buf, 0, buf.getlength(), 0)
     matched = rsre_core.search_context(ctx)
     if not matched:
         return None
     return _extract_result(ctx, self.groupcount)
Exemple #16
0
    def subn(self, repl, string, count=0):
        "NOT_RPYTHON"
        filter = repl
        if not callable(repl) and "\\" in repl:
            # handle non-literal strings; hand it over to the template compiler
            filter = re._subx(self, repl)
        start = 0
        sublist = []
        force_unicode = (isinstance(string, unicode) or
                         isinstance(repl, unicode))
        n = last_pos = 0
        while not count or n < count:
            match = rsre_core.search(self._code, string, start,
                                     flags=self.flags)
            if match is None:
                break
            if last_pos < match.match_start:
                sublist.append(string[last_pos:match.match_start])
            if not (last_pos == match.match_start
                             == match.match_end and n > 0):
                # the above ignores empty matches on latest position
                if callable(filter):
                    piece = filter(self._make_match(match))
                else:
                    piece = filter
                sublist.append(piece)
                last_pos = match.match_end
                n += 1
            elif last_pos >= len(string):
                break     # empty match at the end: finished
            #
            start = match.match_end
            if start == match.match_start:
                start += 1

        if last_pos < len(string):
            sublist.append(string[last_pos:])

        if n == 0:
            # not just an optimization -- see test_sub_unicode
            return string, n

        if force_unicode:
            item = u"".join(sublist)
        else:
            item = "".join(sublist)
        return item, n
Exemple #17
0
 def split(self, string, maxsplit=0):
     splitlist = []
     start = 0
     n = 0
     last = 0
     while not maxsplit or n < maxsplit:
         match = rsre_core.search(self._code, string, start,
                                  flags=self.flags)
         if match is None:
             break
         if match.match_start == match.match_end: # zero-width match
             if match.match_start == len(string): # at end of string
                 break
             start = match.match_end + 1
             continue
         splitlist.append(string[last:match.match_start])
         # add groups (if any)
         if self.groups:
             match1 = self._make_match(match)
             splitlist.extend(match1.groups(None))
         n += 1
         last = start = match.match_end
     splitlist.append(string[last:])
     return splitlist
Exemple #18
0
 def test_code2(self):
     r_code2 = get_code(r'<item>\s*<title>(.*?)</title>')
     res = rsre_core.search(r_code2, "foo bar <item>  <title>abc</title>def")
     assert res is not None
     assert res.span() == (8, 34)
Exemple #19
0
 def match_string(self, s):
     self.ensure_compiled()
     ctx = rsre_core.search(self.code, s)
     if not ctx:
         return None
     return _extract_result(ctx, self.groupcount)
Exemple #20
0
 def search(self, string, pos=0, endpos=sys.maxint):
     return self._make_match(
         rsre_core.search(self._code, string, pos, endpos,
                          flags=self.flags))
Exemple #21
0
 def test_pure_literal(self):
     r_code3 = get_code(r'foobar')
     res = rsre_core.search(r_code3, "foo bar foobar baz")
     assert res is not None
     assert res.span() == (8, 14)
Exemple #22
0
 def match_string_positions(self, s):
     self.ensure_compiled()
     ctx = rsre_core.search(self.code, s)
     if ctx is None:
         return []
     return _extract_spans(ctx, self.groupcount)
Exemple #23
0
 def match_string_positions(self, s, start=0, end=sys.maxint):
     self.ensure_compiled()
     ctx = rsre_core.search(self.code, s, start=start, end=end)
     if ctx is None:
         return None
     return _extract_spans(ctx, self.groupcount)
Exemple #24
0
 def test_code2(self):
     r_code2 = get_code(r'<item>\s*<title>(.*?)</title>')
     res = rsre_core.search(r_code2, "foo bar <item>  <title>abc</title>def")
     assert res is not None
     assert res.span() == (8, 34)
Exemple #25
0
 def search(self, string, pos=0, endpos=sys.maxint):
     return self._make_match(rsre_core.search(self._code, string,
                                              pos, endpos,
                                              flags=self.flags))
Exemple #26
0
 def test_pure_literal(self):
     r_code3 = get_code(r'foobar')
     res = rsre_core.search(r_code3, "foo bar foobar baz")
     assert res is not None
     assert res.span() == (8, 14)
Exemple #27
0
 def match_string(self, s):
     self.ensure_compiled()
     ctx = rsre_core.search(self.code, s)
     if not ctx:
         return None
     return _extract_result(ctx, self.groupcount)