| 1 |
import sys, random |
| 2 |
from rpython.rlib import debug |
| 3 |
from rpython.rlib.rsre.rsre_core import _adjust, match_context, search_context |
| 4 |
from rpython.rlib.rsre.rsre_core import MODE_FULL |
| 5 |
from rpython.rlib.rsre.rsre_core import StrMatchContext, EndOfString |
| 6 |
|
| 7 |
|
| 8 |
class Position(object): |
| 9 |
def __init__(self, p): |
| 10 |
assert isinstance(p, int) |
| 11 |
if p < 0: |
| 12 |
raise debug.NegativeArgumentNotAllowed( |
| 13 |
"making a Position with byte index %r" % p) |
| 14 |
self._p = p |
| 15 |
def __repr__(self): |
| 16 |
return '<Position %d>' % (self._p) |
| 17 |
def __cmp__(self, other): |
| 18 |
if isinstance(other, Position): |
| 19 |
return cmp(self._p, other._p) |
| 20 |
if type(other) is int and other == -1: |
| 21 |
return cmp(self._p, -1) |
| 22 |
raise TypeError("cannot compare %r with %r" % (self, other)) |
| 23 |
|
| 24 |
|
| 25 |
class MatchContextForTests(StrMatchContext): |
| 26 |
"""Concrete subclass for matching in a plain string, tweaked for tests""" |
| 27 |
|
| 28 |
ZERO = Position(0) |
| 29 |
|
| 30 |
def next(self, position): |
| 31 |
assert isinstance(position, Position) |
| 32 |
return Position(position._p + 1) |
| 33 |
next_indirect = next |
| 34 |
|
| 35 |
def prev(self, position): |
| 36 |
assert isinstance(position, Position) |
| 37 |
if position._p == 0: |
| 38 |
raise EndOfString |
| 39 |
return Position(position._p - 1) |
| 40 |
prev_indirect = prev |
| 41 |
|
| 42 |
def next_n(self, position, n, end_position): |
| 43 |
assert isinstance(position, Position) |
| 44 |
assert isinstance(end_position, Position) |
| 45 |
assert position._p <= end_position._p |
| 46 |
r = position._p + n |
| 47 |
if r > end_position._p: |
| 48 |
raise EndOfString |
| 49 |
return Position(r) |
| 50 |
|
| 51 |
def prev_n(self, position, n, start_position): |
| 52 |
assert isinstance(position, Position) |
| 53 |
assert isinstance(start_position, Position) |
| 54 |
assert position._p >= start_position._p |
| 55 |
r = position._p - n |
| 56 |
if r < start_position._p: |
| 57 |
raise EndOfString |
| 58 |
return Position(r) |
| 59 |
|
| 60 |
def _real_pos(self, position): |
| 61 |
if type(position) is int and position == -1: |
| 62 |
return -1 |
| 63 |
assert isinstance(position, Position) |
| 64 |
return position._p |
| 65 |
|
| 66 |
def group(self, groupnum=0): |
| 67 |
frm, to = self.span(groupnum) |
| 68 |
if self.ZERO <= frm <= to: |
| 69 |
return self._string[self._real_pos(frm):self._real_pos(to)] |
| 70 |
else: |
| 71 |
return None |
| 72 |
|
| 73 |
def str(self, position): |
| 74 |
assert isinstance(position, Position) |
| 75 |
return ord(self._string[position._p]) |
| 76 |
|
| 77 |
def debug_check_pos(self, position): |
| 78 |
assert isinstance(position, Position) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
def maximum_distance(self, position_low, position_high): |
| 90 |
"""Return an estimate. The real value may be lower.""" |
| 91 |
assert isinstance(position_low, Position) |
| 92 |
assert isinstance(position_high, Position) |
| 93 |
return position_high._p - position_low._p + random.randrange(0, 10) |
| 94 |
|
| 95 |
def bytes_difference(self, position1, position2): |
| 96 |
assert isinstance(position1, Position) |
| 97 |
assert isinstance(position2, Position) |
| 98 |
return position1._p - position2._p |
| 99 |
|
| 100 |
def get_single_byte(self, base_position, index): |
| 101 |
assert isinstance(base_position, Position) |
| 102 |
assert isinstance(index, int) |
| 103 |
return ord(self._string[base_position._p + index]) |
| 104 |
|
| 105 |
def go_forward_by_bytes(self, base_position, index): |
| 106 |
assert isinstance(base_position, Position) |
| 107 |
assert isinstance(index, int) |
| 108 |
return Position(base_position._p + index) |
| 109 |
|
| 110 |
|
| 111 |
def match(pattern, string, start=0, end=sys.maxint, fullmatch=False): |
| 112 |
start, end = _adjust(start, end, len(string)) |
| 113 |
start = Position(start) |
| 114 |
end = Position(end) |
| 115 |
ctx = MatchContextForTests(string, start, end) |
| 116 |
if fullmatch: |
| 117 |
ctx.match_mode = MODE_FULL |
| 118 |
if match_context(ctx, pattern): |
| 119 |
return ctx |
| 120 |
else: |
| 121 |
return None |
| 122 |
|
| 123 |
def fullmatch(pattern, string, start=0, end=sys.maxint): |
| 124 |
return match(pattern, string, start, end, fullmatch=True) |
| 125 |
|
| 126 |
def search(pattern, string, start=0, end=sys.maxint): |
| 127 |
start, end = _adjust(start, end, len(string)) |
| 128 |
start = Position(start) |
| 129 |
end = Position(end) |
| 130 |
ctx = MatchContextForTests(string, start, end) |
| 131 |
if search_context(ctx, pattern): |
| 132 |
return ctx |
| 133 |
else: |
| 134 |
return None |