| 1 |
from rpython.annotator import model as annmodel |
| 2 |
from rpython.rlib import jit |
| 3 |
from rpython.rtyper import rint |
| 4 |
from rpython.rtyper.error import TyperError |
| 5 |
from rpython.rtyper.lltypesystem.lltype import Signed, Bool, Void, UniChar |
| 6 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 7 |
from rpython.rtyper.rmodel import IteratorRepr, inputconst, Repr |
| 8 |
from rpython.rtyper.rint import IntegerRepr |
| 9 |
from rpython.rtyper.rfloat import FloatRepr |
| 10 |
from rpython.tool.pairtype import pairtype, pair |
| 11 |
|
| 12 |
def str_decode_utf8(s): |
| 13 |
from rpython.rlib.rstring import UnicodeBuilder |
| 14 |
from rpython.rlib import runicode |
| 15 |
|
| 16 |
size = len(s) |
| 17 |
if size == 0: |
| 18 |
return u'' |
| 19 |
|
| 20 |
result = UnicodeBuilder(size) |
| 21 |
pos = 0 |
| 22 |
while pos < size: |
| 23 |
ordch1 = ord(s[pos]) |
| 24 |
|
| 25 |
|
| 26 |
if ordch1 < 0x80: |
| 27 |
result.append(unichr(ordch1)) |
| 28 |
pos += 1 |
| 29 |
continue |
| 30 |
|
| 31 |
n = ord(runicode._utf8_code_length[ordch1 - 0x80]) |
| 32 |
if pos + n > size: |
| 33 |
raise UnicodeDecodeError('utf8', s, pos, pos + 1, |
| 34 |
'whatever') |
| 35 |
if n == 0: |
| 36 |
raise UnicodeDecodeError('utf8', s, pos, pos + 1, |
| 37 |
'whatever') |
| 38 |
elif n == 1: |
| 39 |
assert 0, "ascii should have gone through the fast path" |
| 40 |
|
| 41 |
elif n == 2: |
| 42 |
ordch2 = ord(s[pos+1]) |
| 43 |
if runicode._invalid_byte_2_of_2(ordch2): |
| 44 |
|
| 45 |
raise UnicodeDecodeError('utf8', s, pos, pos + 1, |
| 46 |
'whatever') |
| 47 |
|
| 48 |
result.append(unichr(((ordch1 & 0x1F) << 6) + |
| 49 |
(ordch2 & 0x3F))) |
| 50 |
pos += 2 |
| 51 |
|
| 52 |
elif n == 3: |
| 53 |
ordch2 = ord(s[pos+1]) |
| 54 |
ordch3 = ord(s[pos+2]) |
| 55 |
if (runicode._invalid_byte_2_of_3(ordch1, ordch2, True) or |
| 56 |
runicode._invalid_byte_3_of_3(ordch3)): |
| 57 |
raise UnicodeDecodeError('utf8', s, pos, pos + 1, |
| 58 |
'whatever') |
| 59 |
|
| 60 |
result.append(unichr(((ordch1 & 0x0F) << 12) + |
| 61 |
((ordch2 & 0x3F) << 6) + |
| 62 |
(ordch3 & 0x3F))) |
| 63 |
pos += 3 |
| 64 |
|
| 65 |
elif n == 4: |
| 66 |
ordch2 = ord(s[pos+1]) |
| 67 |
ordch3 = ord(s[pos+2]) |
| 68 |
ordch4 = ord(s[pos+3]) |
| 69 |
if (runicode._invalid_byte_2_of_4(ordch1, ordch2) or |
| 70 |
runicode._invalid_byte_3_of_4(ordch3) or |
| 71 |
runicode._invalid_byte_4_of_4(ordch4)): |
| 72 |
|
| 73 |
raise UnicodeDecodeError('utf8', s, pos, pos + 1, |
| 74 |
'whatever') |
| 75 |
|
| 76 |
c = (((ordch1 & 0x07) << 18) + |
| 77 |
((ordch2 & 0x3F) << 12) + |
| 78 |
((ordch3 & 0x3F) << 6) + |
| 79 |
(ordch4 & 0x3F)) |
| 80 |
if c <= runicode.MAXUNICODE: |
| 81 |
result.append(runicode.UNICHR(c)) |
| 82 |
else: |
| 83 |
|
| 84 |
|
| 85 |
c -= 0x10000 |
| 86 |
|
| 87 |
result.append(unichr(0xD800 + (c >> 10))) |
| 88 |
|
| 89 |
result.append(unichr(0xDC00 + (c & 0x03FF))) |
| 90 |
pos += 4 |
| 91 |
|
| 92 |
return result.build() |
| 93 |
|
| 94 |
|
| 95 |
class AbstractStringRepr(Repr): |
| 96 |
|
| 97 |
@jit.elidable |
| 98 |
def ll_decode_utf8(self, llvalue): |
| 99 |
from rpython.rtyper.annlowlevel import hlstr |
| 100 |
value = hlstr(llvalue) |
| 101 |
assert value is not None |
| 102 |
|
| 103 |
u = str_decode_utf8(value) |
| 104 |
|
| 105 |
return self.ll.llunicode(u) |
| 106 |
|
| 107 |
def _str_reprs(self, hop): |
| 108 |
return hop.args_r[0].repr, hop.args_r[1].repr |
| 109 |
|
| 110 |
def get_ll_eq_function(self): |
| 111 |
return self.ll.ll_streq |
| 112 |
|
| 113 |
def get_ll_hash_function(self): |
| 114 |
return self.ll.ll_strhash |
| 115 |
|
| 116 |
def get_ll_fasthash_function(self): |
| 117 |
return self.ll.ll_strfasthash |
| 118 |
|
| 119 |
def rtype_len(self, hop): |
| 120 |
string_repr = self.repr |
| 121 |
v_str, = hop.inputargs(string_repr) |
| 122 |
return hop.gendirectcall(self.ll.ll_strlen, v_str) |
| 123 |
|
| 124 |
def rtype_bool(self, hop): |
| 125 |
s_str = hop.args_s[0] |
| 126 |
if s_str.can_be_None: |
| 127 |
string_repr = hop.args_r[0].repr |
| 128 |
v_str, = hop.inputargs(string_repr) |
| 129 |
return hop.gendirectcall(self.ll.ll_str_is_true, v_str) |
| 130 |
else: |
| 131 |
|
| 132 |
return super(AbstractStringRepr, self).rtype_bool(hop) |
| 133 |
|
| 134 |
def rtype_method_startswith(self, hop): |
| 135 |
str1_repr = hop.args_r[0].repr |
| 136 |
str2_repr = hop.args_r[1] |
| 137 |
v_str = hop.inputarg(str1_repr, arg=0) |
| 138 |
if str2_repr == str2_repr.char_repr: |
| 139 |
v_value = hop.inputarg(str2_repr.char_repr, arg=1) |
| 140 |
fn = self.ll.ll_startswith_char |
| 141 |
else: |
| 142 |
v_value = hop.inputarg(str2_repr, arg=1) |
| 143 |
fn = self.ll.ll_startswith |
| 144 |
hop.exception_cannot_occur() |
| 145 |
return hop.gendirectcall(fn, v_str, v_value) |
| 146 |
|
| 147 |
def rtype_method_endswith(self, hop): |
| 148 |
str1_repr = hop.args_r[0].repr |
| 149 |
str2_repr = hop.args_r[1] |
| 150 |
v_str = hop.inputarg(str1_repr, arg=0) |
| 151 |
if str2_repr == str2_repr.char_repr: |
| 152 |
v_value = hop.inputarg(str2_repr.char_repr, arg=1) |
| 153 |
fn = self.ll.ll_endswith_char |
| 154 |
else: |
| 155 |
v_value = hop.inputarg(str2_repr, arg=1) |
| 156 |
fn = self.ll.ll_endswith |
| 157 |
hop.exception_cannot_occur() |
| 158 |
return hop.gendirectcall(fn, v_str, v_value) |
| 159 |
|
| 160 |
def rtype_method_find(self, hop, reverse=False): |
| 161 |
|
| 162 |
string_repr = hop.args_r[0].repr |
| 163 |
char_repr = hop.args_r[0].char_repr |
| 164 |
v_str = hop.inputarg(string_repr, arg=0) |
| 165 |
if hop.args_r[1] == char_repr: |
| 166 |
v_value = hop.inputarg(char_repr, arg=1) |
| 167 |
llfn = reverse and self.ll.ll_rfind_char or self.ll.ll_find_char |
| 168 |
else: |
| 169 |
v_value = hop.inputarg(string_repr, arg=1) |
| 170 |
llfn = reverse and self.ll.ll_rfind or self.ll.ll_find |
| 171 |
if hop.nb_args > 2: |
| 172 |
v_start = hop.inputarg(Signed, arg=2) |
| 173 |
if not hop.args_s[2].nonneg: |
| 174 |
raise TyperError("str.%s() start must be proven non-negative" |
| 175 |
% (reverse and 'rfind' or 'find',)) |
| 176 |
else: |
| 177 |
v_start = hop.inputconst(Signed, 0) |
| 178 |
if hop.nb_args > 3: |
| 179 |
v_end = hop.inputarg(Signed, arg=3) |
| 180 |
if not hop.args_s[3].nonneg: |
| 181 |
raise TyperError("str.%s() end must be proven non-negative" |
| 182 |
% (reverse and 'rfind' or 'find',)) |
| 183 |
else: |
| 184 |
v_end = hop.gendirectcall(self.ll.ll_strlen, v_str) |
| 185 |
hop.exception_cannot_occur() |
| 186 |
return hop.gendirectcall(llfn, v_str, v_value, v_start, v_end) |
| 187 |
|
| 188 |
def rtype_method_rfind(self, hop): |
| 189 |
return self.rtype_method_find(hop, reverse=True) |
| 190 |
|
| 191 |
def rtype_method_count(self, hop): |
| 192 |
rstr = hop.args_r[0].repr |
| 193 |
v_str = hop.inputarg(rstr.repr, arg=0) |
| 194 |
if hop.args_r[1] == rstr.char_repr: |
| 195 |
v_value = hop.inputarg(rstr.char_repr, arg=1) |
| 196 |
llfn = self.ll.ll_count_char |
| 197 |
else: |
| 198 |
v_value = hop.inputarg(rstr.repr, arg=1) |
| 199 |
llfn = self.ll.ll_count |
| 200 |
if hop.nb_args > 2: |
| 201 |
v_start = hop.inputarg(Signed, arg=2) |
| 202 |
if not hop.args_s[2].nonneg: |
| 203 |
raise TyperError("str.count() start must be proven non-negative") |
| 204 |
else: |
| 205 |
v_start = hop.inputconst(Signed, 0) |
| 206 |
if hop.nb_args > 3: |
| 207 |
v_end = hop.inputarg(Signed, arg=3) |
| 208 |
if not hop.args_s[3].nonneg: |
| 209 |
raise TyperError("str.count() end must be proven non-negative") |
| 210 |
else: |
| 211 |
v_end = hop.gendirectcall(self.ll.ll_strlen, v_str) |
| 212 |
hop.exception_cannot_occur() |
| 213 |
return hop.gendirectcall(llfn, v_str, v_value, v_start, v_end) |
| 214 |
|
| 215 |
def rtype_method_strip(self, hop, left=True, right=True): |
| 216 |
rstr = hop.args_r[0].repr |
| 217 |
v_str = hop.inputarg(rstr.repr, arg=0) |
| 218 |
args_v = [v_str] |
| 219 |
if len(hop.args_s) == 2: |
| 220 |
if isinstance(hop.args_s[1], annmodel.SomeString): |
| 221 |
v_stripstr = hop.inputarg(rstr.repr, arg=1) |
| 222 |
args_v.append(v_stripstr) |
| 223 |
func = self.ll.ll_strip_multiple |
| 224 |
else: |
| 225 |
v_char = hop.inputarg(rstr.char_repr, arg=1) |
| 226 |
args_v.append(v_char) |
| 227 |
func = self.ll.ll_strip |
| 228 |
else: |
| 229 |
func = self.ll.ll_strip_default |
| 230 |
args_v.append(hop.inputconst(Bool, left)) |
| 231 |
args_v.append(hop.inputconst(Bool, right)) |
| 232 |
hop.exception_is_here() |
| 233 |
return hop.gendirectcall(func, *args_v) |
| 234 |
|
| 235 |
def rtype_method_lstrip(self, hop): |
| 236 |
return self.rtype_method_strip(hop, left=True, right=False) |
| 237 |
|
| 238 |
def rtype_method_rstrip(self, hop): |
| 239 |
return self.rtype_method_strip(hop, left=False, right=True) |
| 240 |
|
| 241 |
def rtype_method_upper(self, hop): |
| 242 |
string_repr = hop.args_r[0].repr |
| 243 |
v_str, = hop.inputargs(string_repr) |
| 244 |
hop.exception_cannot_occur() |
| 245 |
return hop.gendirectcall(self.ll.ll_upper, v_str) |
| 246 |
|
| 247 |
def rtype_method_lower(self, hop): |
| 248 |
string_repr = hop.args_r[0].repr |
| 249 |
v_str, = hop.inputargs(string_repr) |
| 250 |
hop.exception_cannot_occur() |
| 251 |
return hop.gendirectcall(self.ll.ll_lower, v_str) |
| 252 |
|
| 253 |
def rtype_method_isdigit(self, hop): |
| 254 |
string_repr = hop.args_r[0].repr |
| 255 |
[v_str] = hop.inputargs(string_repr) |
| 256 |
hop.exception_cannot_occur() |
| 257 |
return hop.gendirectcall(self.ll.ll_isdigit, v_str) |
| 258 |
|
| 259 |
def rtype_method_isalpha(self, hop): |
| 260 |
string_repr = hop.args_r[0].repr |
| 261 |
[v_str] = hop.inputargs(string_repr) |
| 262 |
hop.exception_cannot_occur() |
| 263 |
return hop.gendirectcall(self.ll.ll_isalpha, v_str) |
| 264 |
|
| 265 |
def rtype_method_isalnum(self, hop): |
| 266 |
string_repr = hop.args_r[0].repr |
| 267 |
[v_str] = hop.inputargs(string_repr) |
| 268 |
hop.exception_cannot_occur() |
| 269 |
return hop.gendirectcall(self.ll.ll_isalnum, v_str) |
| 270 |
|
| 271 |
def _list_length_items(self, hop, v_lst, LIST): |
| 272 |
"""Return two Variables containing the length and items of a |
| 273 |
list. Need to be overriden because it is typesystem-specific.""" |
| 274 |
raise NotImplementedError |
| 275 |
|
| 276 |
def rtype_method_join(self, hop): |
| 277 |
from rpython.rtyper.lltypesystem.rlist import BaseListRepr |
| 278 |
from rpython.rtyper.lltypesystem.rstr import char_repr, unichar_repr |
| 279 |
hop.exception_cannot_occur() |
| 280 |
rstr = hop.args_r[0] |
| 281 |
if hop.s_result.is_constant(): |
| 282 |
return inputconst(rstr.repr, hop.s_result.const) |
| 283 |
r_lst = hop.args_r[1] |
| 284 |
if not isinstance(r_lst, BaseListRepr): |
| 285 |
raise TyperError("string.join of non-list: %r" % r_lst) |
| 286 |
v_str, v_lst = hop.inputargs(rstr.repr, r_lst) |
| 287 |
v_length, v_items = self._list_length_items(hop, v_lst, r_lst.lowleveltype) |
| 288 |
|
| 289 |
if hop.args_s[0].is_constant() and hop.args_s[0].const == '': |
| 290 |
v_tp = hop.inputconst(Void, self.lowleveltype) |
| 291 |
if r_lst.external_item_repr == rstr.repr: |
| 292 |
llfn = self.ll.ll_join_strs |
| 293 |
return hop.gendirectcall(llfn, v_length, v_items, v_tp) |
| 294 |
elif (r_lst.item_repr == char_repr or |
| 295 |
r_lst.item_repr == unichar_repr): |
| 296 |
return hop.gendirectcall(self.ll.ll_join_chars, v_length, |
| 297 |
v_items, v_tp) |
| 298 |
else: |
| 299 |
raise TyperError("''.join() of non-string list: %r" % r_lst) |
| 300 |
else: |
| 301 |
if r_lst.external_item_repr == rstr.repr: |
| 302 |
llfn = self.ll.ll_join |
| 303 |
else: |
| 304 |
raise TyperError("sep.join() of non-string list: %r" % r_lst) |
| 305 |
return hop.gendirectcall(llfn, v_str, v_length, v_items) |
| 306 |
|
| 307 |
def rtype_method_splitlines(self, hop): |
| 308 |
rstr = hop.args_r[0].repr |
| 309 |
if hop.nb_args == 2: |
| 310 |
args = hop.inputargs(rstr.repr, Bool) |
| 311 |
else: |
| 312 |
args = [hop.inputarg(rstr.repr, 0), hop.inputconst(Bool, False)] |
| 313 |
try: |
| 314 |
list_type = hop.r_result.lowleveltype.TO |
| 315 |
except AttributeError: |
| 316 |
list_type = hop.r_result.lowleveltype |
| 317 |
cLIST = hop.inputconst(Void, list_type) |
| 318 |
hop.exception_cannot_occur() |
| 319 |
return hop.gendirectcall(self.ll.ll_splitlines, cLIST, *args) |
| 320 |
|
| 321 |
def rtype_method_split(self, hop): |
| 322 |
rstr = hop.args_r[0].repr |
| 323 |
v_str = hop.inputarg(rstr.repr, 0) |
| 324 |
if isinstance(hop.args_s[1], annmodel.SomeString): |
| 325 |
v_chr = hop.inputarg(rstr.repr, 1) |
| 326 |
fn = self.ll.ll_split |
| 327 |
else: |
| 328 |
v_chr = hop.inputarg(rstr.char_repr, 1) |
| 329 |
fn = self.ll.ll_split_chr |
| 330 |
if hop.nb_args == 3: |
| 331 |
v_max = hop.inputarg(Signed, 2) |
| 332 |
else: |
| 333 |
v_max = hop.inputconst(Signed, -1) |
| 334 |
try: |
| 335 |
list_type = hop.r_result.lowleveltype.TO |
| 336 |
except AttributeError: |
| 337 |
list_type = hop.r_result.lowleveltype |
| 338 |
cLIST = hop.inputconst(Void, list_type) |
| 339 |
hop.exception_cannot_occur() |
| 340 |
return hop.gendirectcall(fn, cLIST, v_str, v_chr, v_max) |
| 341 |
|
| 342 |
def rtype_method_rsplit(self, hop): |
| 343 |
rstr = hop.args_r[0].repr |
| 344 |
v_str = hop.inputarg(rstr.repr, 0) |
| 345 |
if isinstance(hop.args_s[1], annmodel.SomeString): |
| 346 |
v_chr = hop.inputarg(rstr.repr, 1) |
| 347 |
fn = self.ll.ll_rsplit |
| 348 |
else: |
| 349 |
v_chr = hop.inputarg(rstr.char_repr, 1) |
| 350 |
fn = self.ll.ll_rsplit_chr |
| 351 |
if hop.nb_args == 3: |
| 352 |
v_max = hop.inputarg(Signed, 2) |
| 353 |
else: |
| 354 |
v_max = hop.inputconst(Signed, -1) |
| 355 |
try: |
| 356 |
list_type = hop.r_result.lowleveltype.TO |
| 357 |
except AttributeError: |
| 358 |
list_type = hop.r_result.lowleveltype |
| 359 |
cLIST = hop.inputconst(Void, list_type) |
| 360 |
hop.exception_cannot_occur() |
| 361 |
return hop.gendirectcall(fn, cLIST, v_str, v_chr, v_max) |
| 362 |
|
| 363 |
def rtype_method_replace(self, hop): |
| 364 |
rstr = hop.args_r[0].repr |
| 365 |
if not (hop.args_r[1] == rstr.char_repr and hop.args_r[2] == rstr.char_repr): |
| 366 |
raise TyperError('replace only works for char args') |
| 367 |
v_str, v_c1, v_c2 = hop.inputargs(rstr.repr, rstr.char_repr, rstr.char_repr) |
| 368 |
hop.exception_cannot_occur() |
| 369 |
return hop.gendirectcall(self.ll.ll_replace_chr_chr, v_str, v_c1, v_c2) |
| 370 |
|
| 371 |
def rtype_int(self, hop): |
| 372 |
hop.has_implicit_exception(ValueError) |
| 373 |
string_repr = hop.args_r[0].repr |
| 374 |
if hop.nb_args == 1: |
| 375 |
v_str, = hop.inputargs(string_repr) |
| 376 |
c_base = inputconst(Signed, 10) |
| 377 |
hop.exception_is_here() |
| 378 |
return hop.gendirectcall(self.ll.ll_int, v_str, c_base) |
| 379 |
if not hop.args_r[1] == rint.signed_repr: |
| 380 |
raise TyperError('base needs to be an int') |
| 381 |
v_str, v_base = hop.inputargs(string_repr, rint.signed_repr) |
| 382 |
hop.exception_is_here() |
| 383 |
return hop.gendirectcall(self.ll.ll_int, v_str, v_base) |
| 384 |
|
| 385 |
def rtype_unicode(self, hop): |
| 386 |
if hop.args_s[0].is_constant(): |
| 387 |
|
| 388 |
hop.exception_cannot_occur() |
| 389 |
return hop.inputconst(hop.r_result, hop.s_result.const) |
| 390 |
repr = hop.args_r[0].repr |
| 391 |
v_str = hop.inputarg(repr, 0) |
| 392 |
if repr == hop.r_result: |
| 393 |
hop.exception_cannot_occur() |
| 394 |
return v_str |
| 395 |
hop.exception_is_here() |
| 396 |
return hop.gendirectcall(self.ll.ll_str2unicode, v_str) |
| 397 |
|
| 398 |
def rtype_bytearray(self, hop): |
| 399 |
hop.exception_is_here() |
| 400 |
return hop.gendirectcall(self.ll.ll_str2bytearray, |
| 401 |
hop.inputarg(hop.args_r[0].repr, 0)) |
| 402 |
|
| 403 |
def rtype_method_decode(self, hop): |
| 404 |
if not hop.args_s[1].is_constant(): |
| 405 |
raise TyperError("encoding must be a constant") |
| 406 |
encoding = hop.args_s[1].const |
| 407 |
v_self = hop.inputarg(self.repr, 0) |
| 408 |
hop.exception_is_here() |
| 409 |
if encoding == 'ascii': |
| 410 |
return hop.gendirectcall(self.ll.ll_str2unicode, v_self) |
| 411 |
elif encoding == 'latin-1': |
| 412 |
return hop.gendirectcall(self.ll_decode_latin1, v_self) |
| 413 |
elif encoding == 'utf-8' or encoding == 'utf8': |
| 414 |
return hop.gendirectcall(self.ll_decode_utf8, v_self) |
| 415 |
else: |
| 416 |
raise TyperError("encoding %s not implemented" % (encoding, )) |
| 417 |
|
| 418 |
def rtype_float(self, hop): |
| 419 |
hop.has_implicit_exception(ValueError) |
| 420 |
string_repr = hop.args_r[0].repr |
| 421 |
v_str, = hop.inputargs(string_repr) |
| 422 |
hop.exception_is_here() |
| 423 |
return hop.gendirectcall(self.ll.ll_float, v_str) |
| 424 |
|
| 425 |
def ll_str(self, s): |
| 426 |
if s: |
| 427 |
return s |
| 428 |
else: |
| 429 |
return self.ll.ll_constant('None') |
| 430 |
|
| 431 |
def rtype_getslice(r_str, hop): |
| 432 |
string_repr = r_str.repr |
| 433 |
v_str = hop.inputarg(string_repr, arg=0) |
| 434 |
kind, vlist = hop.decompose_slice_args() |
| 435 |
ll_fn = getattr(r_str.ll, 'll_stringslice_%s' % (kind,)) |
| 436 |
return hop.gendirectcall(ll_fn, v_str, *vlist) |
| 437 |
|
| 438 |
def rtype_bltn_list(self, hop): |
| 439 |
string_repr = hop.args_r[0].repr |
| 440 |
if hop.r_result.LIST.ITEM != string_repr.lowleveltype.TO.chars.OF: |
| 441 |
raise TyperError("list(str-or-unicode) returns a list of chars; " |
| 442 |
"it cannot return a list of %r" % ( |
| 443 |
hop.r_result.LIST.ITEM,)) |
| 444 |
v_str, = hop.inputargs(string_repr) |
| 445 |
cRESLIST = hop.inputconst(Void, hop.r_result.LIST) |
| 446 |
hop.exception_is_here() |
| 447 |
return hop.gendirectcall(self.ll.ll_string2list, cRESLIST, v_str) |
| 448 |
|
| 449 |
|
| 450 |
class AbstractUnicodeRepr(AbstractStringRepr): |
| 451 |
|
| 452 |
def rtype_method_upper(self, hop): |
| 453 |
raise TyperError("Cannot do toupper on unicode string") |
| 454 |
|
| 455 |
def rtype_method_lower(self, hop): |
| 456 |
raise TyperError("Cannot do tolower on unicode string") |
| 457 |
|
| 458 |
@jit.elidable |
| 459 |
def ll_encode_utf8(self, ll_s): |
| 460 |
from rpython.rtyper.annlowlevel import hlunicode |
| 461 |
from rpython.rlib import runicode |
| 462 |
s = hlunicode(ll_s) |
| 463 |
assert s is not None |
| 464 |
errorhandler = runicode.default_unicode_error_encode |
| 465 |
|
| 466 |
bytes = runicode.unicode_encode_utf_8_elidable( |
| 467 |
s, len(s), 'strict', errorhandler, True) |
| 468 |
return self.ll.llstr(bytes) |
| 469 |
|
| 470 |
def rtype_method_encode(self, hop): |
| 471 |
if not hop.args_s[1].is_constant(): |
| 472 |
raise TyperError("encoding must be constant") |
| 473 |
encoding = hop.args_s[1].const |
| 474 |
if encoding == "ascii" and self.lowleveltype == UniChar: |
| 475 |
expect = UniChar |
| 476 |
else: |
| 477 |
expect = self.repr |
| 478 |
v_self = hop.inputarg(expect, 0) |
| 479 |
hop.exception_is_here() |
| 480 |
if encoding == "ascii": |
| 481 |
return hop.gendirectcall(self.ll_str, v_self) |
| 482 |
elif encoding == "latin-1": |
| 483 |
return hop.gendirectcall(self.ll_encode_latin1, v_self) |
| 484 |
elif encoding == 'utf-8' or encoding == 'utf8': |
| 485 |
return hop.gendirectcall(self.ll_encode_utf8, v_self) |
| 486 |
else: |
| 487 |
raise TyperError("encoding %s not implemented" % (encoding, )) |
| 488 |
|
| 489 |
class BaseCharReprMixin(object): |
| 490 |
|
| 491 |
def convert_const(self, value): |
| 492 |
if not isinstance(value, str) or len(value) != 1: |
| 493 |
raise TyperError("not a character: %r" % (value,)) |
| 494 |
return value |
| 495 |
|
| 496 |
def get_ll_eq_function(self): |
| 497 |
return None |
| 498 |
|
| 499 |
def get_ll_hash_function(self): |
| 500 |
return self.ll.ll_char_hash |
| 501 |
|
| 502 |
get_ll_fasthash_function = get_ll_hash_function |
| 503 |
|
| 504 |
def rtype_len(_, hop): |
| 505 |
return hop.inputconst(Signed, 1) |
| 506 |
|
| 507 |
def rtype_bool(_, hop): |
| 508 |
assert not hop.args_s[0].can_be_None |
| 509 |
return hop.inputconst(Bool, True) |
| 510 |
|
| 511 |
def rtype_ord(_, hop): |
| 512 |
repr = hop.args_r[0].char_repr |
| 513 |
vlist = hop.inputargs(repr) |
| 514 |
return hop.genop('cast_char_to_int', vlist, resulttype=Signed) |
| 515 |
|
| 516 |
def _rtype_method_isxxx(_, llfn, hop): |
| 517 |
repr = hop.args_r[0].char_repr |
| 518 |
vlist = hop.inputargs(repr) |
| 519 |
hop.exception_cannot_occur() |
| 520 |
return hop.gendirectcall(llfn, vlist[0]) |
| 521 |
|
| 522 |
def rtype_method_isspace(self, hop): |
| 523 |
return self._rtype_method_isxxx(self.ll.ll_char_isspace, hop) |
| 524 |
|
| 525 |
def rtype_method_isdigit(self, hop): |
| 526 |
return self._rtype_method_isxxx(self.ll.ll_char_isdigit, hop) |
| 527 |
|
| 528 |
def rtype_method_isalpha(self, hop): |
| 529 |
return self._rtype_method_isxxx(self.ll.ll_char_isalpha, hop) |
| 530 |
|
| 531 |
def rtype_method_isalnum(self, hop): |
| 532 |
return self._rtype_method_isxxx(self.ll.ll_char_isalnum, hop) |
| 533 |
|
| 534 |
def rtype_method_isupper(self, hop): |
| 535 |
return self._rtype_method_isxxx(self.ll.ll_char_isupper, hop) |
| 536 |
|
| 537 |
def rtype_method_islower(self, hop): |
| 538 |
return self._rtype_method_isxxx(self.ll.ll_char_islower, hop) |
| 539 |
|
| 540 |
|
| 541 |
class AbstractCharRepr(BaseCharReprMixin, AbstractStringRepr): |
| 542 |
def rtype_method_lower(self, hop): |
| 543 |
char_repr = hop.args_r[0].char_repr |
| 544 |
v_chr, = hop.inputargs(char_repr) |
| 545 |
hop.exception_cannot_occur() |
| 546 |
return hop.gendirectcall(self.ll.ll_lower_char, v_chr) |
| 547 |
|
| 548 |
def rtype_method_upper(self, hop): |
| 549 |
char_repr = hop.args_r[0].char_repr |
| 550 |
v_chr, = hop.inputargs(char_repr) |
| 551 |
hop.exception_cannot_occur() |
| 552 |
return hop.gendirectcall(self.ll.ll_upper_char, v_chr) |
| 553 |
|
| 554 |
def ll_str(self, ch): |
| 555 |
return self.ll.ll_chr2str(ch) |
| 556 |
|
| 557 |
|
| 558 |
class AbstractUniCharRepr(BaseCharReprMixin, AbstractStringRepr): |
| 559 |
|
| 560 |
def ll_str(self, ch): |
| 561 |
|
| 562 |
return str(unicode(ch)) |
| 563 |
|
| 564 |
def ll_unicode(self, ch): |
| 565 |
return unicode(ch) |
| 566 |
|
| 567 |
|
| 568 |
class __extend__(annmodel.SomeString): |
| 569 |
def rtyper_makerepr(self, rtyper): |
| 570 |
from rpython.rtyper.lltypesystem.rstr import string_repr |
| 571 |
return string_repr |
| 572 |
|
| 573 |
def rtyper_makekey(self): |
| 574 |
return self.__class__, |
| 575 |
|
| 576 |
class __extend__(annmodel.SomeUnicodeString): |
| 577 |
def rtyper_makerepr(self, rtyper): |
| 578 |
from rpython.rtyper.lltypesystem.rstr import unicode_repr |
| 579 |
return unicode_repr |
| 580 |
|
| 581 |
def rtyper_makekey(self): |
| 582 |
return self.__class__, |
| 583 |
|
| 584 |
class __extend__(annmodel.SomeChar): |
| 585 |
def rtyper_makerepr(self, rtyper): |
| 586 |
from rpython.rtyper.lltypesystem.rstr import char_repr |
| 587 |
return char_repr |
| 588 |
|
| 589 |
def rtyper_makekey(self): |
| 590 |
return self.__class__, |
| 591 |
|
| 592 |
class __extend__(annmodel.SomeUnicodeCodePoint): |
| 593 |
def rtyper_makerepr(self, rtyper): |
| 594 |
from rpython.rtyper.lltypesystem.rstr import unichar_repr |
| 595 |
return unichar_repr |
| 596 |
|
| 597 |
def rtyper_makekey(self): |
| 598 |
return self.__class__, |
| 599 |
|
| 600 |
|
| 601 |
class __extend__(pairtype(AbstractStringRepr, Repr)): |
| 602 |
def rtype_mod((r_str, _), hop): |
| 603 |
|
| 604 |
|
| 605 |
return r_str.ll.do_stringformat(hop, [(hop.args_v[1], hop.args_r[1])]) |
| 606 |
|
| 607 |
|
| 608 |
class __extend__(pairtype(AbstractStringRepr, FloatRepr)): |
| 609 |
def rtype_mod(_, hop): |
| 610 |
from rpython.rtyper.lltypesystem.rstr import do_stringformat |
| 611 |
return do_stringformat(hop, [(hop.args_v[1], hop.args_r[1])]) |
| 612 |
|
| 613 |
|
| 614 |
class __extend__(pairtype(AbstractStringRepr, IntegerRepr)): |
| 615 |
def rtype_getitem((r_str, r_int), hop, checkidx=False): |
| 616 |
string_repr = r_str.repr |
| 617 |
v_str, v_index = hop.inputargs(string_repr, Signed) |
| 618 |
if checkidx: |
| 619 |
if hop.args_s[1].nonneg: |
| 620 |
llfn = r_str.ll.ll_stritem_nonneg_checked |
| 621 |
else: |
| 622 |
llfn = r_str.ll.ll_stritem_checked |
| 623 |
else: |
| 624 |
if hop.args_s[1].nonneg: |
| 625 |
llfn = r_str.ll.ll_stritem_nonneg |
| 626 |
else: |
| 627 |
llfn = r_str.ll.ll_stritem |
| 628 |
if checkidx: |
| 629 |
hop.exception_is_here() |
| 630 |
else: |
| 631 |
hop.exception_cannot_occur() |
| 632 |
return hop.gendirectcall(llfn, v_str, v_index) |
| 633 |
|
| 634 |
def rtype_getitem_idx((r_str, r_int), hop): |
| 635 |
return pair(r_str, r_int).rtype_getitem(hop, checkidx=True) |
| 636 |
|
| 637 |
def rtype_mul((r_str, r_int), hop): |
| 638 |
str_repr = r_str.repr |
| 639 |
v_str, v_int = hop.inputargs(str_repr, Signed) |
| 640 |
return hop.gendirectcall(r_str.ll.ll_str_mul, v_str, v_int) |
| 641 |
rtype_inplace_mul = rtype_mul |
| 642 |
|
| 643 |
class __extend__(pairtype(IntegerRepr, AbstractStringRepr)): |
| 644 |
def rtype_mul((r_int, r_str), hop): |
| 645 |
str_repr = r_str.repr |
| 646 |
v_int, v_str = hop.inputargs(Signed, str_repr) |
| 647 |
return hop.gendirectcall(r_str.ll.ll_str_mul, v_str, v_int) |
| 648 |
rtype_inplace_mul = rtype_mul |
| 649 |
|
| 650 |
|
| 651 |
class __extend__(pairtype(AbstractStringRepr, AbstractStringRepr)): |
| 652 |
def rtype_add((r_str1, r_str2), hop): |
| 653 |
str1_repr = r_str1.repr |
| 654 |
str2_repr = r_str2.repr |
| 655 |
if hop.s_result.is_constant(): |
| 656 |
return hop.inputconst(str1_repr, hop.s_result.const) |
| 657 |
v_str1, v_str2 = hop.inputargs(str1_repr, str2_repr) |
| 658 |
return hop.gendirectcall(r_str1.ll.ll_strconcat, v_str1, v_str2) |
| 659 |
rtype_inplace_add = rtype_add |
| 660 |
|
| 661 |
def rtype_eq((r_str1, r_str2), hop): |
| 662 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 663 |
return hop.gendirectcall(r_str1.ll.ll_streq, v_str1, v_str2) |
| 664 |
|
| 665 |
def rtype_ne((r_str1, r_str2), hop): |
| 666 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 667 |
vres = hop.gendirectcall(r_str1.ll.ll_streq, v_str1, v_str2) |
| 668 |
return hop.genop('bool_not', [vres], resulttype=Bool) |
| 669 |
|
| 670 |
def rtype_lt((r_str1, r_str2), hop): |
| 671 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 672 |
vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2) |
| 673 |
return hop.genop('int_lt', [vres, hop.inputconst(Signed, 0)], |
| 674 |
resulttype=Bool) |
| 675 |
|
| 676 |
def rtype_le((r_str1, r_str2), hop): |
| 677 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 678 |
vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2) |
| 679 |
return hop.genop('int_le', [vres, hop.inputconst(Signed, 0)], |
| 680 |
resulttype=Bool) |
| 681 |
|
| 682 |
def rtype_ge((r_str1, r_str2), hop): |
| 683 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 684 |
vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2) |
| 685 |
return hop.genop('int_ge', [vres, hop.inputconst(Signed, 0)], |
| 686 |
resulttype=Bool) |
| 687 |
|
| 688 |
def rtype_gt((r_str1, r_str2), hop): |
| 689 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 690 |
vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2) |
| 691 |
return hop.genop('int_gt', [vres, hop.inputconst(Signed, 0)], |
| 692 |
resulttype=Bool) |
| 693 |
|
| 694 |
def rtype_contains((r_str1, r_str2), hop): |
| 695 |
v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr) |
| 696 |
v_end = hop.gendirectcall(r_str1.ll.ll_strlen, v_str1) |
| 697 |
vres = hop.gendirectcall(r_str1.ll.ll_find, v_str1, v_str2, |
| 698 |
hop.inputconst(Signed, 0), v_end) |
| 699 |
hop.exception_cannot_occur() |
| 700 |
return hop.genop('int_ne', [vres, hop.inputconst(Signed, -1)], |
| 701 |
resulttype=Bool) |
| 702 |
|
| 703 |
|
| 704 |
class __extend__(pairtype(AbstractStringRepr, AbstractCharRepr), |
| 705 |
pairtype(AbstractUnicodeRepr, AbstractUniCharRepr)): |
| 706 |
def rtype_contains((r_str, r_chr), hop): |
| 707 |
string_repr = r_str.repr |
| 708 |
char_repr = r_chr.char_repr |
| 709 |
v_str, v_chr = hop.inputargs(string_repr, char_repr) |
| 710 |
hop.exception_cannot_occur() |
| 711 |
return hop.gendirectcall(r_str.ll.ll_contains, v_str, v_chr) |
| 712 |
|
| 713 |
|
| 714 |
class __extend__(pairtype(AbstractCharRepr, IntegerRepr), |
| 715 |
pairtype(AbstractUniCharRepr, IntegerRepr)): |
| 716 |
|
| 717 |
def rtype_mul((r_chr, r_int), hop): |
| 718 |
char_repr = r_chr.char_repr |
| 719 |
v_char, v_int = hop.inputargs(char_repr, Signed) |
| 720 |
return hop.gendirectcall(r_chr.ll.ll_char_mul, v_char, v_int) |
| 721 |
rtype_inplace_mul = rtype_mul |
| 722 |
|
| 723 |
def rtype_getitem((r_char, r_int), hop, checkidx=False): |
| 724 |
if hop.args_s[1].is_constant() and hop.args_s[1].const == 0: |
| 725 |
hop.exception_cannot_occur() |
| 726 |
return hop.inputarg(hop.r_result, arg=0) |
| 727 |
|
| 728 |
paircls = pairtype(AbstractStringRepr, IntegerRepr) |
| 729 |
return paircls.rtype_getitem(pair(r_char, r_int), hop, checkidx) |
| 730 |
|
| 731 |
|
| 732 |
class __extend__(pairtype(IntegerRepr, AbstractCharRepr), |
| 733 |
pairtype(IntegerRepr, AbstractUniCharRepr)): |
| 734 |
def rtype_mul((r_int, r_chr), hop): |
| 735 |
char_repr = r_chr.char_repr |
| 736 |
v_int, v_char = hop.inputargs(Signed, char_repr) |
| 737 |
return hop.gendirectcall(r_chr.ll.ll_char_mul, v_char, v_int) |
| 738 |
rtype_inplace_mul = rtype_mul |
| 739 |
|
| 740 |
class __extend__(pairtype(AbstractCharRepr, AbstractCharRepr)): |
| 741 |
def rtype_eq(_, hop): return _rtype_compare_template(hop, 'eq') |
| 742 |
def rtype_ne(_, hop): return _rtype_compare_template(hop, 'ne') |
| 743 |
def rtype_lt(_, hop): return _rtype_compare_template(hop, 'lt') |
| 744 |
def rtype_le(_, hop): return _rtype_compare_template(hop, 'le') |
| 745 |
def rtype_gt(_, hop): return _rtype_compare_template(hop, 'gt') |
| 746 |
def rtype_ge(_, hop): return _rtype_compare_template(hop, 'ge') |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
def _rtype_compare_template(hop, func): |
| 751 |
from rpython.rtyper.lltypesystem.rstr import char_repr |
| 752 |
vlist = hop.inputargs(char_repr, char_repr) |
| 753 |
return hop.genop('char_' + func, vlist, resulttype=Bool) |
| 754 |
|
| 755 |
class __extend__(AbstractUniCharRepr): |
| 756 |
|
| 757 |
def convert_const(self, value): |
| 758 |
if isinstance(value, str): |
| 759 |
value = unicode(value) |
| 760 |
if not isinstance(value, unicode) or len(value) != 1: |
| 761 |
raise TyperError("not a unicode character: %r" % (value,)) |
| 762 |
return value |
| 763 |
|
| 764 |
def get_ll_eq_function(self): |
| 765 |
return None |
| 766 |
|
| 767 |
def get_ll_hash_function(self): |
| 768 |
return self.ll.ll_unichar_hash |
| 769 |
|
| 770 |
get_ll_fasthash_function = get_ll_hash_function |
| 771 |
|
| 772 |
def rtype_ord(_, hop): |
| 773 |
from rpython.rtyper.lltypesystem.rstr import unichar_repr |
| 774 |
vlist = hop.inputargs(unichar_repr) |
| 775 |
return hop.genop('cast_unichar_to_int', vlist, resulttype=Signed) |
| 776 |
|
| 777 |
|
| 778 |
class __extend__(pairtype(AbstractUniCharRepr, AbstractUniCharRepr)): |
| 779 |
def rtype_eq(_, hop): return _rtype_unchr_compare_template(hop, 'eq') |
| 780 |
def rtype_ne(_, hop): return _rtype_unchr_compare_template(hop, 'ne') |
| 781 |
def rtype_lt(_, hop): return _rtype_unchr_compare_template_ord(hop, 'lt') |
| 782 |
def rtype_le(_, hop): return _rtype_unchr_compare_template_ord(hop, 'le') |
| 783 |
def rtype_gt(_, hop): return _rtype_unchr_compare_template_ord(hop, 'gt') |
| 784 |
def rtype_ge(_, hop): return _rtype_unchr_compare_template_ord(hop, 'ge') |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
def _rtype_unchr_compare_template(hop, func): |
| 790 |
from rpython.rtyper.lltypesystem.rstr import unichar_repr |
| 791 |
vlist = hop.inputargs(unichar_repr, unichar_repr) |
| 792 |
return hop.genop('unichar_' + func, vlist, resulttype=Bool) |
| 793 |
|
| 794 |
def _rtype_unchr_compare_template_ord(hop, func): |
| 795 |
vlist = hop.inputargs(*hop.args_r) |
| 796 |
vlist2 = [] |
| 797 |
for v in vlist: |
| 798 |
v = hop.genop('cast_unichar_to_int', [v], resulttype=lltype.Signed) |
| 799 |
vlist2.append(v) |
| 800 |
return hop.genop('int_' + func, vlist2, resulttype=Bool) |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
class __extend__(pairtype(AbstractCharRepr, AbstractStringRepr), |
| 806 |
pairtype(AbstractUniCharRepr, AbstractUnicodeRepr)): |
| 807 |
def convert_from_to((r_from, r_to), v, llops): |
| 808 |
from rpython.rtyper.lltypesystem.rstr import ( |
| 809 |
string_repr, unicode_repr, char_repr, unichar_repr) |
| 810 |
if (r_from == char_repr and r_to == string_repr) or\ |
| 811 |
(r_from == unichar_repr and r_to == unicode_repr): |
| 812 |
return llops.gendirectcall(r_from.ll.ll_chr2str, v) |
| 813 |
return NotImplemented |
| 814 |
|
| 815 |
class __extend__(pairtype(AbstractStringRepr, AbstractCharRepr)): |
| 816 |
def convert_from_to((r_from, r_to), v, llops): |
| 817 |
from rpython.rtyper.lltypesystem.rstr import string_repr, char_repr |
| 818 |
if r_from == string_repr and r_to == char_repr: |
| 819 |
c_zero = inputconst(Signed, 0) |
| 820 |
return llops.gendirectcall(r_from.ll.ll_stritem_nonneg, v, c_zero) |
| 821 |
return NotImplemented |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
class AbstractStringIteratorRepr(IteratorRepr): |
| 828 |
|
| 829 |
def newiter(self, hop): |
| 830 |
string_repr = hop.args_r[0].repr |
| 831 |
v_str, = hop.inputargs(string_repr) |
| 832 |
return hop.gendirectcall(self.ll_striter, v_str) |
| 833 |
|
| 834 |
def rtype_next(self, hop): |
| 835 |
v_iter, = hop.inputargs(self) |
| 836 |
hop.has_implicit_exception(StopIteration) |
| 837 |
hop.exception_is_here() |
| 838 |
return hop.gendirectcall(self.ll_strnext, v_iter) |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
class AbstractLLHelpers(object): |
| 849 |
@staticmethod |
| 850 |
def ll_isdigit(s): |
| 851 |
from rpython.rtyper.annlowlevel import hlstr |
| 852 |
|
| 853 |
s = hlstr(s) |
| 854 |
if not s: |
| 855 |
return False |
| 856 |
for ch in s: |
| 857 |
if not ch.isdigit(): |
| 858 |
return False |
| 859 |
return True |
| 860 |
|
| 861 |
@staticmethod |
| 862 |
def ll_isalpha(s): |
| 863 |
from rpython.rtyper.annlowlevel import hlstr |
| 864 |
|
| 865 |
s = hlstr(s) |
| 866 |
if not s: |
| 867 |
return False |
| 868 |
for ch in s: |
| 869 |
if not ch.isalpha(): |
| 870 |
return False |
| 871 |
return True |
| 872 |
|
| 873 |
@staticmethod |
| 874 |
def ll_isalnum(s): |
| 875 |
from rpython.rtyper.annlowlevel import hlstr |
| 876 |
|
| 877 |
s = hlstr(s) |
| 878 |
if not s: |
| 879 |
return False |
| 880 |
for ch in s: |
| 881 |
if not ch.isalnum(): |
| 882 |
return False |
| 883 |
return True |
| 884 |
|
| 885 |
@staticmethod |
| 886 |
def ll_char_isspace(ch): |
| 887 |
c = ord(ch) |
| 888 |
return c == 32 or (9 <= c <= 13) |
| 889 |
|
| 890 |
@staticmethod |
| 891 |
def ll_char_isdigit(ch): |
| 892 |
c = ord(ch) |
| 893 |
return c <= 57 and c >= 48 |
| 894 |
|
| 895 |
@staticmethod |
| 896 |
def ll_char_isalpha(ch): |
| 897 |
c = ord(ch) |
| 898 |
if c >= 97: |
| 899 |
return c <= 122 |
| 900 |
else: |
| 901 |
return 65 <= c <= 90 |
| 902 |
|
| 903 |
@staticmethod |
| 904 |
def ll_char_isalnum(ch): |
| 905 |
c = ord(ch) |
| 906 |
if c >= 65: |
| 907 |
if c >= 97: |
| 908 |
return c <= 122 |
| 909 |
else: |
| 910 |
return c <= 90 |
| 911 |
else: |
| 912 |
return 48 <= c <= 57 |
| 913 |
|
| 914 |
@staticmethod |
| 915 |
def ll_char_isupper(ch): |
| 916 |
c = ord(ch) |
| 917 |
return 65 <= c <= 90 |
| 918 |
|
| 919 |
@staticmethod |
| 920 |
def ll_char_islower(ch): |
| 921 |
c = ord(ch) |
| 922 |
return 97 <= c <= 122 |
| 923 |
|
| 924 |
@staticmethod |
| 925 |
def ll_upper_char(ch): |
| 926 |
if 'a' <= ch <= 'z': |
| 927 |
ch = chr(ord(ch) - 32) |
| 928 |
return ch |
| 929 |
|
| 930 |
@staticmethod |
| 931 |
def ll_lower_char(ch): |
| 932 |
if 'A' <= ch <= 'Z': |
| 933 |
ch = chr(ord(ch) + 32) |
| 934 |
return ch |
| 935 |
|
| 936 |
@staticmethod |
| 937 |
def ll_char_hash(ch): |
| 938 |
return ord(ch) |
| 939 |
|
| 940 |
@staticmethod |
| 941 |
def ll_unichar_hash(ch): |
| 942 |
return ord(ch) |
| 943 |
|
| 944 |
@classmethod |
| 945 |
def ll_str_is_true(cls, s): |
| 946 |
|
| 947 |
return bool(s) and cls.ll_strlen(s) != 0 |
| 948 |
|
| 949 |
@classmethod |
| 950 |
def ll_stritem_nonneg_checked(cls, s, i): |
| 951 |
if i >= cls.ll_strlen(s): |
| 952 |
raise IndexError |
| 953 |
return cls.ll_stritem_nonneg(s, i) |
| 954 |
|
| 955 |
@classmethod |
| 956 |
def ll_stritem(cls, s, i): |
| 957 |
if i < 0: |
| 958 |
i += cls.ll_strlen(s) |
| 959 |
return cls.ll_stritem_nonneg(s, i) |
| 960 |
|
| 961 |
@classmethod |
| 962 |
def ll_stritem_checked(cls, s, i): |
| 963 |
length = cls.ll_strlen(s) |
| 964 |
if i < 0: |
| 965 |
i += length |
| 966 |
if i >= length or i < 0: |
| 967 |
raise IndexError |
| 968 |
return cls.ll_stritem_nonneg(s, i) |
| 969 |
|
| 970 |
@staticmethod |
| 971 |
def parse_fmt_string(fmt): |
| 972 |
|
| 973 |
it = iter(fmt) |
| 974 |
r = [] |
| 975 |
curstr = '' |
| 976 |
for c in it: |
| 977 |
if c == '%': |
| 978 |
f = it.next() |
| 979 |
if f == '%': |
| 980 |
curstr += '%' |
| 981 |
continue |
| 982 |
|
| 983 |
if curstr: |
| 984 |
r.append(curstr) |
| 985 |
curstr = '' |
| 986 |
if f not in 'xdosrf': |
| 987 |
raise TyperError("Unsupported formatting specifier: %r in %r" % (f, fmt)) |
| 988 |
|
| 989 |
r.append((f,)) |
| 990 |
else: |
| 991 |
curstr += c |
| 992 |
if curstr: |
| 993 |
r.append(curstr) |
| 994 |
return r |
| 995 |
|
| 996 |
@staticmethod |
| 997 |
def ll_float(ll_str): |
| 998 |
from rpython.rtyper.annlowlevel import hlstr |
| 999 |
from rpython.rlib.rfloat import rstring_to_float |
| 1000 |
s = hlstr(ll_str) |
| 1001 |
assert s is not None |
| 1002 |
|
| 1003 |
n = len(s) |
| 1004 |
beg = 0 |
| 1005 |
while beg < n: |
| 1006 |
if s[beg] == ' ': |
| 1007 |
beg += 1 |
| 1008 |
else: |
| 1009 |
break |
| 1010 |
if beg == n: |
| 1011 |
raise ValueError |
| 1012 |
end = n - 1 |
| 1013 |
while end >= 0: |
| 1014 |
if s[end] == ' ': |
| 1015 |
end -= 1 |
| 1016 |
else: |
| 1017 |
break |
| 1018 |
assert end >= 0 |
| 1019 |
return rstring_to_float(s[beg:end + 1]) |
| 1020 |
|
| 1021 |
@classmethod |
| 1022 |
def ll_splitlines(cls, LIST, ll_str, keep_newlines): |
| 1023 |
from rpython.rtyper.annlowlevel import hlstr |
| 1024 |
s = hlstr(ll_str) |
| 1025 |
strlen = len(s) |
| 1026 |
i = 0 |
| 1027 |
j = 0 |
| 1028 |
|
| 1029 |
ITEM = LIST.items.TO.OF |
| 1030 |
res = LIST.ll_newlist(0) |
| 1031 |
while j < strlen: |
| 1032 |
while i < strlen and s[i] != '\n' and s[i] != '\r': |
| 1033 |
i += 1 |
| 1034 |
eol = i |
| 1035 |
if i < strlen: |
| 1036 |
if s[i] == '\r' and i + 1 < strlen and s[i + 1] == '\n': |
| 1037 |
i += 2 |
| 1038 |
else: |
| 1039 |
i += 1 |
| 1040 |
if keep_newlines: |
| 1041 |
eol = i |
| 1042 |
list_length = res.ll_length() |
| 1043 |
res._ll_resize_ge(list_length + 1) |
| 1044 |
item = cls.ll_stringslice_startstop(ll_str, j, eol) |
| 1045 |
res.ll_setitem_fast(list_length, llmemory.cast_any_ptr(ITEM, item)) |
| 1046 |
j = i |
| 1047 |
if j < strlen: |
| 1048 |
list_length = res.ll_length() |
| 1049 |
res._ll_resize_ge(list_length + 1) |
| 1050 |
item = cls.ll_stringslice_startstop(ll_str, j, strlen) |
| 1051 |
res.ll_setitem_fast(list_length, llmemory.cast_any_ptr(ITEM, item)) |
| 1052 |
return res |