| 1 |
import sys |
| 2 |
from rpython.rlib.debug import check_nonneg |
| 3 |
from rpython.rlib.rsre.rsre_core import AbstractMatchContext, EndOfString |
| 4 |
from rpython.rlib.rsre import rsre_char |
| 5 |
from rpython.rlib.objectmodel import we_are_translated |
| 6 |
from rpython.rlib import rutf8 |
| 7 |
|
| 8 |
|
| 9 |
class Utf8MatchContext(AbstractMatchContext): |
| 10 |
"""A context that matches unicode, but encoded in a utf8 string. |
| 11 |
Be careful because most positions taken by, handled in, and returned |
| 12 |
by this class are expressed in *bytes*, not in characters. |
| 13 |
""" |
| 14 |
|
| 15 |
def __init__(self, utf8string, match_start, end): |
| 16 |
AbstractMatchContext.__init__(self, match_start, end) |
| 17 |
self._utf8 = utf8string |
| 18 |
|
| 19 |
def str(self, index): |
| 20 |
check_nonneg(index) |
| 21 |
return rutf8.codepoint_at_pos(self._utf8, index) |
| 22 |
|
| 23 |
def get_single_byte(self, base_position, index): |
| 24 |
return self._utf8[base_position + index] |
| 25 |
|
| 26 |
def next(self, position): |
| 27 |
return rutf8.next_codepoint_pos(self._utf8, position) |
| 28 |
next_indirect = next |
| 29 |
|
| 30 |
def prev(self, position): |
| 31 |
if position <= 0: |
| 32 |
raise EndOfString |
| 33 |
position = rutf8.prev_codepoint_pos(self._utf8, position) |
| 34 |
assert position >= 0 |
| 35 |
return position |
| 36 |
prev_indirect = prev |
| 37 |
|
| 38 |
def next_n(self, position, n, end_position): |
| 39 |
i = 0 |
| 40 |
|
| 41 |
while i < n: |
| 42 |
if position >= end_position: |
| 43 |
raise EndOfString |
| 44 |
position = rutf8.next_codepoint_pos(self._utf8, position) |
| 45 |
i += 1 |
| 46 |
return position |
| 47 |
|
| 48 |
def prev_n(self, position, n, start_position): |
| 49 |
i = 0 |
| 50 |
|
| 51 |
while i < n: |
| 52 |
if position <= start_position: |
| 53 |
raise EndOfString |
| 54 |
position = rutf8.prev_codepoint_pos(self._utf8, position) |
| 55 |
i += 1 |
| 56 |
assert position >= 0 |
| 57 |
return position |
| 58 |
|
| 59 |
def debug_check_pos(self, position): |
| 60 |
if we_are_translated(): |
| 61 |
return |
| 62 |
if position == len(self._utf8): |
| 63 |
return |
| 64 |
assert not (0x80 <= self._utf8[position] < 0xC0) |
| 65 |
|
| 66 |
def maximum_distance(self, position_low, position_high): |
| 67 |
|
| 68 |
return position_high - position_low |
| 69 |
|
| 70 |
|
| 71 |
def make_utf8_ctx(utf8string, bytestart, byteend): |
| 72 |
if bytestart < 0: bytestart = 0 |
| 73 |
elif bytestart > len(utf8string): bytestart = len(utf8string) |
| 74 |
if byteend < 0: byteend = 0 |
| 75 |
elif byteend > len(utf8string): byteend = len(utf8string) |
| 76 |
ctx = Utf8MatchContext(utf8string, bytestart, byteend) |
| 77 |
ctx.debug_check_pos(bytestart) |
| 78 |
ctx.debug_check_pos(byteend) |
| 79 |
return ctx |
| 80 |
|
| 81 |
def utf8search(pattern, utf8string, bytestart=0, byteend=sys.maxint): |
| 82 |
|
| 83 |
|
| 84 |
from rpython.rlib.rsre.rsre_core import search_context |
| 85 |
|
| 86 |
ctx = make_utf8_ctx(utf8string, bytestart, byteend) |
| 87 |
if search_context(ctx, pattern): |
| 88 |
return ctx |
| 89 |
else: |
| 90 |
return None |
| 91 |
|
| 92 |
def utf8match(pattern, utf8string, bytestart=0, byteend=sys.maxint, |
| 93 |
fullmatch=False): |
| 94 |
|
| 95 |
|
| 96 |
from rpython.rlib.rsre.rsre_core import match_context, MODE_FULL |
| 97 |
|
| 98 |
ctx = make_utf8_ctx(utf8string, bytestart, byteend) |
| 99 |
if fullmatch: |
| 100 |
ctx.match_mode = MODE_FULL |
| 101 |
if match_context(ctx, pattern): |
| 102 |
return ctx |
| 103 |
else: |
| 104 |
return None |