def test_star_2(self): expr = StateSplit(None, StateMatch()) expr.out = StateChar('a', expr) result = find2(expr, 'aaaaaaaabacbca', 0) assert list(result) == [ (1, 8), (9, 8), (10, 10), (11, 10), (12, 11), (13, 12), (14, 14) ]
def method_find(args): s, expr, start, plain = handle_args(args) matches = next(find2(expr, s, start)) if matches == (-1, -1): return [W_Pri(0)] else: return [W_Num(matches[0]), W_Num(matches[1])]
def method_match(args): s, expr, start, plain = handle_args(args) start_i, stop_i = next(find2(expr, s, start)) if (start_i, stop_i) == (-1, -1): return [W_Pri(0)] else: return [W_Str(s[start_i-1:stop_i])]
def test_star_1(self): expr = StateSplit(None, StateMatch()) expr.out = StateChar('c', expr) result = find2(expr, 'aaaabacccca', 0) assert list(result) == [ (1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 10), (11, 10) ]
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)]
def test_single_char_more_matches(self): expr = StateChar('c', StateMatch()) result = find2(expr, 'xyzaaaccaa', 0) assert list(result) == [(7, 7), (8, 8)]
def test_two_chars_no_matches(self): expr = StateChar('a', StateChar('b', StateMatch())) result = find2(expr, 'acbaaubbbbb', 0) assert list(result) == [(-1, -1)]
def test_simple_or(self): expr = compile_re('(aa|bb)') result = find2(expr, 'xyzabbaab', 0) assert list(result) == [(5, 6), (7, 8)]
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)]
def test_three_chars_one_matches_offset(self): expr = StateChar('a', StateChar('b', StateChar('c', StateMatch()))) result = find2(expr, 'abcjjjabc', 4) assert list(result) == [(7, 9)]
def test_three_chars_big_negative_offset_match(self): expr = StateChar('a', StateChar('b', StateChar('c', StateMatch()))) result = find2(expr, 'abcjjjabc', -100) assert list(result) == [(1, 3), (7, 9)]
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)]
def test_three_chars_one_match(self): expr = StateChar('a', StateChar('b', StateChar('c', StateMatch()))) result = find2(expr, 'ccabababccbababacccbaccabbbc', 0) assert list(result) == [(7, 9)]
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)]
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)]
def test_single_char_one_match(self): expr = StateChar('c', StateMatch()) result = find2(expr, 'asdasdxcz', 0) assert list(result) == [(8, 8)]
def test_grouped_star(self): expr = compile_re('(ab)*') result = find2(expr, 'ababababab', 0) assert list(result) == [(1, 10)]
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)]
def test_two_chars_one_match(self): expr = StateChar('a', StateChar('b', StateMatch())) result = find2(expr, 'ccvvvbbajbajbabb', 0) assert list(result) == [(14, 15)]
def test_simple_plus(self): expr = compile_re('a+') result = find2(expr, 'bxaaaabak', 0) assert list(result) == [(3, 6), (8, 8)]
def tests_find_two_chars_matches(self): expr = StateChar('a', StateChar('b', StateMatch())) result = find2(expr, 'baaaabbacaabbcc', 0) assert list(result) == [(5, 6), (11, 12)]
def test_grouped_plus(self): expr = compile_re('(a|b)+c') result = find2(expr, 'xxcaababcvbc', 0) assert list(result) == [(4, 9), (11, 12)]
def test_three_chars_two_matches(self): expr = StateChar('a', StateChar('b', StateChar('c', StateMatch()))) result = find2(expr, 'babcccabababccbababacccbaccabbbc', 0) assert list(result) == [(2, 4), (11, 13)]
def test_single_char_no_match(self): expr = StateChar('c', StateMatch()) result = find2(expr, 'xyz', 0) assert list(result) == [(-1, -1)]
def test_three_chars_negative_offset_no_match(self): expr = StateChar('a', StateChar('b', StateChar('c', StateMatch()))) result = find2(expr, 'abcjjjabc', -2) assert list(result) == [(-1, -1)]
def test_star_between_chars_match_star(self): star = StateSplit(None, StateChar('c', StateMatch())) star.out = StateChar('b', star) expr = StateChar('a', star) result = find2(expr, 'xaabbbbbcjjjabcxalcac', 0) assert list(result) == [(3, 9), (13, 15), (20, 21)]
def test_three_chars_big_offset(self): expr = StateChar('a', StateChar('b', StateChar('c', StateMatch()))) result = find2(expr, 'abcjjjabc', 100) assert list(result) == [(-1, -1)]
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)]
def test_or_repetition(self): expr = compile_re('(aa|bb){2}') result = find2(expr, 'xabbxaaaaxjkbbajbbaal', 0) assert list(result) == [(6, 9), (17, 20)]
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)]
def test_star_between_chars_star_match_end(self): star = StateSplit(None, StateMatch()) star.out = StateChar('b', star) expr = StateChar('a', star) result = find2(expr, 'acjjjabcabbbbb', 0) assert list(result) == [(1, 1), (6, 7), (9, 14)]
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)]