| 1 |
from rpython.rlib.rsre import rsre_char, rsre_core |
| 2 |
from rpython.rlib.rsre.rsre_constants import SRE_FLAG_LOCALE, SRE_FLAG_UNICODE |
| 3 |
|
| 4 |
def setup_module(mod): |
| 5 |
from rpython.rlib.unicodedata import unicodedb |
| 6 |
rsre_char.set_unicode_db(unicodedb) |
| 7 |
|
| 8 |
|
| 9 |
def check_charset(pattern, idx, char): |
| 10 |
p = rsre_core.CompiledPattern(pattern, 0) |
| 11 |
return rsre_char.check_charset(Ctx(p), p, idx, char) |
| 12 |
|
| 13 |
|
| 14 |
UPPER_PI = 0x3a0 |
| 15 |
LOWER_PI = 0x3c0 |
| 16 |
INDIAN_DIGIT = 0x966 |
| 17 |
ROMAN_NUMERAL = 0x2165 |
| 18 |
FULLWIDTH_DIGIT = 0xff10 |
| 19 |
CIRCLED_NUMBER = 0x32b4 |
| 20 |
DINGBAT_CIRCLED = 0x2781 |
| 21 |
EM_SPACE = 0x2001 |
| 22 |
LINE_SEP = 0x2028 |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
def test_getlower(): |
| 27 |
assert rsre_char.getlower(ord('A'), 0) == ord('a') |
| 28 |
assert rsre_char.getlower(ord('2'), 0) == ord('2') |
| 29 |
assert rsre_char.getlower(10, 0) == 10 |
| 30 |
assert rsre_char.getlower(UPPER_PI, 0) == UPPER_PI |
| 31 |
|
| 32 |
assert rsre_char.getlower(ord('A'), SRE_FLAG_UNICODE) == ord('a') |
| 33 |
assert rsre_char.getlower(ord('2'), SRE_FLAG_UNICODE) == ord('2') |
| 34 |
assert rsre_char.getlower(10, SRE_FLAG_UNICODE) == 10 |
| 35 |
assert rsre_char.getlower(UPPER_PI, SRE_FLAG_UNICODE) == LOWER_PI |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
assert rsre_char.getlower(UPPER_PI, SRE_FLAG_LOCALE) == UPPER_PI |
| 40 |
assert rsre_char.getlower(UPPER_PI, SRE_FLAG_LOCALE | SRE_FLAG_UNICODE) \ |
| 41 |
== UPPER_PI |
| 42 |
assert rsre_char.getlower(2**30, SRE_FLAG_UNICODE) == 2 ** 30 |
| 43 |
|
| 44 |
def test_getupper(): |
| 45 |
assert rsre_char.getupper(ord('A'), 0) == ord('A') |
| 46 |
assert rsre_char.getupper(ord('b'), 0) == ord('B') |
| 47 |
assert rsre_char.getupper(10, 0) == 10 |
| 48 |
assert rsre_char.getupper(LOWER_PI, 0) == LOWER_PI |
| 49 |
|
| 50 |
assert rsre_char.getupper(ord('a'), SRE_FLAG_UNICODE) == ord('A') |
| 51 |
assert rsre_char.getupper(ord('2'), SRE_FLAG_UNICODE) == ord('2') |
| 52 |
assert rsre_char.getupper(10, SRE_FLAG_UNICODE) == 10 |
| 53 |
assert rsre_char.getupper(LOWER_PI, SRE_FLAG_UNICODE) == UPPER_PI |
| 54 |
|
| 55 |
assert rsre_char.getupper(LOWER_PI, SRE_FLAG_LOCALE) == LOWER_PI |
| 56 |
assert rsre_char.getupper(LOWER_PI, SRE_FLAG_LOCALE | SRE_FLAG_UNICODE) \ |
| 57 |
== LOWER_PI |
| 58 |
assert rsre_char.getupper(2**30, SRE_FLAG_UNICODE) == 2 ** 30 |
| 59 |
|
| 60 |
|
| 61 |
def test_getupper_getlower_unicode_ascii_shortcut(): |
| 62 |
from rpython.rlib.unicodedata import unicodedb |
| 63 |
try: |
| 64 |
rsre_char.set_unicode_db(None) |
| 65 |
for i in range(128): |
| 66 |
|
| 67 |
rsre_char.getlower(i, SRE_FLAG_UNICODE) |
| 68 |
rsre_char.getupper(i, SRE_FLAG_UNICODE) |
| 69 |
finally: |
| 70 |
rsre_char.set_unicode_db(unicodedb) |
| 71 |
|
| 72 |
|
| 73 |
def test_is_word(): |
| 74 |
assert rsre_char.is_word(ord('A')) |
| 75 |
assert rsre_char.is_word(ord('_')) |
| 76 |
assert not rsre_char.is_word(UPPER_PI) |
| 77 |
assert not rsre_char.is_word(LOWER_PI) |
| 78 |
assert not rsre_char.is_word(ord(',')) |
| 79 |
|
| 80 |
assert rsre_char.is_uni_word(ord('A')) |
| 81 |
assert rsre_char.is_uni_word(ord('_')) |
| 82 |
assert rsre_char.is_uni_word(UPPER_PI) |
| 83 |
assert rsre_char.is_uni_word(LOWER_PI) |
| 84 |
assert not rsre_char.is_uni_word(ord(',')) |
| 85 |
assert not rsre_char.is_uni_word(2**30) |
| 86 |
|
| 87 |
def test_category(): |
| 88 |
from rpython.rlib.rsre.rpy.sre_constants import CHCODES |
| 89 |
cat = rsre_char.category_dispatch |
| 90 |
|
| 91 |
assert cat(CHCODES["category_digit"], ord('1')) |
| 92 |
assert not cat(CHCODES["category_digit"], ord('a')) |
| 93 |
assert not cat(CHCODES["category_digit"], INDIAN_DIGIT) |
| 94 |
|
| 95 |
assert not cat(CHCODES["category_not_digit"], ord('1')) |
| 96 |
assert cat(CHCODES["category_not_digit"], ord('a')) |
| 97 |
assert cat(CHCODES["category_not_digit"], INDIAN_DIGIT) |
| 98 |
|
| 99 |
assert not cat(CHCODES["category_space"], ord('1')) |
| 100 |
assert not cat(CHCODES["category_space"], ord('a')) |
| 101 |
assert cat(CHCODES["category_space"], ord(' ')) |
| 102 |
assert cat(CHCODES["category_space"], ord('\n')) |
| 103 |
assert cat(CHCODES["category_space"], ord('\t')) |
| 104 |
assert cat(CHCODES["category_space"], ord('\r')) |
| 105 |
assert cat(CHCODES["category_space"], ord('\v')) |
| 106 |
assert cat(CHCODES["category_space"], ord('\f')) |
| 107 |
assert not cat(CHCODES["category_space"], EM_SPACE) |
| 108 |
|
| 109 |
assert cat(CHCODES["category_not_space"], ord('1')) |
| 110 |
assert cat(CHCODES["category_not_space"], ord('a')) |
| 111 |
assert not cat(CHCODES["category_not_space"], ord(' ')) |
| 112 |
assert not cat(CHCODES["category_not_space"], ord('\n')) |
| 113 |
assert not cat(CHCODES["category_not_space"], ord('\t')) |
| 114 |
assert not cat(CHCODES["category_not_space"], ord('\r')) |
| 115 |
assert not cat(CHCODES["category_not_space"], ord('\v')) |
| 116 |
assert not cat(CHCODES["category_not_space"], ord('\f')) |
| 117 |
assert cat(CHCODES["category_not_space"], EM_SPACE) |
| 118 |
|
| 119 |
assert cat(CHCODES["category_word"], ord('l')) |
| 120 |
assert cat(CHCODES["category_word"], ord('4')) |
| 121 |
assert cat(CHCODES["category_word"], ord('_')) |
| 122 |
assert not cat(CHCODES["category_word"], ord(' ')) |
| 123 |
assert not cat(CHCODES["category_word"], ord('\n')) |
| 124 |
assert not cat(CHCODES["category_word"], LOWER_PI) |
| 125 |
|
| 126 |
assert not cat(CHCODES["category_not_word"], ord('l')) |
| 127 |
assert not cat(CHCODES["category_not_word"], ord('4')) |
| 128 |
assert not cat(CHCODES["category_not_word"], ord('_')) |
| 129 |
assert cat(CHCODES["category_not_word"], ord(' ')) |
| 130 |
assert cat(CHCODES["category_not_word"], ord('\n')) |
| 131 |
assert cat(CHCODES["category_not_word"], LOWER_PI) |
| 132 |
|
| 133 |
assert cat(CHCODES["category_linebreak"], ord('\n')) |
| 134 |
assert not cat(CHCODES["category_linebreak"], ord(' ')) |
| 135 |
assert not cat(CHCODES["category_linebreak"], ord('s')) |
| 136 |
assert not cat(CHCODES["category_linebreak"], ord('\r')) |
| 137 |
assert not cat(CHCODES["category_linebreak"], LINE_SEP) |
| 138 |
|
| 139 |
assert cat(CHCODES["category_uni_linebreak"], ord('\n')) |
| 140 |
assert not cat(CHCODES["category_uni_linebreak"], ord(' ')) |
| 141 |
assert not cat(CHCODES["category_uni_linebreak"], ord('s')) |
| 142 |
assert cat(CHCODES["category_uni_linebreak"], LINE_SEP) |
| 143 |
|
| 144 |
assert not cat(CHCODES["category_not_linebreak"], ord('\n')) |
| 145 |
assert cat(CHCODES["category_not_linebreak"], ord(' ')) |
| 146 |
assert cat(CHCODES["category_not_linebreak"], ord('s')) |
| 147 |
assert cat(CHCODES["category_not_linebreak"], ord('\r')) |
| 148 |
assert cat(CHCODES["category_not_linebreak"], LINE_SEP) |
| 149 |
|
| 150 |
assert not cat(CHCODES["category_uni_not_linebreak"], ord('\n')) |
| 151 |
assert cat(CHCODES["category_uni_not_linebreak"], ord(' ')) |
| 152 |
assert cat(CHCODES["category_uni_not_linebreak"], ord('s')) |
| 153 |
assert not cat(CHCODES["category_uni_not_linebreak"], LINE_SEP) |
| 154 |
|
| 155 |
assert cat(CHCODES["category_uni_digit"], INDIAN_DIGIT) |
| 156 |
assert cat(CHCODES["category_uni_digit"], FULLWIDTH_DIGIT) |
| 157 |
assert not cat(CHCODES["category_uni_digit"], ROMAN_NUMERAL) |
| 158 |
assert not cat(CHCODES["category_uni_digit"], CIRCLED_NUMBER) |
| 159 |
assert not cat(CHCODES["category_uni_digit"], DINGBAT_CIRCLED) |
| 160 |
|
| 161 |
assert not cat(CHCODES["category_uni_not_digit"], INDIAN_DIGIT) |
| 162 |
assert not cat(CHCODES["category_uni_not_digit"], FULLWIDTH_DIGIT) |
| 163 |
assert cat(CHCODES["category_uni_not_digit"], ROMAN_NUMERAL) |
| 164 |
assert cat(CHCODES["category_uni_not_digit"], CIRCLED_NUMBER) |
| 165 |
assert cat(CHCODES["category_uni_not_digit"], DINGBAT_CIRCLED) |
| 166 |
|
| 167 |
|
| 168 |
class Ctx: |
| 169 |
def __init__(self, pattern): |
| 170 |
self.pattern = pattern |
| 171 |
|
| 172 |
def test_general_category(): |
| 173 |
from rpython.rlib.unicodedata import unicodedb |
| 174 |
|
| 175 |
for cat, positive, negative in [('L', u'aZ\xe9', u'. ?'), |
| 176 |
('P', u'.?', u'aZ\xe9 ')]: |
| 177 |
pat_pos = [70, ord(cat), 0] |
| 178 |
pat_neg = [70, ord(cat) | 0x80, 0] |
| 179 |
for c in positive: |
| 180 |
assert unicodedb.category(ord(c)).startswith(cat) |
| 181 |
assert check_charset(pat_pos, 0, ord(c)) |
| 182 |
assert not check_charset(pat_neg, 0, ord(c)) |
| 183 |
for c in negative: |
| 184 |
assert not unicodedb.category(ord(c)).startswith(cat) |
| 185 |
assert not check_charset(pat_pos, 0, ord(c)) |
| 186 |
assert check_charset(pat_neg, 0, ord(c)) |
| 187 |
|
| 188 |
def cat2num(cat): |
| 189 |
return ord(cat[0]) | (ord(cat[1]) << 8) |
| 190 |
|
| 191 |
for cat, positive, negative in [('Lu', u'A', u'z\xe9 '), |
| 192 |
('Ll', u'z\xe9', u'A \n')]: |
| 193 |
pat_pos = [70, cat2num(cat), 0] |
| 194 |
pat_neg = [70, cat2num(cat) | 0x80, 0] |
| 195 |
for c in positive: |
| 196 |
assert unicodedb.category(ord(c)) == cat |
| 197 |
assert check_charset(pat_pos, 0, ord(c)) |
| 198 |
assert not check_charset(pat_neg, 0, ord(c)) |
| 199 |
for c in negative: |
| 200 |
assert unicodedb.category(ord(c)) != cat |
| 201 |
assert not check_charset(pat_pos, 0, ord(c)) |
| 202 |
assert check_charset(pat_neg, 0, ord(c)) |
| 203 |
|
| 204 |
|
| 205 |
pat = [70, cat2num('Lu'), 70, cat2num('Ll'), 70, cat2num('Lt'), 0] |
| 206 |
assert check_charset(pat, 0, 65) |
| 207 |
assert check_charset(pat, 0, 99) |
| 208 |
assert not check_charset(pat, 0, 688) |
| 209 |
assert not check_charset(pat, 0, 5870) |
| 210 |
|
| 211 |
def test_iscased(): |
| 212 |
assert rsre_char.iscased_ascii(65) |
| 213 |
assert rsre_char.iscased_ascii(100) |
| 214 |
assert rsre_char.iscased_ascii(64) is False |
| 215 |
assert rsre_char.iscased_ascii(126) is False |
| 216 |
assert rsre_char.iscased_ascii(1260) is False |
| 217 |
assert rsre_char.iscased_ascii(12600) is False |
| 218 |
for i in range(65536): |
| 219 |
assert rsre_char.iscased_unicode(i) == ( |
| 220 |
rsre_char.getlower_unicode(i) != i or |
| 221 |
rsre_char.getupper_unicode(i) != i) |