| 1 |
from rpython.rlib.rarithmetic import r_longlong, r_int32, r_uint32, intmask |
| 2 |
from rpython.rtyper.lltypesystem.rffi import r_ushort, r_short |
| 3 |
|
| 4 |
|
| 5 |
def signed_ord(c): |
| 6 |
x = ord(c) |
| 7 |
if x > 0x80: |
| 8 |
x -= 0x100 |
| 9 |
return x |
| 10 |
|
| 11 |
def _all_uint32(l): |
| 12 |
return [r_uint32(x) for x in l] |
| 13 |
def _all_int32(l): |
| 14 |
return [r_int32(x) for x in l] |
| 15 |
def _all_ushort(l): |
| 16 |
return [r_ushort(x) for x in l] |
| 17 |
def _all_short(l): |
| 18 |
return [r_short(x) for x in l] |
| 19 |
|
| 20 |
_cjk_prefix = "CJK UNIFIED IDEOGRAPH-" |
| 21 |
_hangul_prefix = 'HANGUL SYLLABLE ' |
| 22 |
|
| 23 |
_hangul_L = ['G', 'GG', 'N', 'D', 'DD', 'R', 'M', 'B', 'BB', |
| 24 |
'S', 'SS', '', 'J', 'JJ', 'C', 'K', 'T', 'P', 'H'] |
| 25 |
_hangul_V = ['A', 'AE', 'YA', 'YAE', 'EO', 'E', 'YEO', 'YE', 'O', 'WA', 'WAE', |
| 26 |
'OE', 'YO', 'U', 'WEO', 'WE', 'WI', 'YU', 'EU', 'YI', 'I'] |
| 27 |
_hangul_T = ['', 'G', 'GG', 'GS', 'N', 'NJ', 'NH', 'D', 'L', 'LG', 'LM', |
| 28 |
'LB', 'LS', 'LT', 'LP', 'LH', 'M', 'B', 'BS', 'S', 'SS', |
| 29 |
'NG', 'J', 'C', 'K', 'T', 'P', 'H'] |
| 30 |
|
| 31 |
def _lookup_hangul(syllables): |
| 32 |
from rpython.rlib.rstring import startswith |
| 33 |
l_code = v_code = t_code = -1 |
| 34 |
for i in range(len(_hangul_L)): |
| 35 |
jamo = _hangul_L[i] |
| 36 |
if (startswith(syllables, jamo) and |
| 37 |
(l_code < 0 or len(jamo) > len(_hangul_L[l_code]))): |
| 38 |
l_code = i |
| 39 |
if l_code < 0: |
| 40 |
raise KeyError |
| 41 |
start = len(_hangul_L[l_code]) |
| 42 |
|
| 43 |
for i in range(len(_hangul_V)): |
| 44 |
jamo = _hangul_V[i] |
| 45 |
if (syllables[start:start + len(jamo)] == jamo and |
| 46 |
(v_code < 0 or len(jamo) > len(_hangul_V[v_code]))): |
| 47 |
v_code = i |
| 48 |
if v_code < 0: |
| 49 |
raise KeyError |
| 50 |
start += len(_hangul_V[v_code]) |
| 51 |
|
| 52 |
for i in range(len(_hangul_T)): |
| 53 |
jamo = _hangul_T[i] |
| 54 |
if (syllables[start:start + len(jamo)] == jamo and |
| 55 |
(t_code < 0 or len(jamo) > len(_hangul_T[t_code]))): |
| 56 |
t_code = i |
| 57 |
if t_code < 0: |
| 58 |
raise KeyError |
| 59 |
start += len(_hangul_T[t_code]) |
| 60 |
|
| 61 |
if len(syllables[start:]): |
| 62 |
raise KeyError |
| 63 |
return 0xAC00 + (l_code * 21 + v_code) * 28 + t_code |
| 64 |
|