| 1 |
from rpython.rlib import rutf8 |
| 2 |
from rpython.rlib.objectmodel import specialize |
| 3 |
from rpython.rlib.rstring import StringBuilder |
| 4 |
from rpython.rlib.rarithmetic import r_uint, r_ulonglong, intmask |
| 5 |
from rpython.rtyper.annlowlevel import llunicode |
| 6 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 7 |
from rpython.rtyper.lltypesystem.rstr import copy_unicode_to_raw |
| 8 |
|
| 9 |
|
| 10 |
class OutOfRange(Exception): |
| 11 |
ordinal = 0 |
| 12 |
|
| 13 |
def __init__(self, ordinal): |
| 14 |
ordinal = intmask(rffi.cast(rffi.INT, ordinal)) |
| 15 |
self.ordinal = ordinal |
| 16 |
|
| 17 |
def utf8_from_char32(ptr, length): |
| 18 |
|
| 19 |
ptr = rffi.cast(rffi.UINTP, ptr) |
| 20 |
u = StringBuilder(length) |
| 21 |
j = 0 |
| 22 |
while j < length: |
| 23 |
ch = intmask(ptr[j]) |
| 24 |
j += 1 |
| 25 |
try: |
| 26 |
rutf8.unichr_as_utf8_append(u, ch, allow_surrogates=True) |
| 27 |
except rutf8.OutOfRange: |
| 28 |
raise OutOfRange(ch) |
| 29 |
return u.build(), length |
| 30 |
|
| 31 |
def utf8_from_char16(ptr, length): |
| 32 |
|
| 33 |
ptr = rffi.cast(rffi.USHORTP, ptr) |
| 34 |
u = StringBuilder(length) |
| 35 |
j = 0 |
| 36 |
result_length = length |
| 37 |
while j < length: |
| 38 |
ch = intmask(ptr[j]) |
| 39 |
j += 1 |
| 40 |
if 0xD800 <= ch <= 0xDBFF and j < length: |
| 41 |
ch2 = intmask(ptr[j]) |
| 42 |
if 0xDC00 <= ch2 <= 0xDFFF: |
| 43 |
ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000 |
| 44 |
j += 1 |
| 45 |
result_length -= 1 |
| 46 |
rutf8.unichr_as_utf8_append(u, ch, allow_surrogates=True) |
| 47 |
return u.build(), result_length |
| 48 |
|
| 49 |
|
| 50 |
@specialize.ll() |
| 51 |
def _measure_length(ptr, maxlen): |
| 52 |
result = 0 |
| 53 |
if maxlen < 0: |
| 54 |
while intmask(ptr[result]) != 0: |
| 55 |
result += 1 |
| 56 |
else: |
| 57 |
while result < maxlen and intmask(ptr[result]) != 0: |
| 58 |
result += 1 |
| 59 |
return result |
| 60 |
|
| 61 |
def measure_length_16(ptr, maxlen=-1): |
| 62 |
return _measure_length(rffi.cast(rffi.USHORTP, ptr), maxlen) |
| 63 |
|
| 64 |
def measure_length_32(ptr, maxlen=-1): |
| 65 |
return _measure_length(rffi.cast(rffi.UINTP, ptr), maxlen) |
| 66 |
|
| 67 |
|
| 68 |
def utf8_size_as_char16(u): |
| 69 |
|
| 70 |
TABLE = "\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x01\x01\x01\x02" |
| 71 |
result = 0 |
| 72 |
for c in u: |
| 73 |
result += ord(TABLE[ord(c) >> 4]) |
| 74 |
return result |
| 75 |
|
| 76 |
def utf8_to_char32(utf8, target_ptr, target_length, add_final_zero): |
| 77 |
|
| 78 |
|
| 79 |
unichardata = rffi.cast(rffi.UINTP, target_ptr) |
| 80 |
i = 0 |
| 81 |
for j in range(target_length): |
| 82 |
code = rutf8.codepoint_at_pos(utf8, i) |
| 83 |
unichardata[j] = rffi.cast(rffi.UINT, code) |
| 84 |
i = rutf8.next_codepoint_pos(utf8, i) |
| 85 |
assert i == len(utf8) |
| 86 |
if add_final_zero: |
| 87 |
unichardata[target_length] = rffi.cast(rffi.UINT, 0) |
| 88 |
|
| 89 |
def utf8_to_char16(utf8, target_ptr, target_length, add_final_zero): |
| 90 |
|
| 91 |
|
| 92 |
ptr = rffi.cast(rffi.USHORTP, target_ptr) |
| 93 |
i = 0 |
| 94 |
while i < len(utf8): |
| 95 |
ordinal = rutf8.codepoint_at_pos(utf8, i) |
| 96 |
if ordinal > 0xFFFF: |
| 97 |
ordinal -= 0x10000 |
| 98 |
ptr[0] = rffi.cast(rffi.USHORT, 0xD800 | (ordinal >> 10)) |
| 99 |
ptr[1] = rffi.cast(rffi.USHORT, 0xDC00 | (ordinal & 0x3FF)) |
| 100 |
ptr = rffi.ptradd(ptr, 2) |
| 101 |
else: |
| 102 |
ptr[0] = rffi.cast(rffi.USHORT, ordinal) |
| 103 |
ptr = rffi.ptradd(ptr, 1) |
| 104 |
i = rutf8.next_codepoint_pos(utf8, i) |
| 105 |
assert ptr == ( |
| 106 |
rffi.ptradd(rffi.cast(rffi.USHORTP, target_ptr), target_length)) |
| 107 |
if add_final_zero: |
| 108 |
ptr[0] = rffi.cast(rffi.USHORT, 0) |