예제 #1
0
 def test_build_expr_pattern_with_star(self):
     expr = compile_re('a*', False)
     assert isinstance(expr, StateSplit)
     assert isinstance(expr.out, StateChar)
     assert expr.out.out == expr
     assert expr.out.start == ord('a')
     assert isinstance(expr.out2, StateMatch)
예제 #2
0
    def test_build_expr_pattern_with_star_4(self):
        expr = compile_re('a.*c%a*', False)

        # a
        assert isinstance(expr, StateChar)
        assert expr.start == ord('a')

        # .*
        node = expr.out
        assert isinstance(node, StateSplit)
        assert isinstance(node.out, StateDot)
        assert node.out.out == node

        # c
        node = node.out2
        assert isinstance(node, StateChar)
        assert node.start == ord('c')

        # %a*
        node = node.out
        assert isinstance(node, StateSplit)
        assert isinstance(node.out, StateCharRange)
        assert node.out.start == ord('A')
        assert node.out.stop == ord('z')
        assert node.out.out == node

        # match
        node = node.out2
        assert isinstance(node, StateMatch)
예제 #3
0
    def test_build_group_star_chained(self):
        expr = compile_re('(ab)*ab', False)

        # *
        assert isinstance(expr, StateSplit)

        # a
        node = expr.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')

        # b
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')
        assert node.out == expr

        # ab
        node = expr.out2
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')

        # b
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')

        # match
        assert isinstance(node.out, StateMatch)
예제 #4
0
    def test_build_expr_with_repitition(self):
        expr = compile_re('a{3}')

        assert isinstance(expr, StateChar)
        assert isinstance(expr.out, StateChar)
        assert isinstance(expr.out.out, StateChar)
        assert isinstance(expr.out.out.out, StateMatch)
예제 #5
0
    def test_build_group_or_between_chars(self):
        expr = compile_re('x(aa|bb)x')
        # xaax
        assert isinstance(expr, StateChar)
        assert expr.start == ord('x')

        # |
        node = expr.out
        assert isinstance(node, StateSplit)

        # aax
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('x')
        assert isinstance(node.out, StateMatch)

        # bbx
        node = expr.out.out2
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('x')
        assert isinstance(node.out, StateMatch)
예제 #6
0
 def test_three_chars_build_expr(self):
     expr = compile_re('abc')
     assert isinstance(expr, StateChar)
     assert expr.start == ord('a')
     assert isinstance(expr.out, StateChar)
     assert expr.out.start == ord('b')
     assert isinstance(expr.out.out, StateChar)
     assert expr.out.out.start == ord('c')
예제 #7
0
 def test_chars_and_dots_build_expr(self):
     expr = compile_re('a.c.', False)
     assert isinstance(expr, StateChar)
     assert expr.start == ord('a')
     assert isinstance(expr.out, StateDot)
     assert isinstance(expr.out.out, StateChar)
     assert expr.out.out.start == ord('c')
     assert isinstance(expr.out.out.out, StateDot)
     assert isinstance(expr.out.out.out.out, StateMatch)
예제 #8
0
 def test_chars_and_special_a_build_expr(self):
     expr = compile_re('%aa%a', False)
     assert isinstance(expr, StateCharRange)
     assert expr.start == ord('A')
     assert expr.stop == ord('z')
     assert isinstance(expr.out, StateChar)
     assert expr.out.stop == ord('a')
     assert isinstance(expr.out.out, StateCharRange)
     assert expr.out.out.start == ord('A')
     assert expr.out.out.stop == ord('z')
     assert isinstance(expr.out.out.out, StateMatch)
예제 #9
0
파일: string.py 프로젝트: magniff/luna
def handle_args(args):
    s = args[0].s_val
    start = 0
    plain = False
    expr = compile_re(args[1].s_val, plain)

    if len(args) > 2:
        start = args[2].n_val - 1
        if len(args) > 3:
            w_pri = args[3]
            assert isinstance(w_pri, W_Pri)
            plain = w_pri.is_true()
    return s, expr, start, plain
예제 #10
0
    def test_build_expr_simple_or(self):
        expr = compile_re('a|b', False)

        # |
        assert isinstance(expr, StateSplit)

        # a
        node = expr.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')
        assert isinstance(node.out, StateMatch)

        # b
        node = expr.out2
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')
        assert isinstance(node.out, StateMatch)
예제 #11
0
파일: string.py 프로젝트: magniff/luna
def method_gsub(args):
    s = args[0].s_val
    pattern = args[1].s_val
    replace = args[2].s_val
    expr = compile_re(pattern, False)
    sublist = []
    last_stop = 0
    for m_start, m_stop in find2(expr, s, 0):
        if (m_start, m_stop) == (-1, -1):
            return [W_Str(s)]
        else:
            sublist.append(s[last_stop:m_start-1])
            sublist.append(replace)
            last_stop = m_stop
    sublist.append(s[last_stop:])
    res = "".join(sublist)
    return [W_Str(res)]
예제 #12
0
    def test_build_group_or(self):
        expr = compile_re('(aa|bb)', False)

        # |
        assert isinstance(expr, StateSplit)

        # aa
        node = expr.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('a')
        assert isinstance(node.out, StateMatch)

        # bb
        node = expr.out2
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')
        node = node.out
        assert isinstance(node, StateChar)
        assert node.stop == ord('b')
        assert isinstance(node.out, StateMatch)
예제 #13
0
 def test_simple_or(self):
     expr = compile_re('(aa|bb)')
     result = find2(expr, 'xyzabbaab', 0)
     assert list(result) == [(5, 6), (7, 8)]
예제 #14
0
 def test_grouped_or_between_chars(self):
     expr = compile_re('x(aa|bb)x')
     result = find2(expr, 'axaaxaxbxbbxa', 0)
     assert list(result) == [(2, 5), (9, 12)]
예제 #15
0
 def test_chained_grouped_or_match(self):
     expr = compile_re('x(aa|bb)(cc|dd)x')
     result = find2(expr, 'axaaddaxbbccxxaacx', 0)
     assert list(result) == [(8, 13)]
예제 #16
0
 def test_escape_percent_build_expr(self):
     expr = compile_re('%%', False)
     assert isinstance(expr, StateChar)
     assert expr.start == ord('%')
     assert isinstance(expr.out, StateMatch)
예제 #17
0
 def test_chained_grouped_or_no_match(self):
     expr = compile_re('x(aa|bb)(cc|dd)x')
     result = find2(expr, 'xaaccddxxaaddddxxaacc', 0)
     assert list(result) == [(-1, -1)]
예제 #18
0
 def test_simple_plus(self):
     expr = compile_re('a+')
     result = find2(expr, 'bxaaaabak', 0)
     assert list(result) == [(3, 6), (8, 8)]
예제 #19
0
 def test_grouped_star_between_chars_no_match(self):
     expr = compile_re('x(ab)*x')
     result = find2(expr, 'ababxabababab', 0)
     assert list(result) == [(-1, -1)]
예제 #20
0
 def test_build_expr_invalid_special_char(self):
     with pytest.raises(RuntimeError):
         compile_re('%,')
예제 #21
0
 def test_grouped_star_and_or_match(self):
     expr = compile_re('x((aa)*|(bb)*)x')
     result = find2(expr, 'xaaaaaaxxx', 0)
     assert list(result) == [(1, 8), (9, 10)]
예제 #22
0
 def test_or_repetition(self):
     expr = compile_re('(aa|bb){2}')
     result = find2(expr, 'xabbxaaaaxjkbbajbbaal', 0)
     assert list(result) == [(6, 9), (17, 20)]
예제 #23
0
 def test_grouped_plus(self):
     expr = compile_re('(a|b)+c')
     result = find2(expr, 'xxcaababcvbc', 0)
     assert list(result) == [(4, 9), (11, 12)]
예제 #24
0
 def test_grouped_star(self):
     expr = compile_re('(ab)*')
     result = find2(expr, 'ababababab', 0)
     assert list(result) == [(1, 10)]
예제 #25
0
 def test_match_evil(self):
     expr = compile_re('(a|b)*a(a|b){5}a(a|b)*')
     result = find2(expr, 'aaaababababba', 0)
     assert list(result) == [(1, 13)]
예제 #26
0
 def test_grouped_star_and_or_no_match(self):
     expr = compile_re('x((aa)*|(bb)*)x')
     result = find2(expr, 'xaaaaaxbxabxbbbb', 0)
     assert list(result) == [(-1, -1)]
예제 #27
0
 def test_build_expr_misplaced_star(self):
     with pytest.raises(RuntimeError):
         compile_re('*')
예제 #28
0
 def test_match_evil_no_match(self):
     expr = compile_re('(a|b)*a(a|b){5}a(a|b)*')
     result = find2(expr, 'aaaaaaxbbbbaaaaabbbbbbb', 0)
     assert list(result) == [(-1, -1)]
예제 #29
0
 def test_build_expr_misplaced_percent_3(self):
     with pytest.raises(RuntimeError):
         compile_re('a%')
예제 #30
0
 def test_single_char_build_expr(self):
     expr = compile_re('a')
     assert isinstance(expr, StateChar)
     assert expr.start == ord('a')
     assert expr.stop == ord('a')