1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
|
import sys
from pypy.interpreter.error import OperationError, oefmt
from rpython.rlib.objectmodel import specialize
from rpython.rlib.rstring import StringBuilder
from rpython.rlib import rutf8
from rpython.rlib.rarithmetic import r_uint, intmask
from rpython.rtyper.lltypesystem import rffi
from pypy.module.unicodedata.interp_ucd import unicodedb
from rpython.rlib import runicode
@specialize.memo()
def decode_error_handler(space):
# Fast version of the "strict" errors handler.
def raise_unicode_exception_decode(errors, encoding, msg, s,
startingpos, endingpos):
raise OperationError(space.w_UnicodeDecodeError,
space.newtuple([space.newtext(encoding),
space.newbytes(s),
space.newint(startingpos),
space.newint(endingpos),
space.newtext(msg)]))
return raise_unicode_exception_decode
def decode_never_raise(errors, encoding, msg, s, startingpos, endingpos):
assert startingpos >= 0
assert endingpos >= 0
ux = ['\ux' + hex(ord(x))[2:].upper() for x in s[startingpos:endingpos]]
return ''.join(ux), endingpos, 'b', ''
@specialize.memo()
def encode_error_handler(space):
# Fast version of the "strict" errors handler.
def raise_unicode_exception_encode(errors, encoding, msg, utf8,
startingpos, endingpos):
u_len = rutf8.codepoints_in_utf8(utf8)
raise OperationError(space.w_UnicodeEncodeError,
space.newtuple([space.newtext(encoding),
space.newutf8(utf8, u_len),
space.newint(startingpos),
space.newint(endingpos),
space.newtext(msg)]))
return raise_unicode_exception_encode
def default_error_encode(
errors, encoding, msg, u, startingpos, endingpos):
"""A default handler, for tests"""
assert endingpos >= 0
if errors == 'replace':
return '?', endingpos
if errors == 'ignore':
return '', endingpos
raise ValueError
# ____________________________________________________________
_WIN32 = sys.platform == 'win32'
_MACOSX = sys.platform == 'darwin'
def fsdecode(space, w_string):
from pypy.module._codecs import interp_codecs
state = space.fromcache(interp_codecs.CodecState)
errorhandler=state.decode_error_handler
utf8 = space.bytes_w(w_string)
# fast path for ascii
if rutf8.first_non_ascii_char(utf8) < 0:
return space.newtext(utf8, len(utf8))
if _WIN32:
import pypy.interpreter.unicodehelper_win32 as win32
slen = len(utf8)
utf8, _, lgt = str_decode_utf8(utf8, 'surrogateescape', True, errorhandler)
elif 0 and _MACOSX:
utf8, lgt, pos = str_decode_utf8(utf8, 'surrogateescape', True,
errorhandler, allow_surrogates=False)
elif space.sys.filesystemencoding is None or state.codec_need_encodings:
# bootstrap check: if the filesystemencoding isn't initialized
# or the filesystem codec is implemented in Python we cannot
# use it before the codecs are ready. use the locale codec
# instead
from pypy.module._codecs.locale import (
str_decode_locale_surrogateescape)
utf8, lgt = str_decode_locale_surrogateescape(utf8)
else:
from pypy.module.sys.interp_encoding import getfilesystemencoding
return space.call_method(w_string, 'decode',
getfilesystemencoding(space),
space.newtext('surrogateescape'))
return space.newtext(utf8, lgt)
def fsencode(space, w_uni):
from pypy.module._codecs import interp_codecs
state = space.fromcache(interp_codecs.CodecState)
if _WIN32:
errorhandler=state.encode_error_handler
utf8 = space.utf8_w(w_uni)
bytes = utf8_encode_utf_8(utf8, 'surrogateescape', errorhandler)
elif 0 and _MACOSX:
utf8 = space.utf8_w(w_uni)
errorhandler=state.encode_error_handler,
bytes = utf8_encode_utf_8(utf8, 'surrogateescape', errorhandler,
allow_surrogates=False)
elif space.sys.filesystemencoding is None or state.codec_need_encodings:
# bootstrap check: if the filesystemencoding isn't initialized
# or the filesystem codec is implemented in Python we cannot
# use it before the codecs are ready. use the locale codec
# instead
from pypy.module._codecs.locale import (
utf8_encode_locale_surrogateescape)
utf8 = space.utf8_w(w_uni)
ulen = space.len_w(w_uni)
if '\x00' in utf8:
raise oefmt(space.w_ValueError, "embedded null character")
bytes = utf8_encode_locale_surrogateescape(utf8, ulen)
else:
from pypy.module.sys.interp_encoding import getfilesystemencoding
return space.call_method(w_uni, 'encode',
getfilesystemencoding(space),
space.newtext('surrogateescape'))
return space.newbytes(bytes)
def encode(space, w_data, encoding=None, errors='strict'):
from pypy.objspace.std.unicodeobject import encode_object
return encode_object(space, w_data, encoding, errors)
# These functions take and return unwrapped rpython strings
def decode_raw_unicode_escape(space, string):
return str_decode_raw_unicode_escape(
string, "strict",
final=True, errorhandler=decode_error_handler(space))
def check_ascii_or_raise(space, string):
try:
rutf8.check_ascii(string)
except rutf8.CheckError as e:
decode_error_handler(space)('strict', 'ascii',
'ordinal not in range(128)', string,
e.pos, e.pos + 1)
assert False, "unreachable"
def check_utf8_or_raise(space, string, start=0, end=-1):
# Surrogates are accepted and not treated specially at all.
# If there happen to be two 3-bytes encoding a pair of surrogates,
# you still get two surrogate unicode characters in the result.
# These are the Python3 rules, Python2 differs
assert isinstance(string, str)
try:
return rutf8.check_utf8(string, True, start, end)
except rutf8.CheckError as e:
decode_error_handler(space)('strict', 'utf-8',
'unexpected end of data', string,
e.pos, e.pos + 1)
def str_decode_ascii(s, errors, final, errorhandler):
try:
rutf8.check_ascii(s)
return s, len(s), len(s)
except rutf8.CheckError:
return _str_decode_ascii_slowpath(s, errors, final, errorhandler)
def _str_decode_ascii_slowpath(s, errors, final, errorhandler):
i = 0
res = StringBuilder()
while i < len(s):
ch = s[i]
if ord(ch) > 0x7F:
r, i, rettype, s = errorhandler(errors, 'ascii', 'ordinal not in range(128)',
s, i, i + 1)
res.append(r)
else:
res.append(ch)
i += 1
ress = res.build()
lgt = rutf8.check_utf8(ress, True)
return ress, lgt, lgt
def str_decode_latin_1(s, errors, final, errorhandler):
try:
rutf8.check_ascii(s)
return s, len(s), len(s)
except rutf8.CheckError:
return _str_decode_latin_1_slowpath(s, errors, final, errorhandler)
def _str_decode_latin_1_slowpath(s, errors, final, errorhandler):
res = StringBuilder(len(s))
i = 0
while i < len(s):
if ord(s[i]) > 0x7F:
while i < len(s) and ord(s[i]) > 0x7F:
rutf8.unichr_as_utf8_append(res, ord(s[i]))
i += 1
else:
start = i
end = i + 1
while end < len(s) and ord(s[end]) <= 0x7F:
end += 1
res.append_slice(s, start, end)
i = end
return res.build(), len(s), len(s)
class ErrorHandlerError(Exception):
def __init__(self, new, old):
self.new = new
self.old = old
def utf8_encode_utf_8(s, errors, errorhandler, allow_surrogates=False):
if len(s) == 0:
return ''
# two fast paths
if allow_surrogates:
# already valid utf-8 with surrogates, surrogates are allowed, so just
# return
return s
if not rutf8.has_surrogates(s):
# already valid utf-8 and doesn't contain surrogates, so we don't need
# to do anything
return s
# annoying slow path
return _utf8_encode_utf_8_deal_with_surrogates(s, errors, errorhandler)
def _utf8_encode_utf_8_deal_with_surrogates(s, errors, errorhandler):
pos = 0
upos = 0
result = StringBuilder(len(s))
while pos < len(s):
try:
rutf8.check_utf8(s, allow_surrogates=False, start=pos)
# otherwise the fast path above would have triggered
assert pos != 0
result.append_slice(s, pos, len(s))
break
except rutf8.CheckError as e:
end = e.pos
assert end >= 0
result.append_slice(s, pos, end)
upos += rutf8.codepoints_in_utf8(s, start=pos, end=end)
pos = end
# Try to get collect surrogates in one pass
# XXX do we care about performance in this case?
# XXX should this loop for more than one pair?
delta = 1
uchr = rutf8.codepoint_at_pos(s, pos)
if 0xD800 <= uchr <= 0xDBFF:
pos = rutf8.next_codepoint_pos(s, pos)
if pos < len(s):
uchr = rutf8.codepoint_at_pos(s, pos)
if 0xDC00 <= uchr <= 0xDFFF:
delta += 1
res, newindex, rettype, obj = errorhandler(errors, 'utf-8',
'surrogates not allowed', s, upos, upos + delta)
if rettype == 'u':
try:
rutf8.check_ascii(res)
except rutf8.CheckError:
# this is a weird behaviour of CPython, but it's what happens
errorhandler("strict", 'utf-8', 'surrogates not allowed', s, upos, upos + delta)
assert 0, "unreachable"
s = obj
result.append(res)
if newindex <= upos:
raise ErrorHandlerError(newindex, upos)
upos = newindex
pos = rutf8._pos_at_index(s, upos)
return result.build()
def utf8_encode_latin_1(s, errors, errorhandler, allow_surrogates=False):
try:
rutf8.check_ascii(s)
return s
except rutf8.CheckError, e:
return _utf8_encode_latin_1_slowpath(s, e.pos, errors, errorhandler)
def _utf8_encode_latin_1_slowpath(s, first_non_ascii_char, errors, errorhandler):
result = StringBuilder(len(s))
result.append_slice(s, 0, first_non_ascii_char)
pos = index = first_non_ascii_char
while pos < len(s):
ch = rutf8.codepoint_at_pos(s, pos)
if ch <= 0xFF:
result.append(chr(ch))
index += 1
pos = rutf8.next_codepoint_pos(s, pos)
else:
startindex = index
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
while pos < len(s) and rutf8.codepoint_at_pos(s, pos) > 0xFF:
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
msg = "ordinal not in range(256)"
res, newindex, rettype, obj = errorhandler(
errors, 'latin1', msg, s, startindex, index)
if rettype == 'u':
for cp in rutf8.Utf8StringIterator(res):
if cp > 0xFF:
errorhandler("strict", 'latin1', msg, s, startindex, index)
raise RuntimeError('error handler should not have returned')
result.append(chr(cp))
else:
for ch in res:
result.append(ch)
s = obj
if index != newindex: # Should be uncommon
index = newindex
pos = rutf8._pos_at_index(s, newindex)
return result.build()
def utf8_encode_ascii(s, errors, errorhandler, allow_surrogates=False):
""" Don't be confused - this is a slowpath for errors e.g. "ignore"
or an obscure errorhandler
"""
result = StringBuilder(len(s))
index = 0
pos = 0
while pos < len(s):
ch = rutf8.codepoint_at_pos(s, pos)
if ch <= 0x7F:
result.append(chr(ch))
index += 1
pos = rutf8.next_codepoint_pos(s, pos)
else:
startindex = index
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
while pos < len(s) and rutf8.codepoint_at_pos(s, pos) > 0x7F:
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
msg = "ordinal not in range(128)"
res, newindex, rettype, obj = errorhandler(
errors, 'ascii', msg, s, startindex, index)
if rettype == 'u':
for cp in rutf8.Utf8StringIterator(res):
if cp > 0x80:
errorhandler("strict", 'ascii', msg, s, startindex, index)
raise RuntimeError('error handler should not have returned')
result.append(chr(cp))
else:
for ch in res:
result.append(ch)
obj = s
pos = rutf8._pos_at_index(s, newindex)
return result.build()
if _WIN32:
import pypy.interpreter.unicodehelper_win32 as win32
def utf8_encode_mbcs(s, errors, errorhandler, allow_surrogates=False):
return win32.utf8_encode_mbcs(s, errors, errorhandler)
def utf8_encode_utf8(s, errors, errorhandler, allow_surrogates=False):
return win32.utf8_encode_utf8(s, errors, errorhandler)
def str_decode_mbcs(s, errors, final, errorhandler):
res, size = win32.str_decode_mbcs(s, errors, errorhandler, final=final)
return res, size, size
def utf8_encode_oem(s, errors, errorhandler, allow_surrogates=False):
res = win32.utf8_encode_oem(s, errors, errorhandler)
return res
def str_decode_oem(s, errors, final, errorhandler):
res, size = win32.str_decode_oem(s, errors, errorhandler, final)
return res, size, size
def utf8_encode_code_page(cp, s, errors, errorhandler, allow_surrogates=False):
res = win32.utf8_encode_code_page(cp, s, errors, errorhandler)
return res
def str_decode_code_page(cp, s, errors, final, errorhandler):
res, size = win32.str_decode_code_page(cp, s, errors, errorhandler, final)
return res, size, size
def str_decode_utf8(s, errors, final, errorhandler, allow_surrogates=False):
try:
# fast version first
return s, rutf8.check_utf8(s, allow_surrogates=allow_surrogates), len(s)
except rutf8.CheckError:
return _str_decode_utf8_slowpath(
s, errors, final, errorhandler, allow_surrogates=allow_surrogates)
def _str_decode_utf8_slowpath(s, errors, final, errorhandler, allow_surrogates):
""" Same as checking for the valid utf8, but we know the utf8 is not
valid so we're trying to either raise or pack stuff with error handler.
The key difference is that this is call_may_force.
In CPython this is done in unicode_decode_utf8, which has no
allow_surrogates. That argument is used in at least decode_utf8sp, in
interpreter.error._compute_value.
"""
if errors is None:
errors = 'strict'
result = StringBuilder(len(s))
pos = 0
while pos < len(s):
ordch1 = ord(s[pos])
# fast path for ASCII
# XXX maybe use a while loop here
if ordch1 <= 0x7F:
pos += 1
result.append(chr(ordch1))
continue
n = ord(runicode._utf8_code_length[ordch1 - 0x80])
if pos + n > len(s):
# argh, this obscure block of code is mostly a copy of
# what follows :-(
charsleft = len(s) - pos - 1 # either 0, 1, 2
# note: when we get the 'unexpected end of data' we need
# to care about the pos returned; it can be lower than len(s),
# in case we need to continue running this loop
if not charsleft:
# there's only the start byte and nothing else
if not final:
break
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'unexpected end of data',
s, pos, pos+1)
result.append(r)
continue
ordch2 = ord(s[pos+1])
if n == 3:
# 3-bytes seq with only a continuation byte
if rutf8._invalid_byte_2_of_3(ordch1, ordch2, allow_surrogates):
# second byte invalid, take the first and continue
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+1)
result.append(r)
continue
else:
# second byte valid, but third byte missing
if not final:
break
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'unexpected end of data',
s, pos, pos+2)
result.append(r)
continue
elif n == 4:
# 4-bytes seq with 1 or 2 continuation bytes
if rutf8._invalid_byte_2_of_4(ordch1, ordch2):
# second byte invalid, take the first and continue
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+1)
result.append(r)
continue
elif charsleft == 2 and rutf8._invalid_byte_3_of_4(ord(s[pos+2])):
# third byte invalid, take the first two and continue
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+2)
result.append(r)
continue
else:
# there's only 1 or 2 valid cb, but the others are missing
if not final:
break
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'unexpected end of data',
s, pos, pos+charsleft+1)
result.append(r)
continue
raise AssertionError("unreachable")
if n == 0:
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid start byte',
s, pos, pos+1)
result.append(r)
elif n == 1:
assert 0, "ascii should have gone through the fast path"
elif n == 2:
ordch2 = ord(s[pos+1])
if rutf8._invalid_byte_2_of_2(ordch2):
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+1)
result.append(r)
continue
# 110yyyyy 10zzzzzz -> 00000000 00000yyy yyzzzzzz
result.append(chr(ordch1))
result.append(chr(ordch2))
pos += 2
elif n == 3:
ordch2 = ord(s[pos+1])
ordch3 = ord(s[pos+2])
if rutf8._invalid_byte_2_of_3(ordch1, ordch2, allow_surrogates):
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+1)
result.append(r)
continue
elif rutf8._invalid_byte_3_of_3(ordch3):
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+2)
result.append(r)
continue
# 1110xxxx 10yyyyyy 10zzzzzz -> 00000000 xxxxyyyy yyzzzzzz
result.append(chr(ordch1))
result.append(chr(ordch2))
result.append(chr(ordch3))
pos += 3
elif n == 4:
ordch2 = ord(s[pos+1])
ordch3 = ord(s[pos+2])
ordch4 = ord(s[pos+3])
if rutf8._invalid_byte_2_of_4(ordch1, ordch2):
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+1)
result.append(r)
continue
elif rutf8._invalid_byte_3_of_4(ordch3):
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+2)
result.append(r)
continue
elif rutf8._invalid_byte_4_of_4(ordch4):
r, pos, rettype, s = errorhandler(errors, 'utf-8',
'invalid continuation byte',
s, pos, pos+3)
result.append(r)
continue
# 11110www 10xxxxxx 10yyyyyy 10zzzzzz -> 000wwwxx xxxxyyyy yyzzzzzz
result.append(chr(ordch1))
result.append(chr(ordch2))
result.append(chr(ordch3))
result.append(chr(ordch4))
pos += 4
r = result.build()
# XXX can keep track of the resulting length without calling check_utf8
# here
return r, rutf8.check_utf8(r, True), pos
hexdigits = "0123456789ABCDEFabcdef"
def hexescape(builder, s, pos, digits,
encoding, errorhandler, message, errors):
chr = 0
if pos + digits > len(s):
endinpos = pos
while endinpos < len(s) and s[endinpos] in hexdigits:
endinpos += 1
r, pos, rettype, s = errorhandler(
errors, encoding, message, s, pos - 2, endinpos)
builder.append(r)
else:
try:
chr = int(s[pos:pos + digits], 16)
except ValueError:
endinpos = pos
while s[endinpos] in hexdigits:
endinpos += 1
r, pos, rettype, s = errorhandler(
errors, encoding, message, s, pos - 2, endinpos)
builder.append(r)
else:
# when we get here, chr is a 32-bit unicode character
try:
builder.append_code(chr)
pos += digits
except rutf8.OutOfRange:
message = "illegal Unicode character"
r, pos, rettype, s = errorhandler(
errors, encoding, message, s, pos - 2, pos + digits)
builder.append(r)
return pos, s
def str_decode_unicode_escape(s, errors, final, errorhandler, ud_handler):
if len(s) == 0:
return '', 0, 0, None
builder = rutf8.Utf8StringBuilder(len(s))
pos = 0
first_escape_error_char = None
while pos < len(s):
ch = s[pos]
# Non-escape characters are interpreted as Unicode ordinals
if ch != '\\':
if ord(ch) > 0x7F:
builder.append_code(ord(ch))
else:
builder.append(ch)
pos += 1
continue
# - Escapes
if pos + 1 >= len(s):
if not final:
break
message = "\\ at end of string"
r, pos, rettype, s = errorhandler(errors, "unicodeescape",
message, s, pos - 1, len(s))
builder.append(r)
continue
pos += 1
ch = s[pos]
pos += 1
# \x escapes
if ch == '\n':
pass
elif ch == '\\':
builder.append_char('\\')
elif ch == '\'':
builder.append_char('\'')
elif ch == '\"':
builder.append_char('\"')
elif ch == 'b':
builder.append_char('\b')
elif ch == 'f':
builder.append_char('\f')
elif ch == 't':
builder.append_char('\t')
elif ch == 'n':
builder.append_char('\n')
elif ch == 'r':
builder.append_char('\r')
elif ch == 'v':
builder.append_char('\v')
elif ch == 'a':
builder.append_char('\a')
elif '0' <= ch <= '7':
x = ord(ch) - ord('0')
if pos < len(s):
ch = s[pos]
if '0' <= ch <= '7':
pos += 1
x = (x << 3) + ord(ch) - ord('0')
if pos < len(s):
ch = s[pos]
if '0' <= ch <= '7':
pos += 1
x = (x << 3) + ord(ch) - ord('0')
if x > 0x7F:
builder.append_code(x)
else:
builder.append_char(chr(x))
# hex escapes
# \xXX
elif ch == 'x':
digits = 2
if pos + digits > len(s) and not final:
pos -= 2
break
message = "truncated \\xXX escape"
pos, s = hexescape(builder, s, pos, digits,
"unicodeescape", errorhandler, message, errors)
# \uXXXX
elif ch == 'u':
digits = 4
if pos + digits > len(s) and not final:
pos -= 2
break
message = "truncated \\uXXXX escape"
pos, s = hexescape(builder, s, pos, digits,
"unicodeescape", errorhandler, message, errors)
# \UXXXXXXXX
elif ch == 'U':
digits = 8
if pos + digits > len(s) and not final:
pos -= 2
break
message = "truncated \\UXXXXXXXX escape"
pos, s = hexescape(builder, s, pos, digits,
"unicodeescape", errorhandler, message, errors)
# \N{name}
elif ch == 'N' and ud_handler is not None:
message = "malformed \\N character escape"
look = pos
if look < len(s) and s[look] == '{':
# look for the closing brace
while look < len(s) and s[look] != '}':
look += 1
if look < len(s) and s[look] == '}':
# found a name. look it up in the unicode database
message = "unknown Unicode character name"
name = s[pos + 1:look]
code = ud_handler.call(name)
if code < 0:
r, pos, rettype, s = errorhandler(
errors, "unicodeescape", message,
s, pos - 1, look + 1)
builder.append(r)
continue
pos = look + 1
builder.append_code(code)
else:
if not final:
pos -= 2
break
r, pos, rettype, s = errorhandler(errors, "unicodeescape",
message, s, pos - 1, look + 1)
builder.append(r)
else:
if not final:
pos -= 2
break
r, pos, rettype, s = errorhandler(errors, "unicodeescape",
message, s, pos - 1, look + 1)
builder.append(r)
else:
builder.append_char('\\')
builder.append_code(ord(ch))
first_escape_error_char = ch
return builder.build(), builder.getlength(), pos, first_escape_error_char
def wcharpsize2utf8(space, wcharp, size):
"""Safe version of rffi.wcharpsize2utf8.
Raises app-level ValueError if any wchar value is outside the valid
codepoint range.
"""
if _WIN32:
import pypy.interpreter.unicodehelper_win32 as win32
# wcharp is actually utf16
return win32._unibuf_to_utf8(wcharp, size)
else:
try:
return rffi.wcharpsize2utf8(wcharp, size)
except rutf8.OutOfRange as e:
raise wrap_unicode_out_of_range_error(space, e)
def wrap_unicode_out_of_range_error(space, e):
raise oefmt(space.w_ValueError,
"character %s is not in range [U+0000; U+10ffff]", 'U+%x' % e.code)
# ____________________________________________________________
# Raw unicode escape
def str_decode_raw_unicode_escape(s, errors, final=False,
errorhandler=None):
if len(s) == 0:
return '', 0, 0
builder = rutf8.Utf8StringBuilder(len(s))
pos = 0
while pos < len(s):
ch = s[pos]
pos += 1
# Non-escape characters are interpreted as Unicode ordinals
if ch != '\\':
builder.append_code(ord(ch))
continue
if pos == len(s):
if final:
# we have a backslash at the end of the string, stop here
builder.append_char('\\')
else:
pos -= 1
break
ch = s[pos]
if ch == "\\":
builder.append_char('\\')
pos += 1
continue
if s[pos] == 'u':
digits = 4
message = "truncated \\uXXXX escape"
elif s[pos] == 'U':
digits = 8
message = "truncated \\UXXXXXXXX escape"
else:
builder.append_char('\\')
builder.append_char(ch)
pos += 1
continue
pos += 1
if pos + digits > len(s) and not final:
pos -= 2
break
pos, s = hexescape(builder, s, pos, digits,
"rawunicodeescape", errorhandler, message, errors)
return builder.build(), builder.getlength(), pos
_utf8_encode_unicode_escape = rutf8.make_utf8_escape_function()
TABLE = '0123456789abcdef'
def raw_unicode_escape_helper(result, char):
if char >= 0x10000 or char < 0:
result.append("\\U")
zeros = 8
elif char >= 0x100:
result.append("\\u")
zeros = 4
else:
result.append("\\x")
zeros = 2
for i in range(zeros-1, -1, -1):
result.append(TABLE[(char >> (4 * i)) & 0x0f])
def utf8_encode_raw_unicode_escape(s, errors, errorhandler, allow_surrogates=False):
# errorhandler is not used: this function cannot cause Unicode errors
size = len(s)
if size == 0:
return ''
result = StringBuilder(size)
pos = 0
while pos < size:
oc = rutf8.codepoint_at_pos(s, pos)
if oc < 0x100:
result.append(chr(oc))
else:
raw_unicode_escape_helper(result, oc)
pos = rutf8.next_codepoint_pos(s, pos)
return result.build()
def utf8_encode_unicode_escape(s, errors, errorhandler, allow_surrogates=False):
return _utf8_encode_unicode_escape(s)
# ____________________________________________________________
# utf-7
# Three simple macros defining base-64
def _utf7_IS_BASE64(oc):
"Is c a base-64 character?"
c = chr(oc)
return c.isalnum() or c == '+' or c == '/'
def _utf7_TO_BASE64(n):
"Returns the base-64 character of the bottom 6 bits of n"
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[n & 0x3f]
def _utf7_FROM_BASE64(c):
"given that c is a base-64 character, what is its base-64 value?"
if c >= 'a':
return ord(c) - 71
elif c >= 'A':
return ord(c) - 65
elif c >= '0':
return ord(c) + 4
elif c == '+':
return 62
else: # c == '/'
return 63
def _utf7_DECODE_DIRECT(oc):
return oc <= 127 and oc != ord('+')
# The UTF-7 encoder treats ASCII characters differently according to
# whether they are Set D, Set O, Whitespace, or special (i.e. none of
# the above). See RFC2152. This array identifies these different
# sets:
# 0 : "Set D"
# alphanumeric and '(),-./:?
# 1 : "Set O"
# !"#$%&*;<=>@[]^_`{|}
# 2 : "whitespace"
# ht nl cr sp
# 3 : special (must be base64 encoded)
# everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127)
utf7_category = [
# nul soh stx etx eot enq ack bel bs ht nl vt np cr so si
3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3,
# dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
# sp ! " # $ % & ' ( ) * + , - . /
2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0,
# 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
# @ A B C D E F G H I J K L M N O
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
# P Q R S T U V W X Y Z [ \ ] ^ _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1,
# ` a b c d e f g h i j k l m n o
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
# p q r s t u v w x y z { | } ~ del
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3,
]
# ENCODE_DIRECT: this character should be encoded as itself. The
# answer depends on whether we are encoding set O as itself, and also
# on whether we are encoding whitespace as itself. RFC2152 makes it
# clear that the answers to these questions vary between
# applications, so this code needs to be flexible.
def _utf7_ENCODE_DIRECT(oc, directO, directWS):
return(oc < 128 and oc > 0 and
(utf7_category[oc] == 0 or
(directWS and utf7_category[oc] == 2) or
(directO and utf7_category[oc] == 1)))
def _utf7_ENCODE_CHAR(result, oc, base64bits, base64buffer):
if oc >= 0x10000:
# code first surrogate
base64bits += 16
base64buffer = (base64buffer << 16) | 0xd800 | ((oc-0x10000) >> 10)
while base64bits >= 6:
result.append(_utf7_TO_BASE64(base64buffer >> (base64bits-6)))
base64bits -= 6
# prepare second surrogate
oc = 0xDC00 | ((oc-0x10000) & 0x3FF)
base64bits += 16
base64buffer = (base64buffer << 16) | oc
while base64bits >= 6:
result.append(_utf7_TO_BASE64(base64buffer >> (base64bits-6)))
base64bits -= 6
return base64bits, base64buffer
def str_decode_utf_7(s, errors, final=False,
errorhandler=None):
if len(s) == 0:
return '', 0, 0
inShift = False
base64bits = 0
base64buffer = 0
surrogate = 0
outsize = 0
result = StringBuilder(len(s))
pos = 0
shiftOutStartPos = 0
startinpos = 0
while pos < len(s):
ch = s[pos]
if inShift: # in a base-64 section
if _utf7_IS_BASE64(ord(ch)): #consume a base-64 character
base64buffer = (base64buffer << 6) | _utf7_FROM_BASE64(ch)
assert base64buffer >= 0
base64bits += 6
pos += 1
if base64bits >= 16:
# enough bits for a UTF-16 value
outCh = base64buffer >> (base64bits - 16)
assert outCh >= 0
base64bits -= 16
base64buffer &= (1 << base64bits) - 1 # clear high bits
assert outCh <= 0xffff
if surrogate:
# expecting a second surrogate
if outCh >= 0xDC00 and outCh <= 0xDFFF:
code = (((surrogate & 0x3FF)<<10) |
(outCh & 0x3FF)) + 0x10000
rutf8.unichr_as_utf8_append(result, code)
outsize += 1
surrogate = 0
continue
else:
rutf8.unichr_as_utf8_append(result, surrogate,
allow_surrogates=True)
outsize += 1
surrogate = 0
# Not done with outCh: falls back to next line
if outCh >= 0xD800 and outCh <= 0xDBFF:
# first surrogate
surrogate = outCh
else:
outsize += 1
assert outCh >= 0
rutf8.unichr_as_utf8_append(result, outCh, True)
else:
# now leaving a base-64 section
inShift = False
if base64bits > 0: # left-over bits
if base64bits >= 6:
# We've seen at least one base-64 character
pos += 1
msg = "partial character in shift sequence"
r, pos, rettype, s = errorhandler(errors, 'utf7',
msg, s, pos-1, pos)
reslen = rutf8.check_utf8(r, True)
outsize += reslen
result.append(r)
continue
else:
# Some bits remain; they should be zero
if base64buffer != 0:
pos += 1
msg = "non-zero padding bits in shift sequence"
r, pos, rettype, s = errorhandler(errors, 'utf7',
msg, s, pos-1, pos)
reslen = rutf8.check_utf8(r, True)
outsize += reslen
result.append(r)
continue
if surrogate and _utf7_DECODE_DIRECT(ord(ch)):
outsize += 1
rutf8.unichr_as_utf8_append(result, surrogate, True)
surrogate = 0
if ch == '-':
# '-' is absorbed; other terminating characters are
# preserved
pos += 1
elif ch == '+':
startinpos = pos
pos += 1 # consume '+'
if pos < len(s) and s[pos] == '-': # '+-' encodes '+'
pos += 1
result.append('+')
outsize += 1
elif pos < len(s) and not _utf7_IS_BASE64(ord(s[pos])):
msg = "ill-formed sequence"
r, pos, rettype, s = errorhandler(errors, 'utf7', msg, s, pos-1, pos+1)
reslen = rutf8.check_utf8(r, True)
outsize += reslen
result.append(r)
else: # begin base64-encoded section
inShift = 1
surrogate = 0
shiftOutStartPos = result.getlength()
base64bits = 0
base64buffer = 0
elif _utf7_DECODE_DIRECT(ord(ch)): # character decodes at itself
result.append(ch)
outsize += 1
pos += 1
else:
startinpos = pos
pos += 1
msg = "unexpected special character"
r, pos, rettype, s = errorhandler(errors, 'utf7', msg, s, pos-1, pos)
reslen = rutf8.check_utf8(r, True)
outsize += reslen
result.append(r)
# end of string
final_length = result.getlength()
final_size = len(s)
if inShift and final: # in shift sequence, no more to follow
inShift = 0
if (surrogate or
base64bits >= 6 or
(base64bits > 0 and base64buffer != 0)):
# if we're in an inconsistent state, that's an error
msg = "unterminated shift sequence"
r, pos, rettype, s = errorhandler(errors, 'utf7', msg, s, shiftOutStartPos, pos)
reslen = rutf8.check_utf8(r, True)
outsize += reslen
result.append(r)
final_length = result.getlength()
elif inShift:
final_size = startinpos
final_length = shiftOutStartPos # back off output
assert final_length >= 0
return result.build()[:final_length], outsize, final_size
def utf8_encode_utf_7(s, errors, errorhandler, allow_surrogates=False):
# only uses s, other arguments are ignored
size = len(s)
if size == 0:
return ''
result = StringBuilder(size)
encodeSetO = encodeWhiteSpace = False
inShift = False
base64bits = 0
base64buffer = 0
pos = 0
while pos < size:
oc = rutf8.codepoint_at_pos(s, pos)
if not inShift:
if oc == ord('+'):
result.append('+-')
elif _utf7_ENCODE_DIRECT(oc, not encodeSetO, not encodeWhiteSpace):
result.append(chr(oc))
else:
result.append('+')
inShift = True
base64bits, base64buffer = _utf7_ENCODE_CHAR(
result, oc, base64bits, base64buffer)
else:
if _utf7_ENCODE_DIRECT(oc, not encodeSetO, not encodeWhiteSpace):
# shifting out
if base64bits: # output remaining bits
result.append(_utf7_TO_BASE64(base64buffer << (6-base64bits)))
base64buffer = 0
base64bits = 0
inShift = False
## Characters not in the BASE64 set implicitly unshift the
## sequence so no '-' is required, except if the character is
## itself a '-'
if _utf7_IS_BASE64(oc) or oc == ord('-'):
result.append('-')
result.append(chr(oc))
else:
base64bits, base64buffer = _utf7_ENCODE_CHAR(
result, oc, base64bits, base64buffer)
pos = rutf8.next_codepoint_pos(s, pos)
if base64bits:
result.append(_utf7_TO_BASE64(base64buffer << (6 - base64bits)))
if inShift:
result.append('-')
return result.build()
def decode_utf8sp(space, string):
# Surrogate-preserving utf-8 decoding. Assuming there is no
# encoding error, it should always be reversible, and the reverse is
# unused encode_utf8sp().
return str_decode_utf8(string, "string", True, decode_never_raise,
allow_surrogates=True)
# ____________________________________________________________
# utf-16
BYTEORDER = sys.byteorder
BYTEORDER2 = BYTEORDER[0] + 'e' # either "le" or "be"
assert BYTEORDER2 in ('le', 'be')
def str_decode_utf_16(s, errors, final=True,
errorhandler=None):
return str_decode_utf_16_helper(s, errors, final, errorhandler,
"native")[:3]
def str_decode_utf_16_be(s, errors, final=True,
errorhandler=None):
return str_decode_utf_16_helper(s, errors, final, errorhandler, "big",
'utf16-be')[:3]
def str_decode_utf_16_le(s, errors, final=True,
errorhandler=None):
return str_decode_utf_16_helper(s, errors, final, errorhandler, "little",
'utf16-le')[:3]
def str_decode_utf_16_helper(s, errors, final=True,
errorhandler=None,
byteorder="native",
public_encoding_name='utf16'):
bo = 0
if BYTEORDER == 'little':
ihi = 1
ilo = 0
else:
ihi = 0
ilo = 1
# Check for BOM marks (U+FEFF) in the input and adjust current
# byte order setting accordingly. In native mode, the leading BOM
# mark is skipped, in all other modes, it is copied to the output
# stream as-is (giving a ZWNBSP character).
pos = 0
if byteorder == 'native':
if len(s) >= 2:
bom = (ord(s[ihi]) << 8) | ord(s[ilo])
if BYTEORDER == 'little':
if bom == 0xFEFF:
pos += 2
bo = -1
elif bom == 0xFFFE:
pos += 2
bo = 1
else:
if bom == 0xFEFF:
pos += 2
bo = 1
elif bom == 0xFFFE:
pos += 2
bo = -1
elif byteorder == 'little':
bo = -1
else:
bo = 1
if len(s) == 0:
return '', 0, 0, bo
if bo == -1:
# force little endian
ihi = 1
ilo = 0
elif bo == 1:
# force big endian
ihi = 0
ilo = 1
result = StringBuilder(len(s) // 2)
while pos < len(s):
# remaining bytes at the end? (len(s) should be even)
if len(s) - pos < 2:
if not final:
break
r, pos, rettype, s = errorhandler(errors, public_encoding_name,
"truncated data",
s, pos, len(s))
result.append(r)
if len(s) - pos < 2:
break
ch = (ord(s[pos + ihi]) << 8) | ord(s[pos + ilo])
pos += 2
if ch < 0xD800 or ch > 0xDFFF:
rutf8.unichr_as_utf8_append(result, ch)
continue
# unexpected low surrogate
elif ch >= 0xDC00:
r, pos, rettype, s = errorhandler(errors, public_encoding_name,
"illegal encoding",
s, pos - 2, pos)
result.append(r)
continue
# UTF-16 code pair:
if len(s) - pos < 2:
pos -= 2
if not final:
break
errmsg = "unexpected end of data"
r, pos, rettype, s = errorhandler(errors, public_encoding_name,
errmsg, s, pos, len(s))
result.append(r)
else:
ch2 = (ord(s[pos+ihi]) << 8) | ord(s[pos+ilo])
pos += 2
if 0xDC00 <= ch2 <= 0xDFFF:
ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000
rutf8.unichr_as_utf8_append(result, ch)
continue
else:
r, pos, rettype, s = errorhandler(errors, public_encoding_name,
"illegal UTF-16 surrogate",
s, pos - 4, pos - 2)
result.append(r)
r = result.build()
lgt = rutf8.check_utf8(r, True)
return r, lgt, pos, bo
def _STORECHAR(result, CH, byteorder):
hi = chr(((CH) >> 8) & 0xff)
lo = chr((CH) & 0xff)
if byteorder == 'little':
result.append(lo)
result.append(hi)
else:
result.append(hi)
result.append(lo)
def utf8_encode_utf_16_helper(s, errors,
errorhandler=None,
allow_surrogates=True,
byteorder='little',
public_encoding_name='utf16'):
if len(s) == 0:
if byteorder == 'native':
result = StringBuilder(2)
_STORECHAR(result, 0xFEFF, BYTEORDER)
return result.build()
return ""
result = StringBuilder(len(s) * 2 + 2)
if byteorder == 'native':
_STORECHAR(result, 0xFEFF, BYTEORDER)
byteorder = BYTEORDER
pos = 0
index = 0
while pos < len(s):
cp = rutf8.codepoint_at_pos(s, pos)
if cp < 0xD800:
_STORECHAR(result, cp, byteorder)
elif cp >= 0x10000:
_STORECHAR(result, 0xD800 | ((cp-0x10000) >> 10), byteorder)
_STORECHAR(result, 0xDC00 | ((cp-0x10000) & 0x3FF), byteorder)
elif cp >= 0xE000 or allow_surrogates:
_STORECHAR(result, cp, byteorder)
else:
r, newindex, rettype, s = errorhandler(
errors, public_encoding_name, 'surrogates not allowed',
s, index, index+1)
if rettype == 'u':
for cp in rutf8.Utf8StringIterator(r):
if cp < 0xD800 or allow_surrogates:
_STORECHAR(result, cp, byteorder)
else:
errorhandler('strict', public_encoding_name,
'surrogates not allowed',
s, index, index+1)
else:
for ch in r:
cp = ord(ch)
if cp < 0xD800 or allow_surrogates:
_STORECHAR(result, cp, byteorder)
else:
errorhandler('strict', public_encoding_name,
'surrogates not allowed',
s, index, index+1)
if index != newindex: # Should be uncommon
index = newindex
pos = rutf8._pos_at_index(s, newindex)
continue
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
return result.build()
def utf8_encode_utf_16(s, errors,
errorhandler=None,
allow_surrogates=False):
return utf8_encode_utf_16_helper(s, errors, errorhandler,
allow_surrogates, "native",
'utf-16-' + BYTEORDER2)
def utf8_encode_utf_16_be(s, errors,
errorhandler=None,
allow_surrogates=False):
return utf8_encode_utf_16_helper(s, errors, errorhandler,
allow_surrogates, "big",
'utf-16-be')
def utf8_encode_utf_16_le(s, errors,
errorhandler=None,
allow_surrogates=False):
return utf8_encode_utf_16_helper(s, errors, errorhandler,
allow_surrogates, "little",
'utf-16-le')
# ____________________________________________________________
# utf-32
def str_decode_utf_32(s, errors, final=True,
errorhandler=None):
return str_decode_utf_32_helper(
s, errors, final, errorhandler, "native", 'utf-32-' + BYTEORDER2,
allow_surrogates=False)[:3]
def str_decode_utf_32_be(s, errors, final=True,
errorhandler=None):
return str_decode_utf_32_helper(
s, errors, final, errorhandler, "big", 'utf-32-be',
allow_surrogates=False)[:3]
def str_decode_utf_32_le(s, errors, final=True,
errorhandler=None):
return str_decode_utf_32_helper(
s, errors, final, errorhandler, "little", 'utf-32-le',
allow_surrogates=False)[:3]
BOM32_DIRECT = intmask(0x0000FEFF)
BOM32_REVERSE = intmask(0xFFFE0000)
def str_decode_utf_32_helper(s, errors, final,
errorhandler,
byteorder="native",
public_encoding_name='utf32',
allow_surrogates=True):
assert errorhandler is not None
bo = 0
if BYTEORDER == 'little':
iorder0, iorder1, iorder2, iorder3 = 0, 1, 2, 3
else:
iorder0, iorder1, iorder2, iorder3 = 3, 2, 1, 0
# Check for BOM marks (U+FEFF) in the input and adjust current
# byte order setting accordingly. In native mode, the leading BOM
# mark is skipped, in all other modes, it is copied to the output
# stream as-is (giving a ZWNBSP character).
pos = 0
if byteorder == 'native':
if len(s) >= 4:
bom = intmask(
(ord(s[iorder3]) << 24) | (ord(s[iorder2]) << 16) |
(ord(s[iorder1]) << 8) | ord(s[iorder0]))
if BYTEORDER == 'little':
if bom == BOM32_DIRECT:
pos += 4
bo = -1
elif bom == BOM32_REVERSE:
pos += 4
bo = 1
else:
if bom == BOM32_DIRECT:
pos += 4
bo = 1
elif bom == BOM32_REVERSE:
pos += 4
bo = -1
elif byteorder == 'little':
bo = -1
else:
bo = 1
if len(s) == 0:
return '', 0, 0, bo
if bo == -1:
# force little endian
iorder0, iorder1, iorder2, iorder3 = 0, 1, 2, 3
elif bo == 1:
# force big endian
iorder0, iorder1, iorder2, iorder3 = 3, 2, 1, 0
result = StringBuilder(len(s) // 4)
while pos < len(s):
# remaining bytes at the end? (len(s) should be divisible by 4)
if len(s) - pos < 4:
if not final:
break
r, pos, rettype, s = errorhandler(errors, public_encoding_name,
"truncated data",
s, pos, len(s))
result.append(r)
if len(s) - pos < 4:
break
continue
ch = ((ord(s[pos + iorder3]) << 24) | (ord(s[pos + iorder2]) << 16) |
(ord(s[pos + iorder1]) << 8) | ord(s[pos + iorder0]))
if not allow_surrogates and 0xD800 <= ch <= 0xDFFF:
r, pos, rettype, obj = errorhandler(errors, public_encoding_name,
"code point in surrogate code point "
"range(0xd800, 0xe000)",
s, pos, pos + 4)
result.append(r)
continue
elif r_uint(ch) >= 0x110000:
r, pos, rettype, s = errorhandler(errors, public_encoding_name,
"codepoint not in range(0x110000)",
s, pos, len(s))
result.append(r)
continue
rutf8.unichr_as_utf8_append(result, ch, allow_surrogates=allow_surrogates)
pos += 4
r = result.build()
lgt = rutf8.check_utf8(r, True)
return r, lgt, pos, bo
def _STORECHAR32(result, CH, byteorder):
c0 = chr(((CH) >> 24) & 0xff)
c1 = chr(((CH) >> 16) & 0xff)
c2 = chr(((CH) >> 8) & 0xff)
c3 = chr((CH) & 0xff)
if byteorder == 'little':
result.append(c3)
result.append(c2)
result.append(c1)
result.append(c0)
else:
result.append(c0)
result.append(c1)
result.append(c2)
result.append(c3)
def utf8_encode_utf_32_helper(s, errors,
errorhandler=None,
allow_surrogates=True,
byteorder='little',
public_encoding_name='utf32'):
# s is utf8
if len(s) == 0:
if byteorder == 'native':
result = StringBuilder(4)
_STORECHAR32(result, 0xFEFF, BYTEORDER)
return result.build()
return ""
result = StringBuilder(len(s) * 4 + 4)
if byteorder == 'native':
_STORECHAR32(result, 0xFEFF, BYTEORDER)
byteorder = BYTEORDER
pos = 0
index = 0
while pos < len(s):
ch = rutf8.codepoint_at_pos(s, pos)
if not allow_surrogates and 0xD800 <= ch < 0xE000:
r, newindex, rettype, obj = errorhandler(
errors, public_encoding_name, 'surrogates not allowed',
s, index, index+1)
if rettype == 'u':
for ch in rutf8.Utf8StringIterator(r):
if ch < 0xD800:
_STORECHAR32(result, ch, byteorder)
else:
errorhandler(
'strict', public_encoding_name, 'surrogates not allowed',
s, index, index+1)
else:
for ch in r:
cp = ord(ch)
if cp < 0xD800:
_STORECHAR32(result, cp, byteorder)
else:
errorhandler(
'strict', public_encoding_name, 'surrogates not allowed',
s, index, index+1)
s = obj
if index != newindex: # Should be uncommon
index = newindex
pos = rutf8._pos_at_index(s, newindex)
continue
pos = rutf8.next_codepoint_pos(s, pos)
_STORECHAR32(result, ch, byteorder)
index += 1
return result.build()
def utf8_encode_utf_32(s, errors,
errorhandler=None, allow_surrogates=True):
return utf8_encode_utf_32_helper(s, errors, errorhandler,
allow_surrogates, "native",
'utf-32-' + BYTEORDER2)
def utf8_encode_utf_32_be(s, errors,
errorhandler=None, allow_surrogates=True):
return utf8_encode_utf_32_helper(s, errors, errorhandler,
allow_surrogates, "big",
'utf-32-be')
def utf8_encode_utf_32_le(s, errors,
errorhandler=None, allow_surrogates=True):
return utf8_encode_utf_32_helper(s, errors, errorhandler,
allow_surrogates, "little",
'utf-32-le')
# ____________________________________________________________
# Charmap
ERROR_CHAR = u'\ufffe'.encode('utf8')
@specialize.argtype(4)
def str_decode_charmap(s, errors, final=False,
errorhandler=None, mapping=None):
"mapping can be a rpython dictionary, or a dict-like object."
# Default to Latin-1
if mapping is None:
return str_decode_latin_1(s, errors, final=final,
errorhandler=errorhandler)
if len(s) == 0:
return '', 0, 0
pos = 0
result = StringBuilder(len(s))
while pos < len(s):
ch = s[pos]
c = mapping.get(ord(ch), ERROR_CHAR)
if c == ERROR_CHAR:
r, pos, rettype, s = errorhandler(errors, "charmap",
"character maps to <undefined>",
s, pos, pos + 1)
result.append(r)
continue
result.append(c)
pos += 1
r = result.build()
lgt = rutf8.codepoints_in_utf8(r)
return r, lgt, pos
def utf8_encode_charmap(s, errors, errorhandler=None, mapping=None, allow_surrogates=False):
if mapping is None:
return utf8_encode_latin_1(s, errors, errorhandler=errorhandler)
if len(s) == 0:
return ''
result = StringBuilder(len(s))
pos = 0
index = 0
while pos < len(s):
ch = rutf8.codepoint_at_pos(s, pos)
c = mapping.get(ch, '')
if len(c) == 0:
# collect all unencodable chars.
startindex = index
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
while (pos < len(s) and
mapping.get(rutf8.codepoint_at_pos(s, pos), '') == ''):
pos = rutf8.next_codepoint_pos(s, pos)
index += 1
r, newindex, rettype, obj = errorhandler(errors, "charmap",
"character maps to <undefined>",
s, startindex, index)
if rettype == 'u':
for cp2 in rutf8.Utf8StringIterator(r):
ch2 = mapping.get(cp2, '')
if not ch2:
errorhandler(
"strict", "charmap", "character maps to <undefined>",
s, startindex, index)
result.append(ch2)
else:
for ch in r:
result.append(ch)
s = obj
if index != newindex: # Should be uncommon
index = newindex
pos = rutf8._pos_at_index(s, newindex)
continue
result.append(c)
index += 1
pos = rutf8.next_codepoint_pos(s, pos)
return result.build()
# ____________________________________________________________
# Decimal Encoder
def unicode_encode_decimal(s, errors, errorhandler=None, allow_surrogates=False):
"""Converts whitespace to ' ', decimal characters to their
corresponding ASCII digit and all other Latin-1 characters except
\0 as-is. Characters outside this range (Unicode ordinals 1-256)
are treated as errors. This includes embedded NULL bytes.
"""
if errorhandler is None:
errorhandler = default_error_encode
result = StringBuilder(len(s))
pos = 0
i = 0
it = rutf8.Utf8StringIterator(s)
for ch in it:
if unicodedb.isspace(ch):
result.append(' ')
i += 1
continue
try:
decimal = unicodedb.decimal(ch)
except KeyError:
pass
else:
result.append(chr(48 + decimal))
i += 1
continue
if 0 < ch < 256:
result.append(chr(ch))
i += 1
continue
# All other characters are considered unencodable
start_index = i
i += 1
while not it.done():
ch = rutf8.codepoint_at_pos(s, it.get_pos())
try:
if (0 < ch < 256 or unicodedb.isspace(ch) or
unicodedb.decimal(ch) >= 0):
break
except KeyError:
# not a decimal
pass
if it.done():
break
ch = next(it)
i += 1
end_index = i
msg = "invalid decimal Unicode string"
r, pos, retype, obj = errorhandler(
errors, 'decimal', msg, s, start_index, end_index)
for ch in rutf8.Utf8StringIterator(r):
if unicodedb.isspace(ch):
result.append(' ')
continue
try:
decimal = unicodedb.decimal(ch)
except KeyError:
pass
else:
result.append(chr(48 + decimal))
continue
if 0 < ch < 256:
result.append(chr(ch))
continue
errorhandler('strict', 'decimal', msg, s, start_index, end_index)
return result.build()
|