| 1 |
from weakref import WeakValueDictionary |
| 2 |
|
| 3 |
from rpython.annotator import model as annmodel |
| 4 |
from rpython.rlib import jit, types, objectmodel, rgc |
| 5 |
from rpython.rlib.objectmodel import (malloc_zero_filled, we_are_translated, |
| 6 |
ll_hash_string, keepalive_until_here, specialize, enforceargs, dont_inline) |
| 7 |
from rpython.rlib.signature import signature |
| 8 |
from rpython.rlib.rarithmetic import ovfcheck |
| 9 |
from rpython.rtyper.error import TyperError |
| 10 |
from rpython.rtyper.debug import ll_assert |
| 11 |
from rpython.rtyper.lltypesystem import ll_str, llmemory |
| 12 |
from rpython.rtyper.lltypesystem.lltype import (GcStruct, Signed, Array, Char, |
| 13 |
UniChar, Ptr, malloc, Bool, Void, GcArray, nullptr, cast_primitive, |
| 14 |
typeOf, staticAdtMethod, GcForwardReference) |
| 15 |
from rpython.rtyper.rbool import BoolRepr |
| 16 |
from rpython.rtyper.rmodel import inputconst, Repr |
| 17 |
from rpython.rtyper.rint import IntegerRepr |
| 18 |
from rpython.rtyper.rstr import (AbstractStringRepr, AbstractCharRepr, |
| 19 |
AbstractUniCharRepr, AbstractStringIteratorRepr, AbstractLLHelpers, |
| 20 |
AbstractUnicodeRepr) |
| 21 |
from rpython.tool.sourcetools import func_with_new_name |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
STR = GcForwardReference() |
| 33 |
UNICODE = GcForwardReference() |
| 34 |
|
| 35 |
def new_malloc(TP, name): |
| 36 |
@enforceargs(int) |
| 37 |
def mallocstr(length): |
| 38 |
ll_assert(length >= 0, "negative string length") |
| 39 |
r = malloc(TP, length) |
| 40 |
if not we_are_translated() or not malloc_zero_filled: |
| 41 |
r.hash = 0 |
| 42 |
return r |
| 43 |
return func_with_new_name(mallocstr, name) |
| 44 |
|
| 45 |
mallocstr = new_malloc(STR, 'mallocstr') |
| 46 |
mallocunicode = new_malloc(UNICODE, 'mallocunicode') |
| 47 |
|
| 48 |
@specialize.memo() |
| 49 |
def emptystrfun(): |
| 50 |
return string_repr.convert_const("") |
| 51 |
|
| 52 |
@specialize.memo() |
| 53 |
def emptyunicodefun(): |
| 54 |
return unicode_repr.convert_const(u'') |
| 55 |
|
| 56 |
def _new_copy_contents_fun(SRC_TP, DST_TP, CHAR_TP, name): |
| 57 |
@specialize.arg(0) |
| 58 |
def _str_ofs(TP, item): |
| 59 |
return (llmemory.offsetof(TP, 'chars') + |
| 60 |
llmemory.itemoffsetof(TP.chars, 0) + |
| 61 |
llmemory.sizeof(CHAR_TP) * item) |
| 62 |
|
| 63 |
@signature(types.any(), types.any(), types.int(), returns=types.any()) |
| 64 |
@specialize.arg(0) |
| 65 |
def _get_raw_buf(TP, src, ofs): |
| 66 |
""" |
| 67 |
WARNING: dragons ahead. |
| 68 |
Return the address of the internal char* buffer of the low level |
| 69 |
string. The return value is valid as long as no GC operation occur, so |
| 70 |
you must ensure that it will be used inside a "GC safe" section, for |
| 71 |
example by marking your function with @rgc.no_collect |
| 72 |
""" |
| 73 |
assert typeOf(src).TO == TP |
| 74 |
assert ofs >= 0 |
| 75 |
return llmemory.cast_ptr_to_adr(src) + _str_ofs(TP, ofs) |
| 76 |
_get_raw_buf._always_inline_ = True |
| 77 |
|
| 78 |
@jit.oopspec('stroruni.copy_contents(src, dst, srcstart, dststart, length)') |
| 79 |
@signature(types.any(), types.any(), types.int(), types.int(), types.int(), returns=types.none()) |
| 80 |
def copy_string_contents(src, dst, srcstart, dststart, length): |
| 81 |
"""Copies 'length' characters from the 'src' string to the 'dst' |
| 82 |
string, starting at position 'srcstart' and 'dststart'.""" |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
assert length >= 0 |
| 90 |
ll_assert(srcstart >= 0, "copystrc: negative srcstart") |
| 91 |
ll_assert(srcstart + length <= len(src.chars), "copystrc: src ovf") |
| 92 |
ll_assert(dststart >= 0, "copystrc: negative dststart") |
| 93 |
ll_assert(dststart + length <= len(dst.chars), "copystrc: dst ovf") |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if rgc.must_split_gc_address_space(): |
| 98 |
i = 0 |
| 99 |
while i < length: |
| 100 |
dst.chars[dststart + i] = src.chars[srcstart + i] |
| 101 |
i += 1 |
| 102 |
return |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
asrc = _get_raw_buf(SRC_TP, src, srcstart) |
| 107 |
adst = _get_raw_buf(DST_TP, dst, dststart) |
| 108 |
llmemory.raw_memcopy(asrc, adst, llmemory.sizeof(CHAR_TP) * length) |
| 109 |
|
| 110 |
keepalive_until_here(src) |
| 111 |
keepalive_until_here(dst) |
| 112 |
copy_string_contents._always_inline_ = True |
| 113 |
copy_string_contents = func_with_new_name(copy_string_contents, |
| 114 |
'copy_%s_contents' % name) |
| 115 |
|
| 116 |
@jit.oopspec('stroruni.copy_string_to_raw(src, ptrdst, srcstart, length)') |
| 117 |
def copy_string_to_raw(src, ptrdst, srcstart, length): |
| 118 |
""" |
| 119 |
Copies 'length' characters from the 'src' string to the 'ptrdst' |
| 120 |
buffer, starting at position 'srcstart'. |
| 121 |
'ptrdst' must be a non-gc Array of Char. |
| 122 |
""" |
| 123 |
|
| 124 |
assert length >= 0 |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if rgc.must_split_gc_address_space(): |
| 129 |
i = 0 |
| 130 |
while i < length: |
| 131 |
ptrdst[i] = src.chars[srcstart + i] |
| 132 |
i += 1 |
| 133 |
return |
| 134 |
|
| 135 |
|
| 136 |
asrc = _get_raw_buf(SRC_TP, src, srcstart) |
| 137 |
adst = llmemory.cast_ptr_to_adr(ptrdst) |
| 138 |
adst = adst + llmemory.itemoffsetof(typeOf(ptrdst).TO, 0) |
| 139 |
llmemory.raw_memcopy(asrc, adst, llmemory.sizeof(CHAR_TP) * length) |
| 140 |
|
| 141 |
keepalive_until_here(src) |
| 142 |
copy_string_to_raw._always_inline_ = True |
| 143 |
copy_string_to_raw = func_with_new_name(copy_string_to_raw, 'copy_%s_to_raw' % name) |
| 144 |
|
| 145 |
@jit.dont_look_inside |
| 146 |
@signature(types.any(), types.any(), types.int(), types.int(), |
| 147 |
returns=types.none()) |
| 148 |
def copy_raw_to_string(ptrsrc, dst, dststart, length): |
| 149 |
|
| 150 |
assert length >= 0 |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if rgc.must_split_gc_address_space(): |
| 155 |
i = 0 |
| 156 |
while i < length: |
| 157 |
dst.chars[dststart + i] = ptrsrc[i] |
| 158 |
i += 1 |
| 159 |
return |
| 160 |
|
| 161 |
|
| 162 |
adst = _get_raw_buf(SRC_TP, dst, dststart) |
| 163 |
asrc = llmemory.cast_ptr_to_adr(ptrsrc) |
| 164 |
|
| 165 |
asrc = asrc + llmemory.itemoffsetof(typeOf(ptrsrc).TO, 0) |
| 166 |
llmemory.raw_memcopy(asrc, adst, llmemory.sizeof(CHAR_TP) * length) |
| 167 |
|
| 168 |
keepalive_until_here(dst) |
| 169 |
copy_raw_to_string._always_inline_ = True |
| 170 |
copy_raw_to_string = func_with_new_name(copy_raw_to_string, |
| 171 |
'copy_raw_to_%s' % name) |
| 172 |
|
| 173 |
return (copy_string_to_raw, copy_raw_to_string, copy_string_contents, |
| 174 |
_get_raw_buf) |
| 175 |
|
| 176 |
(copy_string_to_raw, |
| 177 |
copy_raw_to_string, |
| 178 |
copy_string_contents, |
| 179 |
_get_raw_buf_string) = _new_copy_contents_fun(STR, STR, Char, 'string') |
| 180 |
|
| 181 |
(copy_unicode_to_raw, |
| 182 |
copy_raw_to_unicode, |
| 183 |
copy_unicode_contents, |
| 184 |
_get_raw_buf_unicode) = _new_copy_contents_fun(UNICODE, UNICODE, UniChar, |
| 185 |
'unicode') |
| 186 |
|
| 187 |
CONST_STR_CACHE = WeakValueDictionary() |
| 188 |
CONST_UNICODE_CACHE = WeakValueDictionary() |
| 189 |
|
| 190 |
class BaseLLStringRepr(Repr): |
| 191 |
def convert_const(self, value): |
| 192 |
if value is None: |
| 193 |
return nullptr(self.lowleveltype.TO) |
| 194 |
|
| 195 |
if not isinstance(value, self.basetype): |
| 196 |
raise TyperError("not a str: %r" % (value,)) |
| 197 |
try: |
| 198 |
return self.CACHE[value] |
| 199 |
except KeyError: |
| 200 |
p = self.malloc(len(value)) |
| 201 |
for i in range(len(value)): |
| 202 |
p.chars[i] = cast_primitive(self.base, value[i]) |
| 203 |
p.hash = 0 |
| 204 |
self.ll.ll_strhash(p) |
| 205 |
self.CACHE[value] = p |
| 206 |
return p |
| 207 |
|
| 208 |
def make_iterator_repr(self, variant=None): |
| 209 |
if variant is not None: |
| 210 |
raise TyperError("unsupported %r iterator over a str/unicode" % |
| 211 |
(variant,)) |
| 212 |
return self.repr.iterator_repr |
| 213 |
|
| 214 |
def can_ll_be_null(self, s_value): |
| 215 |
|
| 216 |
if self is string_repr: |
| 217 |
return s_value.can_be_none() |
| 218 |
else: |
| 219 |
return True |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
def _list_length_items(self, hop, v_lst, LIST): |
| 224 |
LIST = LIST.TO |
| 225 |
v_length = hop.gendirectcall(LIST.ll_length, v_lst) |
| 226 |
v_items = hop.gendirectcall(LIST.ll_items, v_lst) |
| 227 |
return v_length, v_items |
| 228 |
|
| 229 |
class StringRepr(BaseLLStringRepr, AbstractStringRepr): |
| 230 |
lowleveltype = Ptr(STR) |
| 231 |
basetype = str |
| 232 |
base = Char |
| 233 |
CACHE = CONST_STR_CACHE |
| 234 |
|
| 235 |
def __init__(self, *args): |
| 236 |
AbstractStringRepr.__init__(self, *args) |
| 237 |
self.ll = LLHelpers |
| 238 |
self.malloc = mallocstr |
| 239 |
|
| 240 |
def ll_decode_latin1(self, value): |
| 241 |
lgt = len(value.chars) |
| 242 |
s = mallocunicode(lgt) |
| 243 |
for i in range(lgt): |
| 244 |
s.chars[i] = cast_primitive(UniChar, value.chars[i]) |
| 245 |
return s |
| 246 |
|
| 247 |
class UnicodeRepr(BaseLLStringRepr, AbstractUnicodeRepr): |
| 248 |
lowleveltype = Ptr(UNICODE) |
| 249 |
basetype = basestring |
| 250 |
base = UniChar |
| 251 |
CACHE = CONST_UNICODE_CACHE |
| 252 |
|
| 253 |
def __init__(self, *args): |
| 254 |
AbstractUnicodeRepr.__init__(self, *args) |
| 255 |
self.ll = LLHelpers |
| 256 |
self.malloc = mallocunicode |
| 257 |
|
| 258 |
@jit.elidable |
| 259 |
def ll_str(self, s): |
| 260 |
|
| 261 |
|
| 262 |
if not s: |
| 263 |
return self.ll.ll_constant('None') |
| 264 |
lgt = len(s.chars) |
| 265 |
result = mallocstr(lgt) |
| 266 |
for i in range(lgt): |
| 267 |
c = s.chars[i] |
| 268 |
if ord(c) > 127: |
| 269 |
raise UnicodeEncodeError("character not in ascii range") |
| 270 |
result.chars[i] = cast_primitive(Char, c) |
| 271 |
return result |
| 272 |
|
| 273 |
@jit.elidable |
| 274 |
def ll_unicode(self, s): |
| 275 |
if s: |
| 276 |
return s |
| 277 |
else: |
| 278 |
return self.ll.ll_constant_unicode(u'None') |
| 279 |
|
| 280 |
@jit.elidable |
| 281 |
def ll_encode_latin1(self, s): |
| 282 |
length = len(s.chars) |
| 283 |
result = mallocstr(length) |
| 284 |
for i in range(length): |
| 285 |
c = s.chars[i] |
| 286 |
if ord(c) > 255: |
| 287 |
raise UnicodeEncodeError("character not in latin1 range") |
| 288 |
result.chars[i] = cast_primitive(Char, c) |
| 289 |
return result |
| 290 |
|
| 291 |
class CharRepr(AbstractCharRepr, StringRepr): |
| 292 |
lowleveltype = Char |
| 293 |
|
| 294 |
class UniCharRepr(AbstractUniCharRepr, UnicodeRepr): |
| 295 |
lowleveltype = UniChar |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
class LLHelpers(AbstractLLHelpers): |
| 308 |
from rpython.rtyper.annlowlevel import llstr, llunicode |
| 309 |
|
| 310 |
@staticmethod |
| 311 |
@jit.elidable |
| 312 |
def ll_str_mul(s, times): |
| 313 |
if times < 0: |
| 314 |
times = 0 |
| 315 |
try: |
| 316 |
size = ovfcheck(len(s.chars) * times) |
| 317 |
except OverflowError: |
| 318 |
raise MemoryError |
| 319 |
newstr = s.malloc(size) |
| 320 |
i = 0 |
| 321 |
if i < size: |
| 322 |
s.copy_contents(s, newstr, 0, 0, len(s.chars)) |
| 323 |
i += len(s.chars) |
| 324 |
while i < size: |
| 325 |
if i <= size - i: |
| 326 |
j = i |
| 327 |
else: |
| 328 |
j = size - i |
| 329 |
s.copy_contents(newstr, newstr, 0, i, j) |
| 330 |
i += j |
| 331 |
return newstr |
| 332 |
|
| 333 |
@staticmethod |
| 334 |
@jit.elidable |
| 335 |
def ll_char_mul(ch, times): |
| 336 |
if typeOf(ch) is Char: |
| 337 |
malloc = mallocstr |
| 338 |
else: |
| 339 |
malloc = mallocunicode |
| 340 |
if times < 0: |
| 341 |
times = 0 |
| 342 |
newstr = malloc(times) |
| 343 |
j = 0 |
| 344 |
|
| 345 |
while j < times: |
| 346 |
newstr.chars[j] = ch |
| 347 |
j += 1 |
| 348 |
return newstr |
| 349 |
|
| 350 |
@staticmethod |
| 351 |
def ll_strlen(s): |
| 352 |
return len(s.chars) |
| 353 |
|
| 354 |
@staticmethod |
| 355 |
@signature(types.any(), types.int(), returns=types.any()) |
| 356 |
def ll_stritem_nonneg(s, i): |
| 357 |
chars = s.chars |
| 358 |
ll_assert(i >= 0, "negative str getitem index") |
| 359 |
ll_assert(i < len(chars), "str getitem index out of bound") |
| 360 |
return chars[i] |
| 361 |
|
| 362 |
@staticmethod |
| 363 |
def ll_chr2str(ch): |
| 364 |
if typeOf(ch) is Char: |
| 365 |
malloc = mallocstr |
| 366 |
else: |
| 367 |
malloc = mallocunicode |
| 368 |
s = malloc(1) |
| 369 |
s.chars[0] = ch |
| 370 |
return s |
| 371 |
|
| 372 |
|
| 373 |
@staticmethod |
| 374 |
@jit.oopspec("str.str2unicode(str)") |
| 375 |
def ll_str2unicode(str): |
| 376 |
lgt = len(str.chars) |
| 377 |
s = mallocunicode(lgt) |
| 378 |
for i in range(lgt): |
| 379 |
if ord(str.chars[i]) > 127: |
| 380 |
raise UnicodeDecodeError |
| 381 |
s.chars[i] = cast_primitive(UniChar, str.chars[i]) |
| 382 |
return s |
| 383 |
|
| 384 |
@staticmethod |
| 385 |
def ll_str2bytearray(str): |
| 386 |
from rpython.rtyper.lltypesystem.rbytearray import BYTEARRAY |
| 387 |
|
| 388 |
lgt = len(str.chars) |
| 389 |
b = malloc(BYTEARRAY, lgt) |
| 390 |
for i in range(lgt): |
| 391 |
b.chars[i] = str.chars[i] |
| 392 |
return b |
| 393 |
|
| 394 |
@staticmethod |
| 395 |
def ll_strhash(s): |
| 396 |
if s: |
| 397 |
return jit.conditional_call_elidable(s.hash, |
| 398 |
LLHelpers._ll_strhash, s) |
| 399 |
else: |
| 400 |
return 0 |
| 401 |
|
| 402 |
@staticmethod |
| 403 |
@dont_inline |
| 404 |
@jit.dont_look_inside |
| 405 |
def _ll_strhash(s): |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
x = ll_hash_string(s) |
| 411 |
if x == 0: |
| 412 |
x = 29872897 |
| 413 |
s.hash = x |
| 414 |
return x |
| 415 |
|
| 416 |
@staticmethod |
| 417 |
def ll_length(s): |
| 418 |
return len(s.chars) |
| 419 |
|
| 420 |
@staticmethod |
| 421 |
def ll_strfasthash(s): |
| 422 |
ll_assert(s.hash != 0, "ll_strfasthash: hash==0") |
| 423 |
return s.hash |
| 424 |
|
| 425 |
@staticmethod |
| 426 |
@jit.elidable |
| 427 |
@jit.oopspec('stroruni.concat(s1, s2)') |
| 428 |
def ll_strconcat(s1, s2): |
| 429 |
len1 = s1.length() |
| 430 |
len2 = s2.length() |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if typeOf(s1) == Ptr(STR): |
| 435 |
newstr = s2.malloc(len1 + len2) |
| 436 |
newstr.copy_contents_from_str(s1, newstr, 0, 0, len1) |
| 437 |
else: |
| 438 |
newstr = s1.malloc(len1 + len2) |
| 439 |
newstr.copy_contents(s1, newstr, 0, 0, len1) |
| 440 |
if typeOf(s2) == Ptr(STR): |
| 441 |
newstr.copy_contents_from_str(s2, newstr, 0, len1, len2) |
| 442 |
else: |
| 443 |
newstr.copy_contents(s2, newstr, 0, len1, len2) |
| 444 |
return newstr |
| 445 |
|
| 446 |
@staticmethod |
| 447 |
@jit.elidable |
| 448 |
def ll_strip(s, ch, left, right): |
| 449 |
s_len = len(s.chars) |
| 450 |
if s_len == 0: |
| 451 |
return s.empty() |
| 452 |
lpos = 0 |
| 453 |
rpos = s_len - 1 |
| 454 |
if left: |
| 455 |
while lpos <= rpos and s.chars[lpos] == ch: |
| 456 |
lpos += 1 |
| 457 |
if right: |
| 458 |
while lpos <= rpos and s.chars[rpos] == ch: |
| 459 |
rpos -= 1 |
| 460 |
if rpos < lpos: |
| 461 |
return s.empty() |
| 462 |
r_len = rpos - lpos + 1 |
| 463 |
result = s.malloc(r_len) |
| 464 |
s.copy_contents(s, result, lpos, 0, r_len) |
| 465 |
return result |
| 466 |
|
| 467 |
@staticmethod |
| 468 |
@jit.elidable |
| 469 |
def ll_strip_default(s, left, right): |
| 470 |
s_len = len(s.chars) |
| 471 |
if s_len == 0: |
| 472 |
return s.empty() |
| 473 |
lpos = 0 |
| 474 |
rpos = s_len - 1 |
| 475 |
if left: |
| 476 |
while lpos <= rpos and s.chars[lpos].isspace(): |
| 477 |
lpos += 1 |
| 478 |
if right: |
| 479 |
while lpos <= rpos and s.chars[rpos].isspace(): |
| 480 |
rpos -= 1 |
| 481 |
if rpos < lpos: |
| 482 |
return s.empty() |
| 483 |
r_len = rpos - lpos + 1 |
| 484 |
result = s.malloc(r_len) |
| 485 |
s.copy_contents(s, result, lpos, 0, r_len) |
| 486 |
return result |
| 487 |
|
| 488 |
@staticmethod |
| 489 |
@jit.elidable |
| 490 |
def ll_strip_multiple(s, s2, left, right): |
| 491 |
s_len = len(s.chars) |
| 492 |
if s_len == 0: |
| 493 |
return s.empty() |
| 494 |
lpos = 0 |
| 495 |
rpos = s_len - 1 |
| 496 |
if left: |
| 497 |
while lpos <= rpos and LLHelpers.ll_contains(s2, s.chars[lpos]): |
| 498 |
lpos += 1 |
| 499 |
if right: |
| 500 |
while lpos <= rpos and LLHelpers.ll_contains(s2, s.chars[rpos]): |
| 501 |
rpos -= 1 |
| 502 |
if rpos < lpos: |
| 503 |
return s.empty() |
| 504 |
r_len = rpos - lpos + 1 |
| 505 |
result = s.malloc(r_len) |
| 506 |
s.copy_contents(s, result, lpos, 0, r_len) |
| 507 |
return result |
| 508 |
|
| 509 |
@staticmethod |
| 510 |
@jit.elidable |
| 511 |
def ll_upper(s): |
| 512 |
s_chars = s.chars |
| 513 |
s_len = len(s_chars) |
| 514 |
if s_len == 0: |
| 515 |
return s.empty() |
| 516 |
i = 0 |
| 517 |
result = mallocstr(s_len) |
| 518 |
|
| 519 |
while i < s_len: |
| 520 |
result.chars[i] = LLHelpers.ll_upper_char(s_chars[i]) |
| 521 |
i += 1 |
| 522 |
return result |
| 523 |
|
| 524 |
@staticmethod |
| 525 |
@jit.elidable |
| 526 |
def ll_lower(s): |
| 527 |
s_chars = s.chars |
| 528 |
s_len = len(s_chars) |
| 529 |
if s_len == 0: |
| 530 |
return s.empty() |
| 531 |
i = 0 |
| 532 |
result = mallocstr(s_len) |
| 533 |
|
| 534 |
while i < s_len: |
| 535 |
result.chars[i] = LLHelpers.ll_lower_char(s_chars[i]) |
| 536 |
i += 1 |
| 537 |
return result |
| 538 |
|
| 539 |
@staticmethod |
| 540 |
def ll_join(s, length, items): |
| 541 |
s_chars = s.chars |
| 542 |
TYP = typeOf(s) |
| 543 |
s_len = len(s_chars) |
| 544 |
num_items = length |
| 545 |
if num_items == 0: |
| 546 |
return s.empty() |
| 547 |
itemslen = 0 |
| 548 |
i = 0 |
| 549 |
while i < num_items: |
| 550 |
try: |
| 551 |
itemslen = ovfcheck(itemslen + len(llmemory.cast_any_ptr(TYP, items[i]).chars)) |
| 552 |
except OverflowError: |
| 553 |
raise MemoryError |
| 554 |
i += 1 |
| 555 |
try: |
| 556 |
seplen = ovfcheck(s_len * (num_items - 1)) |
| 557 |
except OverflowError: |
| 558 |
raise MemoryError |
| 559 |
|
| 560 |
|
| 561 |
result = s.malloc(itemslen + seplen) |
| 562 |
items0 = llmemory.cast_any_ptr(TYP, items[0]) |
| 563 |
res_index = len(items0.chars) |
| 564 |
s.copy_contents(items0, result, 0, 0, res_index) |
| 565 |
i = 1 |
| 566 |
while i < num_items: |
| 567 |
s.copy_contents(s, result, 0, res_index, s_len) |
| 568 |
res_index += s_len |
| 569 |
item = llmemory.cast_any_ptr(TYP, items[i]) |
| 570 |
lgt = len(item.chars) |
| 571 |
s.copy_contents(item, result, 0, res_index, lgt) |
| 572 |
res_index += lgt |
| 573 |
i += 1 |
| 574 |
return result |
| 575 |
|
| 576 |
@staticmethod |
| 577 |
@jit.elidable |
| 578 |
@jit.oopspec('stroruni.cmp(s1, s2)') |
| 579 |
def ll_strcmp(s1, s2): |
| 580 |
if not s1 and not s2: |
| 581 |
return True |
| 582 |
if not s1 or not s2: |
| 583 |
return False |
| 584 |
chars1 = s1.chars |
| 585 |
chars2 = s2.chars |
| 586 |
len1 = len(chars1) |
| 587 |
len2 = len(chars2) |
| 588 |
|
| 589 |
if len1 < len2: |
| 590 |
cmplen = len1 |
| 591 |
else: |
| 592 |
cmplen = len2 |
| 593 |
i = 0 |
| 594 |
while i < cmplen: |
| 595 |
diff = ord(chars1[i]) - ord(chars2[i]) |
| 596 |
if diff != 0: |
| 597 |
return diff |
| 598 |
i += 1 |
| 599 |
return len1 - len2 |
| 600 |
|
| 601 |
@staticmethod |
| 602 |
@jit.elidable |
| 603 |
@jit.oopspec('stroruni.equal(s1, s2)') |
| 604 |
def ll_streq(s1, s2): |
| 605 |
if s1 == s2: |
| 606 |
return True |
| 607 |
if not s1 or not s2: |
| 608 |
return False |
| 609 |
len1 = len(s1.chars) |
| 610 |
len2 = len(s2.chars) |
| 611 |
if len1 != len2: |
| 612 |
return False |
| 613 |
j = 0 |
| 614 |
chars1 = s1.chars |
| 615 |
chars2 = s2.chars |
| 616 |
while j < len1: |
| 617 |
if chars1[j] != chars2[j]: |
| 618 |
return False |
| 619 |
j += 1 |
| 620 |
return True |
| 621 |
|
| 622 |
@staticmethod |
| 623 |
@jit.elidable |
| 624 |
def ll_startswith(s1, s2): |
| 625 |
len1 = len(s1.chars) |
| 626 |
len2 = len(s2.chars) |
| 627 |
if len1 < len2: |
| 628 |
return False |
| 629 |
j = 0 |
| 630 |
chars1 = s1.chars |
| 631 |
chars2 = s2.chars |
| 632 |
while j < len2: |
| 633 |
if chars1[j] != chars2[j]: |
| 634 |
return False |
| 635 |
j += 1 |
| 636 |
|
| 637 |
return True |
| 638 |
|
| 639 |
@staticmethod |
| 640 |
def ll_startswith_char(s, ch): |
| 641 |
if not len(s.chars): |
| 642 |
return False |
| 643 |
return s.chars[0] == ch |
| 644 |
|
| 645 |
@staticmethod |
| 646 |
@jit.elidable |
| 647 |
def ll_endswith(s1, s2): |
| 648 |
len1 = len(s1.chars) |
| 649 |
len2 = len(s2.chars) |
| 650 |
if len1 < len2: |
| 651 |
return False |
| 652 |
j = 0 |
| 653 |
chars1 = s1.chars |
| 654 |
chars2 = s2.chars |
| 655 |
offset = len1 - len2 |
| 656 |
while j < len2: |
| 657 |
if chars1[offset + j] != chars2[j]: |
| 658 |
return False |
| 659 |
j += 1 |
| 660 |
|
| 661 |
return True |
| 662 |
|
| 663 |
@staticmethod |
| 664 |
def ll_endswith_char(s, ch): |
| 665 |
if not len(s.chars): |
| 666 |
return False |
| 667 |
return s.chars[len(s.chars) - 1] == ch |
| 668 |
|
| 669 |
@staticmethod |
| 670 |
@jit.elidable |
| 671 |
@signature(types.any(), types.any(), types.int(), types.int(), returns=types.int()) |
| 672 |
def ll_find_char(s, ch, start, end): |
| 673 |
i = start |
| 674 |
if end > len(s.chars): |
| 675 |
end = len(s.chars) |
| 676 |
while i < end: |
| 677 |
if s.chars[i] == ch: |
| 678 |
return i |
| 679 |
i += 1 |
| 680 |
return -1 |
| 681 |
|
| 682 |
@staticmethod |
| 683 |
@jit.elidable |
| 684 |
@signature(types.any(), types.any(), types.int(), types.int(), returns=types.int()) |
| 685 |
def ll_rfind_char(s, ch, start, end): |
| 686 |
if end > len(s.chars): |
| 687 |
end = len(s.chars) |
| 688 |
i = end |
| 689 |
while i > start: |
| 690 |
i -= 1 |
| 691 |
if s.chars[i] == ch: |
| 692 |
return i |
| 693 |
return -1 |
| 694 |
|
| 695 |
@staticmethod |
| 696 |
@jit.elidable |
| 697 |
def ll_count_char(s, ch, start, end): |
| 698 |
count = 0 |
| 699 |
i = start |
| 700 |
if end > len(s.chars): |
| 701 |
end = len(s.chars) |
| 702 |
while i < end: |
| 703 |
if s.chars[i] == ch: |
| 704 |
count += 1 |
| 705 |
i += 1 |
| 706 |
return count |
| 707 |
|
| 708 |
@staticmethod |
| 709 |
@signature(types.any(), types.any(), types.int(), types.int(), returns=types.int()) |
| 710 |
def ll_find(s1, s2, start, end): |
| 711 |
from rpython.rlib.rstring import SEARCH_FIND |
| 712 |
if start < 0: |
| 713 |
start = 0 |
| 714 |
n = len(s1.chars) |
| 715 |
if end > n: |
| 716 |
end = n |
| 717 |
if end - start < 0: |
| 718 |
return -1 |
| 719 |
|
| 720 |
m = len(s2.chars) |
| 721 |
if m <= 1: |
| 722 |
if m == 0: |
| 723 |
return start |
| 724 |
res = LLHelpers.ll_find_char(s1, s2.chars[0], start, end) |
| 725 |
jit.record_exact_value(res < end, True) |
| 726 |
return res |
| 727 |
|
| 728 |
res = LLHelpers.ll_search(s1, s2, start, end, SEARCH_FIND) |
| 729 |
jit.record_exact_value(res < end, True) |
| 730 |
return res |
| 731 |
|
| 732 |
@staticmethod |
| 733 |
@signature(types.any(), types.any(), types.int(), types.int(), returns=types.int()) |
| 734 |
def ll_rfind(s1, s2, start, end): |
| 735 |
from rpython.rlib.rstring import SEARCH_RFIND |
| 736 |
if start < 0: |
| 737 |
start = 0 |
| 738 |
if end > len(s1.chars): |
| 739 |
end = len(s1.chars) |
| 740 |
if end - start < 0: |
| 741 |
return -1 |
| 742 |
|
| 743 |
m = len(s2.chars) |
| 744 |
if m <= 1: |
| 745 |
if m == 0: |
| 746 |
return end |
| 747 |
res = LLHelpers.ll_rfind_char(s1, s2.chars[0], start, end) |
| 748 |
jit.record_exact_value(res < end, True) |
| 749 |
return res |
| 750 |
|
| 751 |
res = LLHelpers.ll_search(s1, s2, start, end, SEARCH_RFIND) |
| 752 |
jit.record_exact_value(res < end, True) |
| 753 |
return res |
| 754 |
|
| 755 |
@classmethod |
| 756 |
def ll_count(cls, s1, s2, start, end): |
| 757 |
from rpython.rlib.rstring import SEARCH_COUNT |
| 758 |
if start < 0: |
| 759 |
start = 0 |
| 760 |
if end > len(s1.chars): |
| 761 |
end = len(s1.chars) |
| 762 |
if end - start < 0: |
| 763 |
return 0 |
| 764 |
|
| 765 |
m = len(s2.chars) |
| 766 |
if m == 1: |
| 767 |
return cls.ll_count_char(s1, s2.chars[0], start, end) |
| 768 |
|
| 769 |
res = cls.ll_search(s1, s2, start, end, SEARCH_COUNT) |
| 770 |
assert res >= 0 |
| 771 |
return res |
| 772 |
|
| 773 |
@staticmethod |
| 774 |
def ll_search(s1, s2, start, end, mode): |
| 775 |
from rpython.rtyper.annlowlevel import hlstr, hlunicode |
| 776 |
from rpython.rlib import rstring |
| 777 |
tp = typeOf(s1) |
| 778 |
if tp == string_repr.lowleveltype or tp == Char: |
| 779 |
return rstring._search(hlstr(s1), hlstr(s2), start, end, mode) |
| 780 |
else: |
| 781 |
return rstring._search(hlunicode(s1), hlunicode(s2), start, end, mode) |
| 782 |
|
| 783 |
@staticmethod |
| 784 |
@signature(types.int(), types.any(), types.any(), returns=types.any()) |
| 785 |
@jit.look_inside_iff(lambda length, items, TYP: jit.loop_unrolling_heuristic( |
| 786 |
items, length)) |
| 787 |
def ll_join_strs(length, items, TYP): |
| 788 |
|
| 789 |
if length == 1: |
| 790 |
return llmemory.cast_any_ptr(TYP, items[0]) |
| 791 |
|
| 792 |
num_items = length |
| 793 |
itemslen = 0 |
| 794 |
i = 0 |
| 795 |
while i < num_items: |
| 796 |
try: |
| 797 |
item = llmemory.cast_any_ptr(TYP, items[i]) |
| 798 |
itemslen = ovfcheck(itemslen + len(item.chars)) |
| 799 |
except OverflowError: |
| 800 |
raise MemoryError |
| 801 |
i += 1 |
| 802 |
if TYP.TO == STR: |
| 803 |
malloc = mallocstr |
| 804 |
copy_contents = copy_string_contents |
| 805 |
else: |
| 806 |
malloc = mallocunicode |
| 807 |
copy_contents = copy_unicode_contents |
| 808 |
result = malloc(itemslen) |
| 809 |
res_index = 0 |
| 810 |
i = 0 |
| 811 |
while i < num_items: |
| 812 |
item = llmemory.cast_any_ptr(TYP, items[i]) |
| 813 |
item_chars = item.chars |
| 814 |
item_len = len(item_chars) |
| 815 |
copy_contents(item, result, 0, res_index, item_len) |
| 816 |
res_index += item_len |
| 817 |
i += 1 |
| 818 |
return result |
| 819 |
|
| 820 |
@staticmethod |
| 821 |
@jit.look_inside_iff(lambda length, chars, RES: jit.isconstant(length) and jit.isvirtual(chars)) |
| 822 |
def ll_join_chars(length, chars, RES): |
| 823 |
|
| 824 |
|
| 825 |
num_chars = length |
| 826 |
if RES is StringRepr.lowleveltype: |
| 827 |
target = Char |
| 828 |
malloc = mallocstr |
| 829 |
else: |
| 830 |
target = UniChar |
| 831 |
malloc = mallocunicode |
| 832 |
result = malloc(num_chars) |
| 833 |
res_chars = result.chars |
| 834 |
i = 0 |
| 835 |
while i < num_chars: |
| 836 |
res_chars[i] = cast_primitive(target, chars[i]) |
| 837 |
i += 1 |
| 838 |
return result |
| 839 |
|
| 840 |
@staticmethod |
| 841 |
@jit.oopspec('stroruni.slice(s1, start, stop)') |
| 842 |
@signature(types.any(), types.int(), types.int(), returns=types.any()) |
| 843 |
@jit.elidable |
| 844 |
def _ll_stringslice(s1, start, stop): |
| 845 |
lgt = stop - start |
| 846 |
assert start >= 0 |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
if lgt < 0: |
| 851 |
return s1.empty() |
| 852 |
newstr = s1.malloc(lgt) |
| 853 |
s1.copy_contents(s1, newstr, start, 0, lgt) |
| 854 |
return newstr |
| 855 |
|
| 856 |
@staticmethod |
| 857 |
def ll_stringslice_startonly(s1, start): |
| 858 |
return LLHelpers._ll_stringslice(s1, start, len(s1.chars)) |
| 859 |
|
| 860 |
@staticmethod |
| 861 |
@signature(types.any(), types.int(), types.int(), returns=types.any()) |
| 862 |
def ll_stringslice_startstop(s1, start, stop): |
| 863 |
if jit.we_are_jitted(): |
| 864 |
if stop > len(s1.chars): |
| 865 |
stop = len(s1.chars) |
| 866 |
else: |
| 867 |
if stop >= len(s1.chars): |
| 868 |
if start == 0: |
| 869 |
return s1 |
| 870 |
stop = len(s1.chars) |
| 871 |
return LLHelpers._ll_stringslice(s1, start, stop) |
| 872 |
|
| 873 |
@staticmethod |
| 874 |
def ll_stringslice_minusone(s1): |
| 875 |
newlen = len(s1.chars) - 1 |
| 876 |
return LLHelpers._ll_stringslice(s1, 0, newlen) |
| 877 |
|
| 878 |
@staticmethod |
| 879 |
def ll_split_chr(LIST, s, c, max): |
| 880 |
chars = s.chars |
| 881 |
strlen = len(chars) |
| 882 |
count = 1 |
| 883 |
i = 0 |
| 884 |
if max == 0: |
| 885 |
i = strlen |
| 886 |
while i < strlen: |
| 887 |
if chars[i] == c: |
| 888 |
count += 1 |
| 889 |
if max >= 0 and count > max: |
| 890 |
break |
| 891 |
i += 1 |
| 892 |
res = LIST.ll_newlist(count) |
| 893 |
items = res.ll_items() |
| 894 |
ITEM = typeOf(items).TO.OF |
| 895 |
if count == 1: |
| 896 |
items[0] = llmemory.cast_any_ptr(ITEM, s) |
| 897 |
return res |
| 898 |
i = 0 |
| 899 |
j = 0 |
| 900 |
resindex = 0 |
| 901 |
if max == 0: |
| 902 |
j = strlen |
| 903 |
while j < strlen: |
| 904 |
if chars[j] == c: |
| 905 |
item = s.malloc(j - i) |
| 906 |
item.copy_contents(s, item, i, 0, j - i) |
| 907 |
items[resindex] = llmemory.cast_any_ptr(ITEM, item) |
| 908 |
resindex += 1 |
| 909 |
i = j + 1 |
| 910 |
if max >= 0 and resindex >= max: |
| 911 |
j = strlen |
| 912 |
break |
| 913 |
j += 1 |
| 914 |
item = s.malloc(j - i) |
| 915 |
items[resindex] = llmemory.cast_any_ptr(ITEM, item) |
| 916 |
item.copy_contents(s, item, i, 0, j - i) |
| 917 |
return res |
| 918 |
|
| 919 |
@staticmethod |
| 920 |
def ll_split(LIST, s, c, max): |
| 921 |
count = 1 |
| 922 |
if max == -1: |
| 923 |
max = len(s.chars) |
| 924 |
pos = 0 |
| 925 |
last = len(s.chars) |
| 926 |
markerlen = len(c.chars) |
| 927 |
pos = s.find(c, 0, last) |
| 928 |
while pos >= 0 and count <= max: |
| 929 |
pos = s.find(c, pos + markerlen, last) |
| 930 |
count += 1 |
| 931 |
res = LIST.ll_newlist(count) |
| 932 |
items = res.ll_items() |
| 933 |
ITEM = typeOf(items).TO.OF |
| 934 |
if count == 1: |
| 935 |
items[0] = llmemory.cast_any_ptr(ITEM, s) |
| 936 |
return res |
| 937 |
pos = 0 |
| 938 |
count = 0 |
| 939 |
pos = s.find(c, 0, last) |
| 940 |
prev_pos = 0 |
| 941 |
while pos >= 0 and count < max: |
| 942 |
item = s.malloc(pos - prev_pos) |
| 943 |
item.copy_contents(s, item, prev_pos, 0, pos - |
| 944 |
prev_pos) |
| 945 |
items[count] = llmemory.cast_any_ptr(ITEM, item) |
| 946 |
count += 1 |
| 947 |
prev_pos = pos + markerlen |
| 948 |
pos = s.find(c, pos + markerlen, last) |
| 949 |
item = s.malloc(last - prev_pos) |
| 950 |
item.copy_contents(s, item, prev_pos, 0, last - prev_pos) |
| 951 |
items[count] = llmemory.cast_any_ptr(ITEM, item) |
| 952 |
return res |
| 953 |
|
| 954 |
@staticmethod |
| 955 |
def ll_rsplit_chr(LIST, s, c, max): |
| 956 |
chars = s.chars |
| 957 |
strlen = len(chars) |
| 958 |
count = 1 |
| 959 |
i = 0 |
| 960 |
if max == 0: |
| 961 |
i = strlen |
| 962 |
while i < strlen: |
| 963 |
if chars[i] == c: |
| 964 |
count += 1 |
| 965 |
if max >= 0 and count > max: |
| 966 |
break |
| 967 |
i += 1 |
| 968 |
res = LIST.ll_newlist(count) |
| 969 |
items = res.ll_items() |
| 970 |
ITEM = typeOf(items).TO.OF |
| 971 |
if count == 1: |
| 972 |
items[0] = llmemory.cast_any_ptr(ITEM, s) |
| 973 |
return res |
| 974 |
i = strlen |
| 975 |
j = strlen |
| 976 |
resindex = count - 1 |
| 977 |
assert resindex >= 0 |
| 978 |
if max == 0: |
| 979 |
j = 0 |
| 980 |
while j > 0: |
| 981 |
j -= 1 |
| 982 |
if chars[j] == c: |
| 983 |
item = s.malloc(i - j - 1) |
| 984 |
item.copy_contents(s, item, j + 1, 0, i - j - 1) |
| 985 |
items[resindex] = llmemory.cast_any_ptr(ITEM, item) |
| 986 |
resindex -= 1 |
| 987 |
i = j |
| 988 |
if resindex == 0: |
| 989 |
j = 0 |
| 990 |
break |
| 991 |
item = s.malloc(i - j) |
| 992 |
item.copy_contents(s, item, j, 0, i - j) |
| 993 |
items[resindex] = llmemory.cast_any_ptr(ITEM, item) |
| 994 |
return res |
| 995 |
|
| 996 |
@staticmethod |
| 997 |
def ll_rsplit(LIST, s, c, max): |
| 998 |
count = 1 |
| 999 |
if max == -1: |
| 1000 |
max = len(s.chars) |
| 1001 |
pos = len(s.chars) |
| 1002 |
markerlen = len(c.chars) |
| 1003 |
pos = s.rfind(c, 0, pos) |
| 1004 |
while pos >= 0 and count <= max: |
| 1005 |
pos = s.rfind(c, 0, pos - markerlen) |
| 1006 |
count += 1 |
| 1007 |
res = LIST.ll_newlist(count) |
| 1008 |
items = res.ll_items() |
| 1009 |
ITEM = typeOf(items).TO.OF |
| 1010 |
if count == 1: |
| 1011 |
items[0] = llmemory.cast_any_ptr(ITEM, s) |
| 1012 |
return res |
| 1013 |
pos = len(s.chars) |
| 1014 |
prev_pos = pos |
| 1015 |
pos = s.rfind(c, 0, pos) |
| 1016 |
count -= 1 |
| 1017 |
while pos >= 0 and count > 0: |
| 1018 |
item = s.malloc(prev_pos - pos - markerlen) |
| 1019 |
item.copy_contents(s, item, pos + markerlen, 0, |
| 1020 |
prev_pos - pos - markerlen) |
| 1021 |
items[count] = llmemory.cast_any_ptr(ITEM, item) |
| 1022 |
count -= 1 |
| 1023 |
prev_pos = pos |
| 1024 |
pos = s.rfind(c, 0, pos) |
| 1025 |
item = s.malloc(prev_pos) |
| 1026 |
item.copy_contents(s, item, 0, 0, prev_pos) |
| 1027 |
items[count] = llmemory.cast_any_ptr(ITEM, item) |
| 1028 |
return res |
| 1029 |
|
| 1030 |
@staticmethod |
| 1031 |
@jit.elidable |
| 1032 |
def ll_replace_chr_chr(s, c1, c2): |
| 1033 |
length = len(s.chars) |
| 1034 |
newstr = s.malloc(length) |
| 1035 |
src = s.chars |
| 1036 |
dst = newstr.chars |
| 1037 |
j = 0 |
| 1038 |
while j < length: |
| 1039 |
c = src[j] |
| 1040 |
if c == c1: |
| 1041 |
c = c2 |
| 1042 |
dst[j] = c |
| 1043 |
j += 1 |
| 1044 |
return newstr |
| 1045 |
|
| 1046 |
@staticmethod |
| 1047 |
@jit.elidable |
| 1048 |
def ll_contains(s, c): |
| 1049 |
chars = s.chars |
| 1050 |
strlen = len(chars) |
| 1051 |
i = 0 |
| 1052 |
while i < strlen: |
| 1053 |
if chars[i] == c: |
| 1054 |
return True |
| 1055 |
i += 1 |
| 1056 |
return False |
| 1057 |
|
| 1058 |
@staticmethod |
| 1059 |
@jit.elidable |
| 1060 |
def ll_int(s, base): |
| 1061 |
if not 2 <= base <= 36: |
| 1062 |
raise ValueError |
| 1063 |
chars = s.chars |
| 1064 |
strlen = len(chars) |
| 1065 |
i = 0 |
| 1066 |
|
| 1067 |
while i < strlen and ord(chars[i]) == ord(' '): |
| 1068 |
i += 1 |
| 1069 |
if not i < strlen: |
| 1070 |
raise ValueError |
| 1071 |
|
| 1072 |
sign = 1 |
| 1073 |
if ord(chars[i]) == ord('-'): |
| 1074 |
sign = -1 |
| 1075 |
i += 1 |
| 1076 |
elif ord(chars[i]) == ord('+'): |
| 1077 |
i += 1 |
| 1078 |
|
| 1079 |
while i < strlen and ord(chars[i]) == ord(' '): |
| 1080 |
i += 1 |
| 1081 |
|
| 1082 |
val = 0 |
| 1083 |
oldpos = i |
| 1084 |
while i < strlen: |
| 1085 |
c = ord(chars[i]) |
| 1086 |
if ord('a') <= c <= ord('z'): |
| 1087 |
digit = c - ord('a') + 10 |
| 1088 |
elif ord('A') <= c <= ord('Z'): |
| 1089 |
digit = c - ord('A') + 10 |
| 1090 |
elif ord('0') <= c <= ord('9'): |
| 1091 |
digit = c - ord('0') |
| 1092 |
else: |
| 1093 |
break |
| 1094 |
if digit >= base: |
| 1095 |
break |
| 1096 |
val = val * base + digit |
| 1097 |
i += 1 |
| 1098 |
if i == oldpos: |
| 1099 |
raise ValueError |
| 1100 |
|
| 1101 |
while i < strlen and ord(chars[i]) == ord(' '): |
| 1102 |
i += 1 |
| 1103 |
if not i == strlen: |
| 1104 |
raise ValueError |
| 1105 |
return sign * val |
| 1106 |
|
| 1107 |
@staticmethod |
| 1108 |
@specialize.memo() |
| 1109 |
def ll_constant(s): |
| 1110 |
return string_repr.convert_const(s) |
| 1111 |
|
| 1112 |
@staticmethod |
| 1113 |
@specialize.memo() |
| 1114 |
def ll_constant_unicode(s): |
| 1115 |
return unicode_repr.convert_const(s) |
| 1116 |
|
| 1117 |
@classmethod |
| 1118 |
def do_stringformat(cls, hop, sourcevarsrepr): |
| 1119 |
s_str = hop.args_s[0] |
| 1120 |
assert s_str.is_constant() |
| 1121 |
is_unicode = isinstance(s_str, annmodel.SomeUnicodeString) |
| 1122 |
if is_unicode: |
| 1123 |
TEMPBUF = TEMP_UNICODE |
| 1124 |
else: |
| 1125 |
TEMPBUF = TEMP |
| 1126 |
s = s_str.const |
| 1127 |
things = cls.parse_fmt_string(s) |
| 1128 |
size = inputconst(Signed, len(things)) |
| 1129 |
cTEMP = inputconst(Void, TEMPBUF) |
| 1130 |
cflags = inputconst(Void, {'flavor': 'gc'}) |
| 1131 |
vtemp = hop.genop("malloc_varsize", [cTEMP, cflags, size], |
| 1132 |
resulttype=Ptr(TEMPBUF)) |
| 1133 |
|
| 1134 |
argsiter = iter(sourcevarsrepr) |
| 1135 |
|
| 1136 |
from rpython.rtyper.rclass import InstanceRepr |
| 1137 |
for i, thing in enumerate(things): |
| 1138 |
if isinstance(thing, tuple): |
| 1139 |
code = thing[0] |
| 1140 |
vitem, r_arg = argsiter.next() |
| 1141 |
if not hasattr(r_arg, 'll_str'): |
| 1142 |
raise TyperError("ll_str unsupported for: %r" % r_arg) |
| 1143 |
if code == 's': |
| 1144 |
if is_unicode: |
| 1145 |
|
| 1146 |
vchunk = hop.gendirectcall(r_arg.ll_unicode, vitem) |
| 1147 |
else: |
| 1148 |
vchunk = hop.gendirectcall(r_arg.ll_str, vitem) |
| 1149 |
elif code == 'r' and isinstance(r_arg, InstanceRepr): |
| 1150 |
vchunk = hop.gendirectcall(r_arg.ll_str, vitem) |
| 1151 |
elif code == 'd': |
| 1152 |
assert isinstance(r_arg, IntegerRepr) |
| 1153 |
|
| 1154 |
assert isinstance(r_arg, BoolRepr) or r_arg.opprefix is not None |
| 1155 |
|
| 1156 |
vchunk = hop.gendirectcall(ll_str.ll_int2dec, vitem) |
| 1157 |
elif code == 'f': |
| 1158 |
|
| 1159 |
vchunk = hop.gendirectcall(r_arg.ll_str, vitem) |
| 1160 |
elif code == 'x': |
| 1161 |
assert isinstance(r_arg, IntegerRepr) |
| 1162 |
|
| 1163 |
assert isinstance(r_arg, BoolRepr) or r_arg.opprefix is not None |
| 1164 |
vchunk = hop.gendirectcall(ll_str.ll_int2hex, vitem, |
| 1165 |
inputconst(Bool, False)) |
| 1166 |
elif code == 'o': |
| 1167 |
assert isinstance(r_arg, IntegerRepr) |
| 1168 |
|
| 1169 |
assert isinstance(r_arg, BoolRepr) or r_arg.opprefix is not None |
| 1170 |
vchunk = hop.gendirectcall(ll_str.ll_int2oct, vitem, |
| 1171 |
inputconst(Bool, False)) |
| 1172 |
else: |
| 1173 |
raise TyperError("%%%s is not RPython" % (code,)) |
| 1174 |
else: |
| 1175 |
if is_unicode: |
| 1176 |
vchunk = inputconst(unicode_repr, thing) |
| 1177 |
else: |
| 1178 |
vchunk = inputconst(string_repr, thing) |
| 1179 |
i = inputconst(Signed, i) |
| 1180 |
if is_unicode and vchunk.concretetype != Ptr(UNICODE): |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
vchunk = hop.gendirectcall(cls.ll_str2unicode, vchunk) |
| 1185 |
hop.genop('setarrayitem', [vtemp, i, vchunk]) |
| 1186 |
|
| 1187 |
hop.exception_cannot_occur() |
| 1188 |
c_tp = inputconst(Void, TEMPBUF.OF) |
| 1189 |
return hop.gendirectcall(cls.ll_join_strs, size, vtemp, c_tp) |
| 1190 |
|
| 1191 |
@staticmethod |
| 1192 |
@jit.dont_look_inside |
| 1193 |
def ll_string2list(RESLIST, src): |
| 1194 |
length = len(src.chars) |
| 1195 |
lst = RESLIST.ll_newlist(length) |
| 1196 |
dst = lst.ll_items() |
| 1197 |
SRC = typeOf(src).TO |
| 1198 |
DST = typeOf(dst).TO |
| 1199 |
assert DST.OF is SRC.chars.OF |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
if rgc.must_split_gc_address_space(): |
| 1204 |
i = 0 |
| 1205 |
while i < length: |
| 1206 |
dst[i] = src.chars[i] |
| 1207 |
i += 1 |
| 1208 |
return lst |
| 1209 |
|
| 1210 |
|
| 1211 |
asrc = llmemory.cast_ptr_to_adr(src) + ( |
| 1212 |
llmemory.offsetof(SRC, 'chars') + |
| 1213 |
llmemory.itemoffsetof(SRC.chars, 0)) |
| 1214 |
adst = llmemory.cast_ptr_to_adr(dst) + llmemory.itemoffsetof(DST, 0) |
| 1215 |
llmemory.raw_memcopy(asrc, adst, llmemory.sizeof(DST.OF) * length) |
| 1216 |
|
| 1217 |
keepalive_until_here(src) |
| 1218 |
keepalive_until_here(dst) |
| 1219 |
return lst |
| 1220 |
|
| 1221 |
TEMP = GcArray(Ptr(STR)) |
| 1222 |
TEMP_UNICODE = GcArray(Ptr(UNICODE)) |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
STR.become(GcStruct('rpy_string', ('hash', Signed), |
| 1227 |
('chars', Array(Char, hints={'immutable': True, |
| 1228 |
'extra_item_after_alloc': 1})), |
| 1229 |
adtmeths={'malloc' : staticAdtMethod(mallocstr), |
| 1230 |
'empty' : staticAdtMethod(emptystrfun), |
| 1231 |
'copy_contents' : staticAdtMethod(copy_string_contents), |
| 1232 |
'copy_contents_from_str' : staticAdtMethod(copy_string_contents), |
| 1233 |
'gethash': LLHelpers.ll_strhash, |
| 1234 |
'length': LLHelpers.ll_length, |
| 1235 |
'find': LLHelpers.ll_find, |
| 1236 |
'rfind': LLHelpers.ll_rfind}, |
| 1237 |
hints={'remove_hash': True})) |
| 1238 |
UNICODE.become(GcStruct('rpy_unicode', ('hash', Signed), |
| 1239 |
('chars', Array(UniChar, hints={'immutable': True})), |
| 1240 |
adtmeths={'malloc' : staticAdtMethod(mallocunicode), |
| 1241 |
'empty' : staticAdtMethod(emptyunicodefun), |
| 1242 |
'copy_contents' : staticAdtMethod(copy_unicode_contents), |
| 1243 |
'copy_contents_from_str' : staticAdtMethod(copy_unicode_contents), |
| 1244 |
'gethash': LLHelpers.ll_strhash, |
| 1245 |
'length': LLHelpers.ll_length}, |
| 1246 |
hints={'remove_hash': True})) |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
ll_strconcat = LLHelpers.ll_strconcat |
| 1251 |
ll_join = LLHelpers.ll_join |
| 1252 |
ll_str2unicode = LLHelpers.ll_str2unicode |
| 1253 |
do_stringformat = LLHelpers.do_stringformat |
| 1254 |
|
| 1255 |
string_repr = StringRepr() |
| 1256 |
char_repr = CharRepr() |
| 1257 |
unichar_repr = UniCharRepr() |
| 1258 |
char_repr.ll = LLHelpers |
| 1259 |
unichar_repr.ll = LLHelpers |
| 1260 |
unicode_repr = UnicodeRepr() |
| 1261 |
|
| 1262 |
StringRepr.repr = string_repr |
| 1263 |
UnicodeRepr.repr = unicode_repr |
| 1264 |
UniCharRepr.repr = unicode_repr |
| 1265 |
UniCharRepr.char_repr = unichar_repr |
| 1266 |
UnicodeRepr.char_repr = unichar_repr |
| 1267 |
CharRepr.char_repr = char_repr |
| 1268 |
StringRepr.char_repr = char_repr |
| 1269 |
|
| 1270 |
class BaseStringIteratorRepr(AbstractStringIteratorRepr): |
| 1271 |
|
| 1272 |
def __init__(self): |
| 1273 |
self.ll_striter = ll_striter |
| 1274 |
self.ll_strnext = ll_strnext |
| 1275 |
self.ll_getnextindex = ll_getnextindex |
| 1276 |
|
| 1277 |
class StringIteratorRepr(BaseStringIteratorRepr): |
| 1278 |
|
| 1279 |
external_item_repr = char_repr |
| 1280 |
lowleveltype = Ptr(GcStruct('stringiter', |
| 1281 |
('string', string_repr.lowleveltype), |
| 1282 |
('length', Signed), |
| 1283 |
('index', Signed))) |
| 1284 |
|
| 1285 |
class UnicodeIteratorRepr(BaseStringIteratorRepr): |
| 1286 |
|
| 1287 |
external_item_repr = unichar_repr |
| 1288 |
lowleveltype = Ptr(GcStruct('unicodeiter', |
| 1289 |
('string', unicode_repr.lowleveltype), |
| 1290 |
('length', Signed), |
| 1291 |
('index', Signed))) |
| 1292 |
|
| 1293 |
def ll_striter(string): |
| 1294 |
if typeOf(string) == string_repr.lowleveltype: |
| 1295 |
TP = string_repr.iterator_repr.lowleveltype.TO |
| 1296 |
elif typeOf(string) == unicode_repr.lowleveltype: |
| 1297 |
TP = unicode_repr.iterator_repr.lowleveltype.TO |
| 1298 |
else: |
| 1299 |
raise TypeError("Unknown string type %s" % (typeOf(string),)) |
| 1300 |
iter = malloc(TP) |
| 1301 |
iter.string = string |
| 1302 |
iter.length = len(string.chars) |
| 1303 |
iter.index = 0 |
| 1304 |
return iter |
| 1305 |
|
| 1306 |
def ll_strnext(iter): |
| 1307 |
index = iter.index |
| 1308 |
if index >= iter.length: |
| 1309 |
raise StopIteration |
| 1310 |
iter.index = index + 1 |
| 1311 |
return iter.string.chars[index] |
| 1312 |
|
| 1313 |
def ll_getnextindex(iter): |
| 1314 |
return iter.index |
| 1315 |
|
| 1316 |
string_repr.iterator_repr = StringIteratorRepr() |
| 1317 |
unicode_repr.iterator_repr = UnicodeIteratorRepr() |
| 1318 |
|
| 1319 |
@specialize.memo() |
| 1320 |
def conststr(s): |
| 1321 |
return string_repr.convert_const(s) |