jntajis-python 0.2.0__cp314-cp314-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jntajis/__init__.py +129 -0
- jntajis/_jntajis.cp314-win32.pyd +4 -0
- jntajis/_jntajis.h +704511 -0
- jntajis/_jntajis.pyi +19 -0
- jntajis/_jntajis.pyx +1157 -0
- jntajis/_version.py +24 -0
- jntajis/gen.py +850 -0
- jntajis/py.typed +0 -0
- jntajis/pythoncapi_compat_shim.h +20 -0
- jntajis/xlsx_parser/__init__.py +1 -0
- jntajis/xlsx_parser/parser.py +591 -0
- jntajis/xlsx_parser/xmlutils.py +332 -0
- jntajis_python-0.2.0.dist-info/METADATA +119 -0
- jntajis_python-0.2.0.dist-info/RECORD +16 -0
- jntajis_python-0.2.0.dist-info/WHEEL +4 -0
- jntajis_python-0.2.0.dist-info/licenses/LICENSE +29 -0
jntajis/_jntajis.pyx
ADDED
|
@@ -0,0 +1,1157 @@
|
|
|
1
|
+
# cython: language_level=3, cdivision=True, boundscheck=False, wraparound=False, embedsignature=True, freethreading_compatible=True
|
|
2
|
+
cimport cython
|
|
3
|
+
from libc.stdio cimport snprintf
|
|
4
|
+
from libc.stdlib cimport malloc, calloc, free
|
|
5
|
+
from libc.string cimport memcpy
|
|
6
|
+
from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF
|
|
7
|
+
|
|
8
|
+
import enum
|
|
9
|
+
|
|
10
|
+
cdef extern from "./_jntajis.h":
|
|
11
|
+
ctypedef unsigned short uint8_t
|
|
12
|
+
ctypedef unsigned short uint16_t
|
|
13
|
+
ctypedef unsigned int uint32_t
|
|
14
|
+
ctypedef enum JISCharacterClass:
|
|
15
|
+
JISCharacterClass_RESERVED
|
|
16
|
+
JISCharacterClass_KANJI_LEVEL_1
|
|
17
|
+
JISCharacterClass_KANJI_LEVEL_2
|
|
18
|
+
JISCharacterClass_KANJI_LEVEL_3
|
|
19
|
+
JISCharacterClass_KANJI_LEVEL_4
|
|
20
|
+
JISCharacterClass_JISX0208_NON_KANJI
|
|
21
|
+
JISCharacterClass_JISX0213_NON_KANJI
|
|
22
|
+
ctypedef struct ShrinkingTransliterationMapping:
|
|
23
|
+
uint16_t jis
|
|
24
|
+
uint32_t[2] us
|
|
25
|
+
uint32_t[2] sus
|
|
26
|
+
JISCharacterClass class_
|
|
27
|
+
size_t tx_len
|
|
28
|
+
uint16_t[4] tx_jis
|
|
29
|
+
uint32_t[4] tx_us
|
|
30
|
+
ctypedef struct URangeToJISMapping:
|
|
31
|
+
uint32_t start, end
|
|
32
|
+
uint16_t* jis
|
|
33
|
+
ctypedef struct SMUniToJISTuple:
|
|
34
|
+
pass
|
|
35
|
+
const ShrinkingTransliterationMapping[] tx_mappings
|
|
36
|
+
const URangeToJISMapping[] urange_to_jis_mappings
|
|
37
|
+
uint16_t sm_uni_to_jis_mapping(int *state, uint32_t u) nogil
|
|
38
|
+
ctypedef struct UIVSPair:
|
|
39
|
+
uint32_t u
|
|
40
|
+
bint v
|
|
41
|
+
bint sv
|
|
42
|
+
uint8_t s
|
|
43
|
+
ctypedef struct MJMapping:
|
|
44
|
+
uint32_t mj
|
|
45
|
+
uint32_t iu
|
|
46
|
+
UIVSPair[4] v
|
|
47
|
+
ctypedef struct MJShrinkMappingUnicodeSet:
|
|
48
|
+
uint32_t[2] _0
|
|
49
|
+
uint32_t[1] _1
|
|
50
|
+
uint32_t[2] _2
|
|
51
|
+
uint32_t[3] _3
|
|
52
|
+
ctypedef struct MJMappingSet:
|
|
53
|
+
size_t l
|
|
54
|
+
MJMapping[64] ms
|
|
55
|
+
ctypedef struct URangeToMJMappings:
|
|
56
|
+
uint32_t start, end
|
|
57
|
+
const MJMappingSet* mss
|
|
58
|
+
const URangeToMJMappings[] urange_to_mj_mappings
|
|
59
|
+
const MJShrinkMappingUnicodeSet[] mj_shrink_mappings
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
cdef extern from "Python.h":
|
|
63
|
+
ctypedef int Py_UCS4
|
|
64
|
+
cdef enum PyUnicode_Kind:
|
|
65
|
+
PyUnicode_1BYTE_KIND
|
|
66
|
+
PyUnicode_2BYTE_KIND
|
|
67
|
+
PyUnicode_4BYTE_KIND
|
|
68
|
+
PyUnicode_Kind PyUnicode_KIND(object)
|
|
69
|
+
void* PyUnicode_DATA(object)
|
|
70
|
+
Py_ssize_t PyUnicode_GET_LENGTH(object)
|
|
71
|
+
Py_UCS4 PyUnicode_READ(int, void*, Py_ssize_t)
|
|
72
|
+
|
|
73
|
+
cdef extern from "pythoncapi_compat.h":
|
|
74
|
+
# PyUnicodeWriter_Finish() and PyUnicodeWriter_Discard() free the writer;
|
|
75
|
+
# it must not be touched afterwards.
|
|
76
|
+
ctypedef struct PyUnicodeWriter:
|
|
77
|
+
pass
|
|
78
|
+
PyUnicodeWriter* PyUnicodeWriter_Create(Py_ssize_t) except NULL
|
|
79
|
+
void PyUnicodeWriter_Discard(PyUnicodeWriter*)
|
|
80
|
+
int PyUnicodeWriter_WriteChar(PyUnicodeWriter*, Py_UCS4) except -1
|
|
81
|
+
int PyUnicodeWriter_WriteStr(PyUnicodeWriter*, object) except -1
|
|
82
|
+
object PyUnicodeWriter_Finish(PyUnicodeWriter*)
|
|
83
|
+
|
|
84
|
+
ctypedef struct PyBytesWriter:
|
|
85
|
+
pass
|
|
86
|
+
PyBytesWriter* PyBytesWriter_Create(Py_ssize_t) nogil
|
|
87
|
+
void PyBytesWriter_Discard(PyBytesWriter*) nogil
|
|
88
|
+
void* PyBytesWriter_GetData(PyBytesWriter*) nogil
|
|
89
|
+
object PyBytesWriter_FinishWithPointer(PyBytesWriter*, void*) nogil
|
|
90
|
+
void* PyBytesWriter_WriteBytes(PyBytesWriter*, void*, void*, Py_ssize_t) nogil
|
|
91
|
+
|
|
92
|
+
cdef extern from "./pythoncapi_compat_shim.h":
|
|
93
|
+
void* PyBytesWriter_Prepare(PyBytesWriter*, void*, Py_ssize_t) nogil
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
ctypedef bint (*jis_put_func)(JNTAJISIncrementalEncoderContext*, uint16_t)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
ctypedef struct JNTAJISIncrementalEncoder:
|
|
100
|
+
PyObject* encoding
|
|
101
|
+
uint16_t replacement
|
|
102
|
+
jis_put_func put_jis
|
|
103
|
+
size_t lal
|
|
104
|
+
uint32_t[32] la
|
|
105
|
+
int shift_state
|
|
106
|
+
int state
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
ctypedef enum JNTAJISError:
|
|
110
|
+
JNTAJISError_Success = 0
|
|
111
|
+
JNTAJISError_MemoryError = 1
|
|
112
|
+
JNTAJISError_AssertionError = 2
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
ctypedef struct JNTAJISIncrementalEncoderContext:
|
|
116
|
+
JNTAJISIncrementalEncoder* e
|
|
117
|
+
PyBytesWriter* writer
|
|
118
|
+
PyObject* u # borrow
|
|
119
|
+
PyUnicode_Kind uk
|
|
120
|
+
void* ud
|
|
121
|
+
Py_ssize_t ul
|
|
122
|
+
Py_ssize_t pos
|
|
123
|
+
char* p
|
|
124
|
+
JNTAJISError err
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
cdef bint lookup_rev_table(uint16_t* pj, uint32_t u) nogil:
|
|
128
|
+
cdef size_t l = sizeof(urange_to_jis_mappings) // sizeof(urange_to_jis_mappings[0])
|
|
129
|
+
cdef size_t s = 0, e = l
|
|
130
|
+
cdef size_t m
|
|
131
|
+
cdef const URangeToJISMapping* mm
|
|
132
|
+
cdef uint16_t jis
|
|
133
|
+
while s < e and e <= l:
|
|
134
|
+
m = (s + e) // 2
|
|
135
|
+
mm = &urange_to_jis_mappings[m]
|
|
136
|
+
if u < mm.start:
|
|
137
|
+
e = m
|
|
138
|
+
continue
|
|
139
|
+
elif u > mm.end:
|
|
140
|
+
s = m + 1
|
|
141
|
+
continue
|
|
142
|
+
if u > mm.end:
|
|
143
|
+
break
|
|
144
|
+
jis = mm.jis[u - mm.start]
|
|
145
|
+
if jis == <uint16_t>-1:
|
|
146
|
+
break
|
|
147
|
+
pj[0] = jis
|
|
148
|
+
return True
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
cdef bint jis_put_men_1(JNTAJISIncrementalEncoderContext* ctx, uint16_t c) nogil:
|
|
153
|
+
cdef unsigned int men = c // (94 * 94)
|
|
154
|
+
cdef unsigned int ku = c // 94 % 94
|
|
155
|
+
cdef unsigned int ten = c % 94
|
|
156
|
+
cdef char* p
|
|
157
|
+
if men != 0:
|
|
158
|
+
return False
|
|
159
|
+
|
|
160
|
+
p = <char *>PyBytesWriter_Prepare(ctx.writer, ctx.p, 2)
|
|
161
|
+
if not p:
|
|
162
|
+
ctx.err = JNTAJISError_MemoryError
|
|
163
|
+
return False
|
|
164
|
+
p[0] = 0x21 + ku
|
|
165
|
+
p[1] = 0x21 + ten
|
|
166
|
+
p += 2
|
|
167
|
+
ctx.p = p
|
|
168
|
+
return True
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
cdef bint jis_put_jisx0208(JNTAJISIncrementalEncoderContext* ctx, uint16_t c) nogil:
|
|
172
|
+
if c >= sizeof(tx_mappings) // sizeof(tx_mappings[0]):
|
|
173
|
+
return False
|
|
174
|
+
cdef JISCharacterClass class_ = tx_mappings[c].class_
|
|
175
|
+
cdef char* p
|
|
176
|
+
if (
|
|
177
|
+
class_ == JISCharacterClass_KANJI_LEVEL_1 or
|
|
178
|
+
class_ == JISCharacterClass_KANJI_LEVEL_2 or
|
|
179
|
+
class_ == JISCharacterClass_JISX0208_NON_KANJI
|
|
180
|
+
):
|
|
181
|
+
p = <char *>PyBytesWriter_Prepare(ctx.writer, ctx.p, 2)
|
|
182
|
+
if not p:
|
|
183
|
+
ctx.err = JNTAJISError_MemoryError
|
|
184
|
+
return False
|
|
185
|
+
p[0] = 0x21 + c // 94 % 94
|
|
186
|
+
p[1] = 0x21 + c % 94
|
|
187
|
+
p += 2
|
|
188
|
+
ctx.p = p
|
|
189
|
+
return True
|
|
190
|
+
else:
|
|
191
|
+
return False
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
cdef bint jis_put_jisx0208_translit(JNTAJISIncrementalEncoderContext* ctx, uint16_t c) nogil:
|
|
195
|
+
if c >= sizeof(tx_mappings) // sizeof(tx_mappings[0]):
|
|
196
|
+
return False
|
|
197
|
+
cdef const ShrinkingTransliterationMapping* m = &tx_mappings[c]
|
|
198
|
+
cdef JISCharacterClass class_ = m.class_
|
|
199
|
+
cdef char* p
|
|
200
|
+
if (
|
|
201
|
+
class_ == JISCharacterClass_KANJI_LEVEL_1 or
|
|
202
|
+
class_ == JISCharacterClass_KANJI_LEVEL_2 or
|
|
203
|
+
class_ == JISCharacterClass_JISX0208_NON_KANJI
|
|
204
|
+
):
|
|
205
|
+
p = <char *>PyBytesWriter_Prepare(ctx.writer, ctx.p, 2)
|
|
206
|
+
if not p:
|
|
207
|
+
ctx.err = JNTAJISError_MemoryError
|
|
208
|
+
return False
|
|
209
|
+
p[0] = 0x21 + c // 94 % 94
|
|
210
|
+
p[1] = 0x21 + c % 94
|
|
211
|
+
p += 2
|
|
212
|
+
ctx.p = p
|
|
213
|
+
return True
|
|
214
|
+
else:
|
|
215
|
+
if m.tx_len > 0:
|
|
216
|
+
p = <char *>PyBytesWriter_Prepare(ctx.writer, ctx.p, 2 * m.tx_len)
|
|
217
|
+
if not p:
|
|
218
|
+
ctx.err = JNTAJISError_MemoryError
|
|
219
|
+
return False
|
|
220
|
+
for i in range(m.tx_len):
|
|
221
|
+
c = m.tx_jis[i]
|
|
222
|
+
p[0] = 0x21 + c // 94 % 94
|
|
223
|
+
p[1] = 0x21 + c % 94
|
|
224
|
+
p += 2
|
|
225
|
+
ctx.p = p
|
|
226
|
+
return True
|
|
227
|
+
else:
|
|
228
|
+
return False
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
cdef object JNTAJISIncrementalEncoderContext_raise(JNTAJISIncrementalEncoderContext* ctx):
|
|
232
|
+
if ctx.err == JNTAJISError_MemoryError:
|
|
233
|
+
raise MemoryError()
|
|
234
|
+
elif ctx.err == JNTAJISError_AssertionError:
|
|
235
|
+
raise AssertionError()
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
cdef object JNTAJISIncrementalEncoderContext_createUnicodeEncodeError(
|
|
239
|
+
JNTAJISIncrementalEncoderContext* ctx,
|
|
240
|
+
char *reason
|
|
241
|
+
):
|
|
242
|
+
return UnicodeEncodeError(
|
|
243
|
+
<object>ctx.e.encoding,
|
|
244
|
+
<object>ctx.u,
|
|
245
|
+
ctx.pos,
|
|
246
|
+
ctx.pos + 1,
|
|
247
|
+
(<bytes>reason).decode("ascii"),
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
cdef object JNTAJISIncrementalEncoderContext_put_replacement(JNTAJISIncrementalEncoderContext* ctx):
|
|
252
|
+
cdef uint16_t jis = ctx.e.replacement
|
|
253
|
+
|
|
254
|
+
if jis == <uint16_t>-1:
|
|
255
|
+
raise JNTAJISIncrementalEncoderContext_createUnicodeEncodeError(
|
|
256
|
+
ctx, "not convertible to JISX0208",
|
|
257
|
+
)
|
|
258
|
+
else:
|
|
259
|
+
if not jis_put_men_1(ctx, jis):
|
|
260
|
+
raise JNTAJISIncrementalEncoderContext_createUnicodeEncodeError(
|
|
261
|
+
ctx, "replacement character is neither convertible to JISX0208",
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
cdef bint JNTAJISIncrementalEncoderContext_put_shift(
|
|
266
|
+
JNTAJISIncrementalEncoderContext* ctx,
|
|
267
|
+
int next_shift_state
|
|
268
|
+
) nogil:
|
|
269
|
+
if next_shift_state != ctx.e.shift_state:
|
|
270
|
+
ctx.e.shift_state = next_shift_state
|
|
271
|
+
ctx.p = <char *>PyBytesWriter_Prepare(ctx.writer, ctx.p, 1)
|
|
272
|
+
if not ctx.p:
|
|
273
|
+
ctx.err = JNTAJISError_MemoryError
|
|
274
|
+
return False
|
|
275
|
+
if next_shift_state == 0:
|
|
276
|
+
ctx.p[0] = 0x0e
|
|
277
|
+
ctx.p += 1
|
|
278
|
+
elif next_shift_state == 1:
|
|
279
|
+
ctx.p[0] = 0x0f
|
|
280
|
+
ctx.p += 1
|
|
281
|
+
else:
|
|
282
|
+
ctx.err = JNTAJISError_AssertionError
|
|
283
|
+
return False
|
|
284
|
+
return True
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
cdef bint jis_put_siso(
|
|
288
|
+
JNTAJISIncrementalEncoderContext* ctx,
|
|
289
|
+
uint16_t jis
|
|
290
|
+
) nogil:
|
|
291
|
+
if not JNTAJISIncrementalEncoderContext_put_shift(ctx, jis // (94 * 94)):
|
|
292
|
+
return False
|
|
293
|
+
ctx.p = <char *>PyBytesWriter_Prepare(ctx.writer, ctx.p, 2)
|
|
294
|
+
if not ctx.p:
|
|
295
|
+
ctx.err = JNTAJISError_MemoryError
|
|
296
|
+
return False
|
|
297
|
+
ctx.p[0] = 0x21 + jis // 94 % 94
|
|
298
|
+
ctx.p[1] = 0x21 + jis % 94
|
|
299
|
+
ctx.p += 2
|
|
300
|
+
return True
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
cdef object JNTAJISIncrementalEncoderContext_flush_lookahead(
|
|
304
|
+
JNTAJISIncrementalEncoderContext* ctx
|
|
305
|
+
):
|
|
306
|
+
cdef jis_put_func put = ctx.e.put_jis
|
|
307
|
+
cdef size_t i
|
|
308
|
+
cdef uint32_t u
|
|
309
|
+
cdef uint16_t jis
|
|
310
|
+
cdef bint ok
|
|
311
|
+
|
|
312
|
+
for i in range(ctx.e.lal):
|
|
313
|
+
u = ctx.e.la[i]
|
|
314
|
+
ok = lookup_rev_table(&jis, u)
|
|
315
|
+
if ok:
|
|
316
|
+
ok = put(ctx, jis)
|
|
317
|
+
if not ok:
|
|
318
|
+
if ctx.err == JNTAJISError_Success:
|
|
319
|
+
JNTAJISIncrementalEncoderContext_put_replacement(ctx)
|
|
320
|
+
else:
|
|
321
|
+
JNTAJISIncrementalEncoderContext_raise(ctx)
|
|
322
|
+
|
|
323
|
+
JNTAJISIncrementalEncoder_reset(ctx.e)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
cdef object JNTAJISIncrementalEncoderContext_encode(
|
|
327
|
+
JNTAJISIncrementalEncoderContext* ctx
|
|
328
|
+
):
|
|
329
|
+
cdef jis_put_func put = ctx.e.put_jis
|
|
330
|
+
cdef uint32_t u
|
|
331
|
+
cdef uint16_t jis
|
|
332
|
+
cdef JNTAJISIncrementalEncoder* e = ctx.e
|
|
333
|
+
|
|
334
|
+
for ctx.pos in range(ctx.ul):
|
|
335
|
+
u = PyUnicode_READ(ctx.uk, ctx.ud, ctx.pos)
|
|
336
|
+
jis = sm_uni_to_jis_mapping(&e.state, u)
|
|
337
|
+
if e.state == -1:
|
|
338
|
+
if not put(ctx, jis):
|
|
339
|
+
if ctx.err == JNTAJISError_Success:
|
|
340
|
+
JNTAJISIncrementalEncoderContext_put_replacement(ctx)
|
|
341
|
+
else:
|
|
342
|
+
JNTAJISIncrementalEncoderContext_raise(ctx)
|
|
343
|
+
e.lal = 0
|
|
344
|
+
e.state = 0
|
|
345
|
+
else:
|
|
346
|
+
if e.lal >= sizeof(e.la) // sizeof(e.la[0]):
|
|
347
|
+
raise JNTAJISIncrementalEncoderContext_createUnicodeEncodeError(
|
|
348
|
+
ctx, "lookahead buffer overflow"
|
|
349
|
+
)
|
|
350
|
+
e.la[e.lal] = u
|
|
351
|
+
e.lal += 1
|
|
352
|
+
if e.state == 0:
|
|
353
|
+
JNTAJISIncrementalEncoderContext_flush_lookahead(ctx)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
cdef void JNTAJISIncrementalEncoder_reset(JNTAJISIncrementalEncoder* e) noexcept:
|
|
357
|
+
e.state = 0
|
|
358
|
+
e.lal = 0
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
cdef object JNTAJISIncrementalEncoder_encode(
|
|
362
|
+
JNTAJISIncrementalEncoder* e,
|
|
363
|
+
unicode u,
|
|
364
|
+
bint flush,
|
|
365
|
+
):
|
|
366
|
+
cdef JNTAJISIncrementalEncoderContext ctx
|
|
367
|
+
ctx.e = e
|
|
368
|
+
ctx.u = <PyObject*>u # borrow
|
|
369
|
+
ctx.uk = PyUnicode_KIND(u)
|
|
370
|
+
ctx.ud = PyUnicode_DATA(u)
|
|
371
|
+
ctx.ul = PyUnicode_GET_LENGTH(u)
|
|
372
|
+
ctx.pos = 0
|
|
373
|
+
ctx.err = JNTAJISError_Success
|
|
374
|
+
ctx.writer = PyBytesWriter_Create(ctx.ul * 2)
|
|
375
|
+
if not ctx.writer:
|
|
376
|
+
raise MemoryError()
|
|
377
|
+
ctx.p = <char *>PyBytesWriter_GetData(ctx.writer)
|
|
378
|
+
try:
|
|
379
|
+
JNTAJISIncrementalEncoderContext_encode(&ctx)
|
|
380
|
+
if flush:
|
|
381
|
+
JNTAJISIncrementalEncoderContext_flush_lookahead(&ctx)
|
|
382
|
+
JNTAJISIncrementalEncoderContext_put_shift(&ctx, 0)
|
|
383
|
+
return PyBytesWriter_FinishWithPointer(ctx.writer, ctx.p)
|
|
384
|
+
except:
|
|
385
|
+
PyBytesWriter_Discard(ctx.writer)
|
|
386
|
+
raise
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
ctypedef enum ConversionMode:
|
|
390
|
+
ConversionMode_SISO = 0
|
|
391
|
+
ConversionMode_MEN1 = 1
|
|
392
|
+
ConversionMode_JISX0208 = 2
|
|
393
|
+
ConversionMode_JISX0208_TRANSLIT = 3
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
cdef jis_put_func jis_put_func_for_conversion_mode(int conv_mode):
|
|
397
|
+
if conv_mode == ConversionMode_SISO:
|
|
398
|
+
return jis_put_siso
|
|
399
|
+
elif conv_mode == ConversionMode_MEN1:
|
|
400
|
+
return jis_put_men_1
|
|
401
|
+
elif conv_mode == ConversionMode_JISX0208:
|
|
402
|
+
return jis_put_jisx0208
|
|
403
|
+
elif conv_mode == ConversionMode_JISX0208_TRANSLIT:
|
|
404
|
+
return jis_put_jisx0208_translit
|
|
405
|
+
else:
|
|
406
|
+
return NULL
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
cdef void JNTAJISIncrementalEncoder_fini(JNTAJISIncrementalEncoder* e):
|
|
410
|
+
if e.encoding:
|
|
411
|
+
Py_DECREF(<object>e.encoding)
|
|
412
|
+
e.encoding = NULL
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
cdef object JNTAJISIncrementalEncoder_init(JNTAJISIncrementalEncoder* e, unicode encoding, int conv_mode):
|
|
416
|
+
if len(encoding) == 0:
|
|
417
|
+
raise ValueError("encoding cannot be empty")
|
|
418
|
+
Py_INCREF(encoding)
|
|
419
|
+
e.encoding = <PyObject*>encoding
|
|
420
|
+
e.replacement = <uint16_t>-1
|
|
421
|
+
e.put_jis = jis_put_func_for_conversion_mode(<int>conv_mode)
|
|
422
|
+
if not e.put_jis:
|
|
423
|
+
raise ValueError(f"unknown conversion mode: {conv_mode}")
|
|
424
|
+
e.lal = 0
|
|
425
|
+
e.shift_state = 0
|
|
426
|
+
e.state = 0
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
cdef class IncrementalEncoder:
|
|
430
|
+
"""
|
|
431
|
+
An IncrementalEncoder implementation.
|
|
432
|
+
|
|
433
|
+
For the description of each method, please see the Python's
|
|
434
|
+
codec documentation: https://docs.python.org/3/library/codecs.html#codecs.IncrementalEncoder
|
|
435
|
+
"""
|
|
436
|
+
cdef JNTAJISIncrementalEncoder _impl
|
|
437
|
+
|
|
438
|
+
# _impl is mutated in place (e.g. the lookahead buffer), so every access
|
|
439
|
+
# is serialized per instance; this is required on free-threaded builds.
|
|
440
|
+
|
|
441
|
+
def encode(self, in_, final):
|
|
442
|
+
with cython.critical_section(self):
|
|
443
|
+
return JNTAJISIncrementalEncoder_encode(&self._impl, in_, final)
|
|
444
|
+
|
|
445
|
+
def reset(self):
|
|
446
|
+
with cython.critical_section(self):
|
|
447
|
+
JNTAJISIncrementalEncoder_reset(&self._impl)
|
|
448
|
+
|
|
449
|
+
def getstate(self):
|
|
450
|
+
with cython.critical_section(self):
|
|
451
|
+
return self._impl.state * 2 + self._impl.shift_state
|
|
452
|
+
|
|
453
|
+
def setstate(self, state):
|
|
454
|
+
with cython.critical_section(self):
|
|
455
|
+
self._impl.shift_state = state % 2
|
|
456
|
+
self._impl.state = state // 2
|
|
457
|
+
|
|
458
|
+
def __dealloc__(self):
|
|
459
|
+
JNTAJISIncrementalEncoder_fini(&self._impl)
|
|
460
|
+
|
|
461
|
+
def __init__(self, unicode encoding, int conv_mode):
|
|
462
|
+
with cython.critical_section(self):
|
|
463
|
+
JNTAJISIncrementalEncoder_fini(&self._impl)
|
|
464
|
+
JNTAJISIncrementalEncoder_init(&self._impl, encoding, conv_mode)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def jnta_encode(unicode encoding, unicode in_, int conv_mode):
|
|
468
|
+
"""
|
|
469
|
+
Encode a given Unicode string into JIS X 0208:1997 / JIS X 0213:2012.
|
|
470
|
+
"""
|
|
471
|
+
|
|
472
|
+
cdef JNTAJISIncrementalEncoder e
|
|
473
|
+
JNTAJISIncrementalEncoder_init(&e, encoding, conv_mode)
|
|
474
|
+
try:
|
|
475
|
+
return JNTAJISIncrementalEncoder_encode(&e, in_, True)
|
|
476
|
+
finally:
|
|
477
|
+
JNTAJISIncrementalEncoder_fini(&e)
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
ctypedef struct JNTAJISDecoder:
|
|
481
|
+
PyObject* encoding
|
|
482
|
+
PyObject* replacement # NULL to raise on reserved codes
|
|
483
|
+
int conv_mode
|
|
484
|
+
int shift_offset
|
|
485
|
+
int upper
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
cdef object JNTAJISDecoder_createUnicodeDecodeError(
|
|
489
|
+
JNTAJISDecoder *d,
|
|
490
|
+
object underlying,
|
|
491
|
+
Py_ssize_t start,
|
|
492
|
+
Py_ssize_t end,
|
|
493
|
+
char *reason
|
|
494
|
+
):
|
|
495
|
+
return UnicodeDecodeError(
|
|
496
|
+
<object>d.encoding,
|
|
497
|
+
underlying,
|
|
498
|
+
start,
|
|
499
|
+
end,
|
|
500
|
+
(<bytes>reason).decode("ascii"),
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
cdef object JNTAJISDecoder_decode(
|
|
505
|
+
JNTAJISDecoder *d,
|
|
506
|
+
object underlying,
|
|
507
|
+
void* in_bytes,
|
|
508
|
+
Py_ssize_t in_sz
|
|
509
|
+
):
|
|
510
|
+
cdef unsigned char* in_ = <unsigned char *>in_bytes
|
|
511
|
+
cdef unsigned char* p = in_
|
|
512
|
+
cdef unsigned char* e = in_ + in_sz
|
|
513
|
+
cdef unsigned int c0, c1
|
|
514
|
+
cdef uint16_t jis
|
|
515
|
+
cdef const ShrinkingTransliterationMapping* m
|
|
516
|
+
cdef PyUnicodeWriter* writer
|
|
517
|
+
cdef char[256] reason
|
|
518
|
+
cdef size_t i
|
|
519
|
+
|
|
520
|
+
writer = PyUnicodeWriter_Create(in_sz // 2)
|
|
521
|
+
try:
|
|
522
|
+
while p < e:
|
|
523
|
+
if d.upper > 0:
|
|
524
|
+
c0 = d.upper
|
|
525
|
+
d.upper = 0
|
|
526
|
+
else:
|
|
527
|
+
c0 = p[0]
|
|
528
|
+
p += 1
|
|
529
|
+
|
|
530
|
+
if c0 >= 0x21 and c0 <= 0x7e:
|
|
531
|
+
if p >= e:
|
|
532
|
+
d.upper = c0
|
|
533
|
+
break
|
|
534
|
+
c1 = p[0]
|
|
535
|
+
p += 1
|
|
536
|
+
if c1 >= 0x21 and c1 <= 0x7e:
|
|
537
|
+
jis = d.shift_offset + (c0 - 0x21) *94 + (c1 - 0x21)
|
|
538
|
+
m = &tx_mappings[jis]
|
|
539
|
+
if m.class_ == JISCharacterClass_RESERVED and d.replacement:
|
|
540
|
+
PyUnicodeWriter_WriteStr(writer, <object>d.replacement)
|
|
541
|
+
elif m.class_ == JISCharacterClass_RESERVED:
|
|
542
|
+
snprintf(
|
|
543
|
+
reason, sizeof(reason),
|
|
544
|
+
"JIS character %d-%d-%d does not have a corresponding unicode codepoint",
|
|
545
|
+
<int>(d.shift_offset // 94 // 94 + 1),
|
|
546
|
+
c0 - 0x20,
|
|
547
|
+
c1 - 0x20,
|
|
548
|
+
)
|
|
549
|
+
raise JNTAJISDecoder_createUnicodeDecodeError(
|
|
550
|
+
d,
|
|
551
|
+
underlying,
|
|
552
|
+
p - in_ - 2,
|
|
553
|
+
p - in_,
|
|
554
|
+
reason,
|
|
555
|
+
)
|
|
556
|
+
elif (
|
|
557
|
+
d.conv_mode == ConversionMode_JISX0208
|
|
558
|
+
and m.class_ != JISCharacterClass_KANJI_LEVEL_1
|
|
559
|
+
and m.class_ != JISCharacterClass_KANJI_LEVEL_2
|
|
560
|
+
and m.class_ != JISCharacterClass_JISX0208_NON_KANJI
|
|
561
|
+
):
|
|
562
|
+
snprintf(
|
|
563
|
+
reason, sizeof(reason),
|
|
564
|
+
"JIS character %d-%d-%d is not part of JIS X 0208",
|
|
565
|
+
<int>(d.shift_offset // 94 // 94 + 1),
|
|
566
|
+
c0 - 0x20,
|
|
567
|
+
c1 - 0x20,
|
|
568
|
+
)
|
|
569
|
+
raise JNTAJISDecoder_createUnicodeDecodeError(
|
|
570
|
+
d,
|
|
571
|
+
underlying,
|
|
572
|
+
p - in_ - 2,
|
|
573
|
+
p - in_,
|
|
574
|
+
reason,
|
|
575
|
+
)
|
|
576
|
+
elif (
|
|
577
|
+
d.conv_mode == ConversionMode_JISX0208_TRANSLIT
|
|
578
|
+
and (
|
|
579
|
+
m.class_ == JISCharacterClass_JISX0213_NON_KANJI
|
|
580
|
+
or m.class_ == JISCharacterClass_KANJI_LEVEL_3
|
|
581
|
+
or m.class_ == JISCharacterClass_KANJI_LEVEL_4
|
|
582
|
+
)
|
|
583
|
+
and m.tx_len > 0
|
|
584
|
+
):
|
|
585
|
+
for i in range(m.tx_len):
|
|
586
|
+
PyUnicodeWriter_WriteChar(writer, <Py_UCS4>m.tx_us[i])
|
|
587
|
+
else:
|
|
588
|
+
PyUnicodeWriter_WriteChar(writer, <Py_UCS4>m.us[0])
|
|
589
|
+
if m.us[1] != <uint32_t>-1:
|
|
590
|
+
PyUnicodeWriter_WriteChar(writer, <Py_UCS4>m.us[1])
|
|
591
|
+
else:
|
|
592
|
+
# Only the first byte is malformed; c1 may start a valid sequence.
|
|
593
|
+
snprintf(
|
|
594
|
+
reason, sizeof(reason),
|
|
595
|
+
"unexpected byte \\x%02x after \\x%02x",
|
|
596
|
+
c1, c0,
|
|
597
|
+
)
|
|
598
|
+
raise JNTAJISDecoder_createUnicodeDecodeError(
|
|
599
|
+
d,
|
|
600
|
+
underlying,
|
|
601
|
+
p - in_ - 2,
|
|
602
|
+
p - in_ - 1,
|
|
603
|
+
reason,
|
|
604
|
+
)
|
|
605
|
+
else:
|
|
606
|
+
# Plane selection bytes are only valid in ConversionMode.SISO.
|
|
607
|
+
if c0 == 0x0e and d.conv_mode == ConversionMode_SISO:
|
|
608
|
+
d.shift_offset = 0
|
|
609
|
+
elif c0 == 0x0f and d.conv_mode == ConversionMode_SISO:
|
|
610
|
+
d.shift_offset = 94 * 94
|
|
611
|
+
else:
|
|
612
|
+
snprintf(
|
|
613
|
+
reason, sizeof(reason),
|
|
614
|
+
"unexpected byte \\x%02x",
|
|
615
|
+
c0,
|
|
616
|
+
)
|
|
617
|
+
raise JNTAJISDecoder_createUnicodeDecodeError(
|
|
618
|
+
d,
|
|
619
|
+
underlying,
|
|
620
|
+
p - in_ - 1,
|
|
621
|
+
p - in_,
|
|
622
|
+
reason,
|
|
623
|
+
)
|
|
624
|
+
except:
|
|
625
|
+
PyUnicodeWriter_Discard(writer)
|
|
626
|
+
raise
|
|
627
|
+
return PyUnicodeWriter_Finish(writer)
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
cdef void JNTAJISDecoder_fini(JNTAJISDecoder *d):
|
|
631
|
+
Py_DECREF(<object>d.encoding)
|
|
632
|
+
if d.replacement:
|
|
633
|
+
Py_DECREF(<object>d.replacement)
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
cdef object JNTAJISDecoder_init(
|
|
637
|
+
JNTAJISDecoder *d,
|
|
638
|
+
unicode encoding,
|
|
639
|
+
int conv_mode,
|
|
640
|
+
unicode replacement,
|
|
641
|
+
):
|
|
642
|
+
if len(encoding) == 0:
|
|
643
|
+
raise ValueError("encoding cannot be empty")
|
|
644
|
+
if jis_put_func_for_conversion_mode(conv_mode) == NULL:
|
|
645
|
+
raise ValueError(f"unknown conversion mode: {conv_mode}")
|
|
646
|
+
Py_INCREF(encoding)
|
|
647
|
+
d.encoding = <PyObject*>encoding
|
|
648
|
+
if replacement is None:
|
|
649
|
+
d.replacement = NULL
|
|
650
|
+
else:
|
|
651
|
+
Py_INCREF(replacement)
|
|
652
|
+
d.replacement = <PyObject*>replacement
|
|
653
|
+
d.conv_mode = conv_mode
|
|
654
|
+
d.shift_offset = 0
|
|
655
|
+
d.upper = 0
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def jnta_decode(
|
|
659
|
+
unicode encoding,
|
|
660
|
+
bytes in_,
|
|
661
|
+
int conv_mode=ConversionMode_SISO,
|
|
662
|
+
unicode replacement=None,
|
|
663
|
+
):
|
|
664
|
+
"""
|
|
665
|
+
Decode a given JIS character sequence into a Unicode string.
|
|
666
|
+
"""
|
|
667
|
+
|
|
668
|
+
cdef JNTAJISDecoder d
|
|
669
|
+
JNTAJISDecoder_init(&d, encoding, conv_mode, replacement)
|
|
670
|
+
try:
|
|
671
|
+
retval = JNTAJISDecoder_decode(&d, in_, <char *>in_, len(in_))
|
|
672
|
+
if d.upper > 0:
|
|
673
|
+
raise JNTAJISDecoder_createUnicodeDecodeError(
|
|
674
|
+
&d, in_, len(in_) - 1, len(in_),
|
|
675
|
+
"incomplete multibyte character",
|
|
676
|
+
)
|
|
677
|
+
return retval
|
|
678
|
+
finally:
|
|
679
|
+
JNTAJISDecoder_fini(&d)
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
class TransliterationError(Exception):
|
|
683
|
+
pass
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
ctypedef struct JNTAJISShrinkingTransliteratorContext:
|
|
687
|
+
PyUnicodeWriter* writer
|
|
688
|
+
PyObject *replacement
|
|
689
|
+
bint passthrough
|
|
690
|
+
PyObject *in_
|
|
691
|
+
int uk
|
|
692
|
+
void* ud
|
|
693
|
+
Py_ssize_t ul
|
|
694
|
+
Py_ssize_t pos
|
|
695
|
+
int state
|
|
696
|
+
uint32_t[32] la
|
|
697
|
+
size_t lal
|
|
698
|
+
bint finished
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
cdef object JNTAJISShrinkingTransliteratorContext_put_replacement(
|
|
702
|
+
JNTAJISShrinkingTransliteratorContext* t,
|
|
703
|
+
Py_UCS4 u,
|
|
704
|
+
):
|
|
705
|
+
if t.passthrough:
|
|
706
|
+
PyUnicodeWriter_WriteChar(t.writer, u)
|
|
707
|
+
else:
|
|
708
|
+
if len(<object>t.replacement) == 0:
|
|
709
|
+
raise TransliterationError(f"transliteration failed at position {t.pos}")
|
|
710
|
+
PyUnicodeWriter_WriteStr(t.writer, <object>t.replacement)
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
cdef object JNTAJISIncrementalEncoderContext_put(
|
|
714
|
+
JNTAJISShrinkingTransliteratorContext* t,
|
|
715
|
+
uint16_t jis
|
|
716
|
+
):
|
|
717
|
+
cdef const ShrinkingTransliterationMapping* m = &tx_mappings[jis]
|
|
718
|
+
cdef size_t i
|
|
719
|
+
|
|
720
|
+
if m.class_ == JISCharacterClass_RESERVED:
|
|
721
|
+
return False
|
|
722
|
+
else:
|
|
723
|
+
if (
|
|
724
|
+
(
|
|
725
|
+
m.class_ == JISCharacterClass_JISX0213_NON_KANJI or
|
|
726
|
+
m.class_ == JISCharacterClass_KANJI_LEVEL_3 or
|
|
727
|
+
m.class_ == JISCharacterClass_KANJI_LEVEL_4
|
|
728
|
+
)
|
|
729
|
+
and m.tx_len > 0
|
|
730
|
+
):
|
|
731
|
+
for i in range(m.tx_len):
|
|
732
|
+
PyUnicodeWriter_WriteChar(t.writer, <Py_UCS4>m.tx_us[i])
|
|
733
|
+
else:
|
|
734
|
+
PyUnicodeWriter_WriteChar(t.writer, <Py_UCS4>m.us[0])
|
|
735
|
+
if m.us[1] != <uint32_t>-1:
|
|
736
|
+
PyUnicodeWriter_WriteChar(t.writer, <Py_UCS4>m.us[1])
|
|
737
|
+
return True
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
cdef object JNTAJISShrinkingTransliteratorContext_do(
|
|
741
|
+
JNTAJISShrinkingTransliteratorContext* t,
|
|
742
|
+
):
|
|
743
|
+
cdef Py_UCS4 u
|
|
744
|
+
cdef uint16_t jis
|
|
745
|
+
cdef size_t i
|
|
746
|
+
cdef const ShrinkingTransliterationMapping* m
|
|
747
|
+
|
|
748
|
+
for t.pos in range(t.ul):
|
|
749
|
+
u = PyUnicode_READ(t.uk, t.ud, t.pos)
|
|
750
|
+
jis = sm_uni_to_jis_mapping(&t.state, u)
|
|
751
|
+
if t.state == -1:
|
|
752
|
+
if not JNTAJISIncrementalEncoderContext_put(t, jis):
|
|
753
|
+
JNTAJISShrinkingTransliteratorContext_put_replacement(t, u)
|
|
754
|
+
t.state = 0
|
|
755
|
+
else:
|
|
756
|
+
t.la[t.lal] = u
|
|
757
|
+
t.lal += 1
|
|
758
|
+
if t.state == 0:
|
|
759
|
+
for i in range(t.lal):
|
|
760
|
+
if not lookup_rev_table(&jis, t.la[i]):
|
|
761
|
+
JNTAJISShrinkingTransliteratorContext_put_replacement(t, u)
|
|
762
|
+
continue
|
|
763
|
+
else:
|
|
764
|
+
if not JNTAJISIncrementalEncoderContext_put(t, jis):
|
|
765
|
+
JNTAJISShrinkingTransliteratorContext_put_replacement(t, u)
|
|
766
|
+
t.lal = 0
|
|
767
|
+
|
|
768
|
+
return True
|
|
769
|
+
|
|
770
|
+
cdef unicode JNTAJISShrinkingTransliteratorContext_get_result(
|
|
771
|
+
JNTAJISShrinkingTransliteratorContext* t,
|
|
772
|
+
):
|
|
773
|
+
t.finished = 1
|
|
774
|
+
return PyUnicodeWriter_Finish(t.writer)
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
cdef void JNTAJISShrinkingTransliteratorContext_fini(
|
|
778
|
+
JNTAJISShrinkingTransliteratorContext* t,
|
|
779
|
+
):
|
|
780
|
+
if not t.finished:
|
|
781
|
+
PyUnicodeWriter_Discard(t.writer)
|
|
782
|
+
Py_DECREF(<object>t.in_)
|
|
783
|
+
Py_DECREF(<object>t.replacement)
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
cdef JNTAJISShrinkingTransliteratorContext_init(
|
|
787
|
+
JNTAJISShrinkingTransliteratorContext* t,
|
|
788
|
+
unicode in_,
|
|
789
|
+
unicode replacement,
|
|
790
|
+
bint passthrough,
|
|
791
|
+
):
|
|
792
|
+
# Create the writer first so that a failure leaves nothing to release.
|
|
793
|
+
t.writer = PyUnicodeWriter_Create(len(in_))
|
|
794
|
+
Py_INCREF(in_)
|
|
795
|
+
t.in_ = <PyObject*>in_
|
|
796
|
+
Py_INCREF(replacement)
|
|
797
|
+
t.replacement = <PyObject*>replacement
|
|
798
|
+
t.uk = PyUnicode_KIND(in_)
|
|
799
|
+
t.ud = PyUnicode_DATA(in_)
|
|
800
|
+
t.ul = PyUnicode_GET_LENGTH(in_)
|
|
801
|
+
t.state = 0
|
|
802
|
+
t.lal = 0
|
|
803
|
+
t.finished = 0
|
|
804
|
+
t.passthrough = passthrough
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
def jnta_shrink_translit(unicode in_, unicode replacement=u"\ufffd", bint passthrough=False):
|
|
808
|
+
"""
|
|
809
|
+
Transliterate a Unicode string according to the NTA shrink mappings.
|
|
810
|
+
"""
|
|
811
|
+
|
|
812
|
+
cdef JNTAJISShrinkingTransliteratorContext ctx
|
|
813
|
+
|
|
814
|
+
JNTAJISShrinkingTransliteratorContext_init(&ctx, in_, replacement, passthrough)
|
|
815
|
+
try:
|
|
816
|
+
JNTAJISShrinkingTransliteratorContext_do(&ctx)
|
|
817
|
+
return JNTAJISShrinkingTransliteratorContext_get_result(&ctx)
|
|
818
|
+
finally:
|
|
819
|
+
JNTAJISShrinkingTransliteratorContext_fini(&ctx)
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
cdef bint MJShrinkMappingUnicodeSet_valid(const MJShrinkMappingUnicodeSet *sm) nogil:
|
|
823
|
+
cdef size_t i
|
|
824
|
+
for i in range(sizeof(sm._0) // sizeof(sm._0[0])):
|
|
825
|
+
if sm._0[i] != <uint32_t>-1:
|
|
826
|
+
return 1
|
|
827
|
+
for i in range(sizeof(sm._1) // sizeof(sm._1[0])):
|
|
828
|
+
if sm._1[i] != <uint32_t>-1:
|
|
829
|
+
return 1
|
|
830
|
+
for i in range(sizeof(sm._2) // sizeof(sm._2[0])):
|
|
831
|
+
if sm._2[i] != <uint32_t>-1:
|
|
832
|
+
return 1
|
|
833
|
+
for i in range(sizeof(sm._3) // sizeof(sm._3[0])):
|
|
834
|
+
if sm._3[i] != <uint32_t>-1:
|
|
835
|
+
return 1
|
|
836
|
+
return 0
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
cdef bint lookup_mj_mapping_table(const MJMappingSet** pms, uint32_t u) nogil:
|
|
840
|
+
cdef size_t l = sizeof(urange_to_mj_mappings) // sizeof(urange_to_mj_mappings[0])
|
|
841
|
+
cdef size_t s = 0, e = l
|
|
842
|
+
cdef size_t m, i
|
|
843
|
+
cdef const URangeToMJMappings* um
|
|
844
|
+
cdef const MJMappingSet* ms
|
|
845
|
+
while s < e and e <= l:
|
|
846
|
+
m = (s + e) // 2
|
|
847
|
+
um = &urange_to_mj_mappings[m]
|
|
848
|
+
if u < um.start:
|
|
849
|
+
e = m
|
|
850
|
+
continue
|
|
851
|
+
elif u > um.end:
|
|
852
|
+
s = m + 1
|
|
853
|
+
continue
|
|
854
|
+
if u > um.end:
|
|
855
|
+
break
|
|
856
|
+
ms = &um.mss[u - um.start]
|
|
857
|
+
if ms.l == 0:
|
|
858
|
+
break
|
|
859
|
+
pms[0] = ms
|
|
860
|
+
return True
|
|
861
|
+
return False
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
ctypedef struct MJShrinkCandidates:
|
|
865
|
+
size_t l
|
|
866
|
+
UIVSPair[20]* a
|
|
867
|
+
size_t* al
|
|
868
|
+
size_t* is_
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
cdef Py_UCS4 to_ivs(int n) nogil:
|
|
872
|
+
if n < 16:
|
|
873
|
+
return 0xfe00 + n
|
|
874
|
+
else:
|
|
875
|
+
return 0xe00f0 + n
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
cdef object MJShrinkCandidates_append_candidates(MJShrinkCandidates* cands, list li, int limit):
|
|
879
|
+
cdef size_t i
|
|
880
|
+
cdef Py_ssize_t l
|
|
881
|
+
cdef PyUnicodeWriter* w
|
|
882
|
+
cdef UIVSPair* c
|
|
883
|
+
|
|
884
|
+
while True:
|
|
885
|
+
if limit >= 0:
|
|
886
|
+
limit -= 1
|
|
887
|
+
if limit < 0:
|
|
888
|
+
break
|
|
889
|
+
l = 0
|
|
890
|
+
for i in range(cands.l):
|
|
891
|
+
l += 2 if cands.a[i][cands.is_[i]].sv else 1
|
|
892
|
+
|
|
893
|
+
w = PyUnicodeWriter_Create(l)
|
|
894
|
+
try:
|
|
895
|
+
for i in range(cands.l):
|
|
896
|
+
c = &cands.a[i][cands.is_[i]]
|
|
897
|
+
PyUnicodeWriter_WriteChar(w, c.u)
|
|
898
|
+
if c.sv:
|
|
899
|
+
PyUnicodeWriter_WriteChar(w, to_ivs(c.s))
|
|
900
|
+
except:
|
|
901
|
+
PyUnicodeWriter_Discard(w)
|
|
902
|
+
raise
|
|
903
|
+
|
|
904
|
+
li.append(PyUnicodeWriter_Finish(w))
|
|
905
|
+
|
|
906
|
+
for i in range(cands.l):
|
|
907
|
+
cands.is_[i] += 1
|
|
908
|
+
if cands.is_[i] < cands.al[i]:
|
|
909
|
+
break
|
|
910
|
+
cands.is_[i] = 0
|
|
911
|
+
else:
|
|
912
|
+
break
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
cdef void MJShrinkCandidates_fini(MJShrinkCandidates* cands) noexcept:
|
|
916
|
+
if cands.a != NULL:
|
|
917
|
+
free(cands.a)
|
|
918
|
+
if cands.al != NULL:
|
|
919
|
+
free(cands.al)
|
|
920
|
+
if cands.is_ != NULL:
|
|
921
|
+
free(cands.is_)
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
cdef int resolve_ivs_no(Py_UCS4 n) nogil:
|
|
925
|
+
# VS1 to VS16
|
|
926
|
+
if n >= 0xfe00 and n < 0xfe10:
|
|
927
|
+
return <int>n - <int>0xfe00
|
|
928
|
+
# VS17 to VS256
|
|
929
|
+
if n >= 0xe0100 and n < 0xe01f0:
|
|
930
|
+
return <int>n - <int>0xe00f0
|
|
931
|
+
return -1
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
cdef MJShrinkCandidates_init(MJShrinkCandidates* cands, unicode in_, int combo):
|
|
935
|
+
cdef int uk = PyUnicode_KIND(in_)
|
|
936
|
+
cdef Py_ssize_t ul = PyUnicode_GET_LENGTH(in_)
|
|
937
|
+
cdef void* ud = PyUnicode_DATA(in_)
|
|
938
|
+
cdef Py_ssize_t i = 0
|
|
939
|
+
cdef size_t k, j, l, p
|
|
940
|
+
cdef int iv
|
|
941
|
+
cdef Py_UCS4 u, nu
|
|
942
|
+
cdef uint32_t uu
|
|
943
|
+
cdef const MJShrinkMappingUnicodeSet* sm
|
|
944
|
+
cdef const MJMappingSet* ms
|
|
945
|
+
cdef const MJMapping* cmm[64]
|
|
946
|
+
cdef const MJMapping* mm
|
|
947
|
+
cdef const MJMapping** cmmp
|
|
948
|
+
cdef const MJMapping** cmme
|
|
949
|
+
cdef UIVSPair[20] c
|
|
950
|
+
cdef UIVSPair[20]* a
|
|
951
|
+
cdef size_t* al
|
|
952
|
+
cdef size_t* is_
|
|
953
|
+
|
|
954
|
+
a = <UIVSPair[20]*>calloc(ul, sizeof(UIVSPair[20]))
|
|
955
|
+
if a == NULL:
|
|
956
|
+
raise MemoryError()
|
|
957
|
+
al = <size_t*>calloc(ul, sizeof(size_t))
|
|
958
|
+
if al == NULL:
|
|
959
|
+
free(a)
|
|
960
|
+
raise MemoryError()
|
|
961
|
+
is_ = <size_t*>calloc(ul, sizeof(size_t))
|
|
962
|
+
if is_ == NULL:
|
|
963
|
+
free(al)
|
|
964
|
+
free(a)
|
|
965
|
+
raise MemoryError()
|
|
966
|
+
|
|
967
|
+
p = 0
|
|
968
|
+
while i < ul:
|
|
969
|
+
is_[p] = 0
|
|
970
|
+
l = 0
|
|
971
|
+
iv = -1
|
|
972
|
+
u = PyUnicode_READ(uk, ud, i)
|
|
973
|
+
i += 1
|
|
974
|
+
if i < ul:
|
|
975
|
+
nu = PyUnicode_READ(uk, ud, i)
|
|
976
|
+
iv = resolve_ivs_no(nu)
|
|
977
|
+
if iv >= 0:
|
|
978
|
+
i += 1
|
|
979
|
+
|
|
980
|
+
cmme = cmm
|
|
981
|
+
if lookup_mj_mapping_table(&ms, u):
|
|
982
|
+
if iv >= 0:
|
|
983
|
+
# expecting exact match
|
|
984
|
+
for j in range(ms.l):
|
|
985
|
+
mm = &ms.ms[j]
|
|
986
|
+
for k in range(sizeof(mm.v) / sizeof(mm.v[0])):
|
|
987
|
+
if not mm.v[k].v:
|
|
988
|
+
mm = NULL
|
|
989
|
+
break
|
|
990
|
+
if mm.v[k].u == u and mm.v[k].sv and mm.v[k].s == iv:
|
|
991
|
+
break
|
|
992
|
+
else:
|
|
993
|
+
mm = NULL
|
|
994
|
+
if mm != NULL:
|
|
995
|
+
cmmp = cmm
|
|
996
|
+
while cmmp < cmme:
|
|
997
|
+
if cmmp[0] == mm:
|
|
998
|
+
break
|
|
999
|
+
cmmp += 1
|
|
1000
|
+
else:
|
|
1001
|
+
cmme[0] = mm
|
|
1002
|
+
cmme += 1
|
|
1003
|
+
if cmme >= cmm + sizeof(cmm) / sizeof(cmm[0]):
|
|
1004
|
+
raise MemoryError()
|
|
1005
|
+
break
|
|
1006
|
+
else:
|
|
1007
|
+
# search for all candidates
|
|
1008
|
+
for j in range(ms.l):
|
|
1009
|
+
mm = &ms.ms[j]
|
|
1010
|
+
for k in range(sizeof(mm.v) / sizeof(mm.v[0])):
|
|
1011
|
+
if not mm.v[k].v:
|
|
1012
|
+
mm = NULL
|
|
1013
|
+
break
|
|
1014
|
+
if mm.v[k].u == u and not mm.v[k].sv:
|
|
1015
|
+
break
|
|
1016
|
+
else:
|
|
1017
|
+
mm = NULL
|
|
1018
|
+
if mm != NULL:
|
|
1019
|
+
cmmp = cmm
|
|
1020
|
+
while cmmp < cmme:
|
|
1021
|
+
if cmmp[0] == mm:
|
|
1022
|
+
break
|
|
1023
|
+
cmmp += 1
|
|
1024
|
+
else:
|
|
1025
|
+
cmme[0] = mm
|
|
1026
|
+
cmme += 1
|
|
1027
|
+
if cmme >= cmm + sizeof(cmm) / sizeof(cmm[0]):
|
|
1028
|
+
raise MemoryError()
|
|
1029
|
+
if combo & 16 == 0:
|
|
1030
|
+
# narrow down to the canonical glyph, if any
|
|
1031
|
+
cmmp = cmm
|
|
1032
|
+
while cmmp < cmme:
|
|
1033
|
+
if cmmp[0].iu == u:
|
|
1034
|
+
cmm[0] = cmmp[0]
|
|
1035
|
+
cmme = cmm + 1
|
|
1036
|
+
break
|
|
1037
|
+
cmmp += 1
|
|
1038
|
+
|
|
1039
|
+
cmmp = cmm
|
|
1040
|
+
while cmmp < cmme:
|
|
1041
|
+
mm = cmmp[0]
|
|
1042
|
+
|
|
1043
|
+
sm = &mj_shrink_mappings[mm.mj]
|
|
1044
|
+
if MJShrinkMappingUnicodeSet_valid(sm):
|
|
1045
|
+
if combo & 1 != 0:
|
|
1046
|
+
for j in range(sizeof(sm._0) // sizeof(sm._0[0])):
|
|
1047
|
+
uu = sm._0[j]
|
|
1048
|
+
if uu == <uint32_t>-1:
|
|
1049
|
+
break
|
|
1050
|
+
if uu == u and iv < 0:
|
|
1051
|
+
continue
|
|
1052
|
+
for k in range(l):
|
|
1053
|
+
if c[k].u == uu and not c[k].sv:
|
|
1054
|
+
break
|
|
1055
|
+
else:
|
|
1056
|
+
c[l].u = uu
|
|
1057
|
+
c[l].v = True
|
|
1058
|
+
c[l].sv = False
|
|
1059
|
+
c[l].s = 0
|
|
1060
|
+
l += 1
|
|
1061
|
+
if combo & 2 != 0:
|
|
1062
|
+
for j in range(sizeof(sm._1) // sizeof(sm._1[0])):
|
|
1063
|
+
uu = sm._1[j]
|
|
1064
|
+
if uu == <uint32_t>-1:
|
|
1065
|
+
break
|
|
1066
|
+
if uu == u and iv < 0:
|
|
1067
|
+
continue
|
|
1068
|
+
for k in range(l):
|
|
1069
|
+
if c[k].u == uu and not c[k].sv:
|
|
1070
|
+
break
|
|
1071
|
+
else:
|
|
1072
|
+
c[l].u = uu
|
|
1073
|
+
c[l].v = True
|
|
1074
|
+
c[l].sv = False
|
|
1075
|
+
c[l].s = 0
|
|
1076
|
+
l += 1
|
|
1077
|
+
if combo & 4 != 0:
|
|
1078
|
+
for j in range(sizeof(sm._2) // sizeof(sm._2[0])):
|
|
1079
|
+
uu = sm._2[j]
|
|
1080
|
+
if uu == <uint32_t>-1:
|
|
1081
|
+
break
|
|
1082
|
+
if uu == u and iv < 0:
|
|
1083
|
+
continue
|
|
1084
|
+
for k in range(l):
|
|
1085
|
+
if c[k].u == uu and not c[k].sv:
|
|
1086
|
+
break
|
|
1087
|
+
else:
|
|
1088
|
+
c[l].u = uu
|
|
1089
|
+
c[l].v = True
|
|
1090
|
+
c[l].sv = False
|
|
1091
|
+
c[l].s = 0
|
|
1092
|
+
l += 1
|
|
1093
|
+
if combo & 8 != 0:
|
|
1094
|
+
for j in range(sizeof(sm._3) // sizeof(sm._3[0])):
|
|
1095
|
+
uu = sm._3[j]
|
|
1096
|
+
if uu == <uint32_t>-1:
|
|
1097
|
+
break
|
|
1098
|
+
if uu == u and iv < 0:
|
|
1099
|
+
continue
|
|
1100
|
+
for k in range(l):
|
|
1101
|
+
if c[k].u == uu and not c[k].sv:
|
|
1102
|
+
break
|
|
1103
|
+
else:
|
|
1104
|
+
c[l].u = uu
|
|
1105
|
+
c[l].v = True
|
|
1106
|
+
c[l].sv = False
|
|
1107
|
+
c[l].s = 0
|
|
1108
|
+
l += 1
|
|
1109
|
+
cmmp += 1
|
|
1110
|
+
|
|
1111
|
+
cmmp = cmm
|
|
1112
|
+
while cmmp < cmme:
|
|
1113
|
+
mm = cmmp[0]
|
|
1114
|
+
|
|
1115
|
+
for j in range(sizeof(mm.v) / sizeof(mm.v[0])):
|
|
1116
|
+
if not mm.v[j].v:
|
|
1117
|
+
break
|
|
1118
|
+
if not mm.v[j].sv:
|
|
1119
|
+
uu = mm.v[j].u
|
|
1120
|
+
for k in range(l):
|
|
1121
|
+
if c[k].u == uu and not c[k].sv:
|
|
1122
|
+
break
|
|
1123
|
+
else:
|
|
1124
|
+
c[l].u = uu
|
|
1125
|
+
c[l].v = True
|
|
1126
|
+
c[l].sv = False
|
|
1127
|
+
c[l].s = 0
|
|
1128
|
+
l += 1
|
|
1129
|
+
|
|
1130
|
+
cmmp += 1
|
|
1131
|
+
|
|
1132
|
+
if l == 0:
|
|
1133
|
+
c[0].u = u
|
|
1134
|
+
c[0].v = True
|
|
1135
|
+
c[0].sv = iv >= 0
|
|
1136
|
+
c[0].s = iv
|
|
1137
|
+
l = 1
|
|
1138
|
+
al[p] = l
|
|
1139
|
+
memcpy(a[p], c, sizeof(UIVSPair[20]))
|
|
1140
|
+
p += 1
|
|
1141
|
+
|
|
1142
|
+
cands.l = p
|
|
1143
|
+
cands.a = a
|
|
1144
|
+
cands.al = al
|
|
1145
|
+
cands.is_ = is_
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
def mj_shrink_candidates(unicode in_, int combo, int limit = 100):
|
|
1149
|
+
cdef MJShrinkCandidates cands
|
|
1150
|
+
cands.a = cands.al = cands.is_ = NULL
|
|
1151
|
+
retval = []
|
|
1152
|
+
try:
|
|
1153
|
+
MJShrinkCandidates_init(&cands, in_, combo)
|
|
1154
|
+
MJShrinkCandidates_append_candidates(&cands, retval, limit)
|
|
1155
|
+
finally:
|
|
1156
|
+
MJShrinkCandidates_fini(&cands)
|
|
1157
|
+
return retval
|