annotate Objects/stringlib/codecs.h @ 77115:352147bbefdb

#14862: Add missing names to os.__all__
author Petri Lehtinen <petri@digip.org>
date Wed, 23 May 2012 21:36:16 +0300
parents cdcc816dea85
children acca141fda80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1 /* stringlib: codec implementations */
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
3 #if STRINGLIB_IS_UNICODE
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
4
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
5 /* Mask to check or force alignment of a pointer to C 'long' boundaries */
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
6 #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1)
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
7
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
8 /* Mask to quickly check whether a C 'long' contains a
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
9 non-ASCII, UTF8-encoded char. */
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
10 #if (SIZEOF_LONG == 8)
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
11 # define ASCII_CHAR_MASK 0x8080808080808080L
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
12 #elif (SIZEOF_LONG == 4)
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
13 # define ASCII_CHAR_MASK 0x80808080L
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
14 #else
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
15 # error C 'long' size should be either 4 or 8!
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
16 #endif
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
17
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
18 Py_LOCAL_INLINE(Py_UCS4)
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
19 STRINGLIB(utf8_decode)(const char **inptr, const char *end,
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
20 STRINGLIB_CHAR *dest,
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
21 Py_ssize_t *outpos)
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
22 {
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
23 Py_UCS4 ch;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
24 const char *s = *inptr;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
25 const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK);
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
26 STRINGLIB_CHAR *p = dest + *outpos;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
27
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
28 while (s < end) {
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
29 ch = (unsigned char)*s;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
30
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
31 if (ch < 0x80) {
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
32 /* Fast path for runs of ASCII characters. Given that common UTF-8
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
33 input will consist of an overwhelming majority of ASCII
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
34 characters, we try to optimize for this case by checking
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
35 as many characters as a C 'long' can contain.
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
36 First, check if we can do an aligned read, as most CPUs have
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
37 a penalty for unaligned reads.
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
38 */
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
39 if (!((size_t) s & LONG_PTR_MASK)) {
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
40 /* Help register allocation */
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
41 register const char *_s = s;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
42 register STRINGLIB_CHAR *_p = p;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
43 while (_s < aligned_end) {
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
44 /* Read a whole long at a time (either 4 or 8 bytes),
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
45 and do a fast unrolled copy if it only contains ASCII
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
46 characters. */
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
47 unsigned long value = *(unsigned long *) _s;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
48 if (value & ASCII_CHAR_MASK)
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
49 break;
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
50 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
51 _p[0] = (STRINGLIB_CHAR)(value & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
52 _p[1] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
53 _p[2] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
54 _p[3] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
55 # if SIZEOF_LONG == 8
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
56 _p[4] = (STRINGLIB_CHAR)((value >> 32) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
57 _p[5] = (STRINGLIB_CHAR)((value >> 40) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
58 _p[6] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
59 _p[7] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
60 # endif
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
61 #else
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
62 # if SIZEOF_LONG == 8
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
63 _p[0] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
64 _p[1] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
65 _p[2] = (STRINGLIB_CHAR)((value >> 40) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
66 _p[3] = (STRINGLIB_CHAR)((value >> 32) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
67 _p[4] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
68 _p[5] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
69 _p[6] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
70 _p[7] = (STRINGLIB_CHAR)(value & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
71 # else
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
72 _p[0] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
73 _p[1] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
74 _p[2] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
75 _p[3] = (STRINGLIB_CHAR)(value & 0xFFu);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
76 # endif
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
77 #endif
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
78 _s += SIZEOF_LONG;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
79 _p += SIZEOF_LONG;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
80 }
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
81 s = _s;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
82 p = _p;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
83 if (s == end)
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
84 break;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
85 ch = (unsigned char)*s;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
86 }
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
87 if (ch < 0x80) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
88 s++;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
89 *p++ = ch;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
90 continue;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
91 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
92 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
93
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
94 if (ch < 0xC2) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
95 /* invalid sequence
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
96 \x80-\xBF -- continuation byte
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
97 \xC0-\xC1 -- fake 0000-007F */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
98 goto InvalidStart;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
99 }
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
100
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
101 if (ch < 0xE0) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
102 /* \xC2\x80-\xDF\xBF -- 0080-07FF */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
103 Py_UCS4 ch2;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
104 if (end - s < 2) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
105 /* unexpected end of data: the caller will decide whether
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
106 it's an error or not */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
107 break;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
108 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
109 ch2 = (unsigned char)s[1];
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
110 if ((ch2 & 0xC0) != 0x80)
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
111 /* invalid continuation byte */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
112 goto InvalidContinuation;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
113 ch = (ch << 6) + ch2 -
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
114 ((0xC0 << 6) + 0x80);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
115 assert ((ch > 0x007F) && (ch <= 0x07FF));
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
116 s += 2;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
117 if (STRINGLIB_MAX_CHAR <= 0x007F ||
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
118 (STRINGLIB_MAX_CHAR < 0x07FF && ch > STRINGLIB_MAX_CHAR))
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
119 goto Overflow;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
120 *p++ = ch;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
121 continue;
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
122 }
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
123
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
124 if (ch < 0xF0) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
125 /* \xE0\xA0\x80-\xEF\xBF\xBF -- 0800-FFFF */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
126 Py_UCS4 ch2, ch3;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
127 if (end - s < 3) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
128 /* unexpected end of data: the caller will decide whether
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
129 it's an error or not */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
130 break;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
131 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
132 ch2 = (unsigned char)s[1];
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
133 ch3 = (unsigned char)s[2];
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
134 if ((ch2 & 0xC0) != 0x80 ||
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
135 (ch3 & 0xC0) != 0x80) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
136 /* invalid continuation byte */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
137 goto InvalidContinuation;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
138 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
139 if (ch == 0xE0) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
140 if (ch2 < 0xA0)
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
141 /* invalid sequence
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
142 \xE0\x80\x80-\xE0\x9F\xBF -- fake 0000-0800 */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
143 goto InvalidContinuation;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
144 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
145 else if (ch == 0xED && ch2 > 0x9F) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
146 /* Decoding UTF-8 sequences in range \xED\xA0\x80-\xED\xBF\xBF
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
147 will result in surrogates in range D800-DFFF. Surrogates are
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
148 not valid UTF-8 so they are rejected.
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
149 See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
150 (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
151 goto InvalidContinuation;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
152 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
153 ch = (ch << 12) + (ch2 << 6) + ch3 -
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
154 ((0xE0 << 12) + (0x80 << 6) + 0x80);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
155 assert ((ch > 0x07FF) && (ch <= 0xFFFF));
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
156 s += 3;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
157 if (STRINGLIB_MAX_CHAR <= 0x07FF ||
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
158 (STRINGLIB_MAX_CHAR < 0xFFFF && ch > STRINGLIB_MAX_CHAR))
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
159 goto Overflow;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
160 *p++ = ch;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
161 continue;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
162 }
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
163
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
164 if (ch < 0xF5) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
165 /* \xF0\x90\x80\x80-\xF4\x8F\xBF\xBF -- 10000-10FFFF */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
166 Py_UCS4 ch2, ch3, ch4;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
167 if (end - s < 4) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
168 /* unexpected end of data: the caller will decide whether
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
169 it's an error or not */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
170 break;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
171 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
172 ch2 = (unsigned char)s[1];
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
173 ch3 = (unsigned char)s[2];
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
174 ch4 = (unsigned char)s[3];
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
175 if ((ch2 & 0xC0) != 0x80 ||
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
176 (ch3 & 0xC0) != 0x80 ||
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
177 (ch4 & 0xC0) != 0x80) {
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
178 /* invalid continuation byte */
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
179 goto InvalidContinuation;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
180 }
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
181 if (ch == 0xF0) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
182 if (ch2 < 0x90)
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
183 /* invalid sequence
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
184 \xF0\x80\x80\x80-\xF0\x80\xBF\xBF -- fake 0000-FFFF */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
185 goto InvalidContinuation;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
186 }
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
187 else if (ch == 0xF4 && ch2 > 0x8F) {
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
188 /* invalid sequence
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
189 \xF4\x90\x80\80- -- 110000- overflow */
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
190 goto InvalidContinuation;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
191 }
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
192 ch = (ch << 18) + (ch2 << 12) + (ch3 << 6) + ch4 -
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
193 ((0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80);
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
194 assert ((ch > 0xFFFF) && (ch <= 0x10FFFF));
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
195 s += 4;
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
196 if (STRINGLIB_MAX_CHAR <= 0xFFFF ||
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
197 (STRINGLIB_MAX_CHAR < 0x10FFFF && ch > STRINGLIB_MAX_CHAR))
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
198 goto Overflow;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
199 *p++ = ch;
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
200 continue;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
201 }
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
202 goto InvalidStart;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
203 }
76862
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
204 ch = 0;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
205 Overflow:
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
206 Return:
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
207 *inptr = s;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
208 *outpos = p - dest;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
209 return ch;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
210 InvalidStart:
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
211 ch = 1;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
212 goto Return;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
213 InvalidContinuation:
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
214 ch = 2;
e08c3791f035 Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74032
diff changeset
215 goto Return;
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
216 }
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
217
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
218 #undef ASCII_CHAR_MASK
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
219
74032
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
220
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
221 /* UTF-8 encoder specialized for a Unicode kind to avoid the slow
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
222 PyUnicode_READ() macro. Delete some parts of the code depending on the kind:
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
223 UCS-1 strings don't need to handle surrogates for example. */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
224 Py_LOCAL_INLINE(PyObject *)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
225 STRINGLIB(utf8_encoder)(PyObject *unicode,
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
226 STRINGLIB_CHAR *data,
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
227 Py_ssize_t size,
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
228 const char *errors)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
229 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
230 #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
231
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
232 Py_ssize_t i; /* index into s of next input byte */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
233 PyObject *result; /* result string object */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
234 char *p; /* next free byte in output buffer */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
235 Py_ssize_t nallocated; /* number of result bytes allocated */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
236 Py_ssize_t nneeded; /* number of result bytes needed */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
237 #if STRINGLIB_SIZEOF_CHAR > 1
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
238 PyObject *errorHandler = NULL;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
239 PyObject *exc = NULL;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
240 PyObject *rep = NULL;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
241 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
242 #if STRINGLIB_SIZEOF_CHAR == 1
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
243 const Py_ssize_t max_char_size = 2;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
244 char stackbuf[MAX_SHORT_UNICHARS * 2];
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
245 #elif STRINGLIB_SIZEOF_CHAR == 2
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
246 const Py_ssize_t max_char_size = 3;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
247 char stackbuf[MAX_SHORT_UNICHARS * 3];
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
248 #else /* STRINGLIB_SIZEOF_CHAR == 4 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
249 const Py_ssize_t max_char_size = 4;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
250 char stackbuf[MAX_SHORT_UNICHARS * 4];
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
251 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
252
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
253 assert(size >= 0);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
254
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
255 if (size <= MAX_SHORT_UNICHARS) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
256 /* Write into the stack buffer; nallocated can't overflow.
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
257 * At the end, we'll allocate exactly as much heap space as it
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
258 * turns out we need.
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
259 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
260 nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
261 result = NULL; /* will allocate after we're done */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
262 p = stackbuf;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
263 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
264 else {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
265 if (size > PY_SSIZE_T_MAX / max_char_size) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
266 /* integer overflow */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
267 return PyErr_NoMemory();
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
268 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
269 /* Overallocate on the heap, and give the excess back at the end. */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
270 nallocated = size * max_char_size;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
271 result = PyBytes_FromStringAndSize(NULL, nallocated);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
272 if (result == NULL)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
273 return NULL;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
274 p = PyBytes_AS_STRING(result);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
275 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
276
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
277 for (i = 0; i < size;) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
278 Py_UCS4 ch = data[i++];
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
279
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
280 if (ch < 0x80) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
281 /* Encode ASCII */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
282 *p++ = (char) ch;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
283
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
284 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
285 else
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
286 #if STRINGLIB_SIZEOF_CHAR > 1
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
287 if (ch < 0x0800)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
288 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
289 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
290 /* Encode Latin-1 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
291 *p++ = (char)(0xc0 | (ch >> 6));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
292 *p++ = (char)(0x80 | (ch & 0x3f));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
293 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
294 #if STRINGLIB_SIZEOF_CHAR > 1
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
295 else if (Py_UNICODE_IS_SURROGATE(ch)) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
296 Py_ssize_t newpos;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
297 Py_ssize_t repsize, k, startpos;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
298 startpos = i-1;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
299 rep = unicode_encode_call_errorhandler(
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
300 errors, &errorHandler, "utf-8", "surrogates not allowed",
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
301 unicode, &exc, startpos, startpos+1, &newpos);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
302 if (!rep)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
303 goto error;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
304
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
305 if (PyBytes_Check(rep))
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
306 repsize = PyBytes_GET_SIZE(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
307 else
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
308 repsize = PyUnicode_GET_LENGTH(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
309
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
310 if (repsize > max_char_size) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
311 Py_ssize_t offset;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
312
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
313 if (result == NULL)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
314 offset = p - stackbuf;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
315 else
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
316 offset = p - PyBytes_AS_STRING(result);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
317
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
318 if (nallocated > PY_SSIZE_T_MAX - repsize + max_char_size) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
319 /* integer overflow */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
320 PyErr_NoMemory();
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
321 goto error;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
322 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
323 nallocated += repsize - max_char_size;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
324 if (result != NULL) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
325 if (_PyBytes_Resize(&result, nallocated) < 0)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
326 goto error;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
327 } else {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
328 result = PyBytes_FromStringAndSize(NULL, nallocated);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
329 if (result == NULL)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
330 goto error;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
331 Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
332 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
333 p = PyBytes_AS_STRING(result) + offset;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
334 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
335
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
336 if (PyBytes_Check(rep)) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
337 char *prep = PyBytes_AS_STRING(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
338 for(k = repsize; k > 0; k--)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
339 *p++ = *prep++;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
340 } else /* rep is unicode */ {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
341 enum PyUnicode_Kind repkind;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
342 void *repdata;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
343
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
344 if (PyUnicode_READY(rep) < 0)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
345 goto error;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
346 repkind = PyUnicode_KIND(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
347 repdata = PyUnicode_DATA(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
348
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
349 for(k=0; k<repsize; k++) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
350 Py_UCS4 c = PyUnicode_READ(repkind, repdata, k);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
351 if (0x80 <= c) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
352 raise_encode_exception(&exc, "utf-8",
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
353 unicode,
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
354 i-1, i,
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
355 "surrogates not allowed");
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
356 goto error;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
357 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
358 *p++ = (char)c;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
359 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
360 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
361 Py_CLEAR(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
362 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
363 else
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
364 #if STRINGLIB_SIZEOF_CHAR > 2
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
365 if (ch < 0x10000)
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
366 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
367 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
368 *p++ = (char)(0xe0 | (ch >> 12));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
369 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
370 *p++ = (char)(0x80 | (ch & 0x3f));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
371 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
372 #if STRINGLIB_SIZEOF_CHAR > 2
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
373 else /* ch >= 0x10000 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
374 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
375 assert(ch <= MAX_UNICODE);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
376 /* Encode UCS4 Unicode ordinals */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
377 *p++ = (char)(0xf0 | (ch >> 18));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
378 *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
379 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
380 *p++ = (char)(0x80 | (ch & 0x3f));
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
381 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
382 #endif /* STRINGLIB_SIZEOF_CHAR > 2 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
383 #endif /* STRINGLIB_SIZEOF_CHAR > 1 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
384 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
385
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
386 if (result == NULL) {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
387 /* This was stack allocated. */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
388 nneeded = p - stackbuf;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
389 assert(nneeded <= nallocated);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
390 result = PyBytes_FromStringAndSize(stackbuf, nneeded);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
391 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
392 else {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
393 /* Cut back to size actually needed. */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
394 nneeded = p - PyBytes_AS_STRING(result);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
395 assert(nneeded <= nallocated);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
396 _PyBytes_Resize(&result, nneeded);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
397 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
398
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
399 #if STRINGLIB_SIZEOF_CHAR > 1
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
400 Py_XDECREF(errorHandler);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
401 Py_XDECREF(exc);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
402 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
403 return result;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
404
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
405 #if STRINGLIB_SIZEOF_CHAR > 1
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
406 error:
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
407 Py_XDECREF(rep);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
408 Py_XDECREF(errorHandler);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
409 Py_XDECREF(exc);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
410 Py_XDECREF(result);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
411 return NULL;
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
412 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
413
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
414 #undef MAX_SHORT_UNICHARS
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
415 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
416
76971
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
417 /* The pattern for constructing UCS2-repeated masks. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
418 #if SIZEOF_LONG == 8
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
419 # define UCS2_REPEAT_MASK 0x0001000100010001ul
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
420 #elif SIZEOF_LONG == 4
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
421 # define UCS2_REPEAT_MASK 0x00010001ul
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
422 #else
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
423 # error C 'long' size should be either 4 or 8!
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
424 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
425
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
426 /* The mask for fast checking. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
427 #if STRINGLIB_SIZEOF_CHAR == 1
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
428 /* The mask for fast checking of whether a C 'long' contains a
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
429 non-ASCII or non-Latin1 UTF16-encoded characters. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
430 # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * (0xFFFFu & ~STRINGLIB_MAX_CHAR))
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
431 #else
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
432 /* The mask for fast checking of whether a C 'long' may contain
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
433 UTF16-encoded surrogate characters. This is an efficient heuristic,
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
434 assuming that non-surrogate characters with a code point >= 0x8000 are
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
435 rare in most input.
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
436 */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
437 # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * 0x8000u)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
438 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
439 /* The mask for fast byte-swapping. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
440 #define STRIPPED_MASK (UCS2_REPEAT_MASK * 0x00FFu)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
441 /* Swap bytes. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
442 #define SWAB(value) ((((value) >> 8) & STRIPPED_MASK) | \
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
443 (((value) & STRIPPED_MASK) << 8))
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
444
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
445 Py_LOCAL_INLINE(Py_UCS4)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
446 STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
447 STRINGLIB_CHAR *dest, Py_ssize_t *outpos,
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
448 int native_ordering)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
449 {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
450 Py_UCS4 ch;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
451 const unsigned char *aligned_end =
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
452 (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
453 const unsigned char *q = *inptr;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
454 STRINGLIB_CHAR *p = dest + *outpos;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
455 /* Offsets from q for retrieving byte pairs in the right order. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
456 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
457 int ihi = !!native_ordering, ilo = !native_ordering;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
458 #else
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
459 int ihi = !native_ordering, ilo = !!native_ordering;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
460 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
461 --e;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
462
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
463 while (q < e) {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
464 Py_UCS4 ch2;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
465 /* First check for possible aligned read of a C 'long'. Unaligned
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
466 reads are more expensive, better to defer to another iteration. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
467 if (!((size_t) q & LONG_PTR_MASK)) {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
468 /* Fast path for runs of in-range non-surrogate chars. */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
469 register const unsigned char *_q = q;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
470 while (_q < aligned_end) {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
471 unsigned long block = * (unsigned long *) _q;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
472 if (native_ordering) {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
473 /* Can use buffer directly */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
474 if (block & FAST_CHAR_MASK)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
475 break;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
476 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
477 else {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
478 /* Need to byte-swap */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
479 if (block & SWAB(FAST_CHAR_MASK))
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
480 break;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
481 #if STRINGLIB_SIZEOF_CHAR == 1
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
482 block >>= 8;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
483 #else
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
484 block = SWAB(block);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
485 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
486 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
487 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
488 # if SIZEOF_LONG == 4
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
489 p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
490 p[1] = (STRINGLIB_CHAR)(block >> 16);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
491 # elif SIZEOF_LONG == 8
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
492 p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
493 p[1] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
494 p[2] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
495 p[3] = (STRINGLIB_CHAR)(block >> 48);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
496 # endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
497 #else
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
498 # if SIZEOF_LONG == 4
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
499 p[0] = (STRINGLIB_CHAR)(block >> 16);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
500 p[1] = (STRINGLIB_CHAR)(block & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
501 # elif SIZEOF_LONG == 8
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
502 p[0] = (STRINGLIB_CHAR)(block >> 48);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
503 p[1] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
504 p[2] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
505 p[3] = (STRINGLIB_CHAR)(block & 0xFFFFu);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
506 # endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
507 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
508 _q += SIZEOF_LONG;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
509 p += SIZEOF_LONG / 2;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
510 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
511 q = _q;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
512 if (q >= e)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
513 break;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
514 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
515
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
516 ch = (q[ihi] << 8) | q[ilo];
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
517 q += 2;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
518 if (!Py_UNICODE_IS_SURROGATE(ch)) {
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
519 #if STRINGLIB_SIZEOF_CHAR < 2
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
520 if (ch > STRINGLIB_MAX_CHAR)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
521 /* Out-of-range */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
522 goto Return;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
523 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
524 *p++ = (STRINGLIB_CHAR)ch;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
525 continue;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
526 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
527
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
528 /* UTF-16 code pair: */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
529 if (q >= e)
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
530 goto UnexpectedEnd;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
531 if (!Py_UNICODE_IS_HIGH_SURROGATE(ch))
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
532 goto IllegalEncoding;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
533 ch2 = (q[ihi] << 8) | q[ilo];
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
534 q += 2;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
535 if (!Py_UNICODE_IS_LOW_SURROGATE(ch2))
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
536 goto IllegalSurrogate;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
537 ch = Py_UNICODE_JOIN_SURROGATES(ch, ch2);
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
538 #if STRINGLIB_SIZEOF_CHAR < 4
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
539 /* Out-of-range */
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
540 goto Return;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
541 #else
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
542 *p++ = (STRINGLIB_CHAR)ch;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
543 #endif
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
544 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
545 ch = 0;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
546 Return:
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
547 *inptr = q;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
548 *outpos = p - dest;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
549 return ch;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
550 UnexpectedEnd:
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
551 ch = 1;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
552 goto Return;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
553 IllegalEncoding:
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
554 ch = 2;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
555 goto Return;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
556 IllegalSurrogate:
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
557 ch = 3;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
558 goto Return;
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
559 }
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
560 #undef UCS2_REPEAT_MASK
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
561 #undef FAST_CHAR_MASK
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
562 #undef STRIPPED_MASK
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
563 #undef SWAB
cdcc816dea85 Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76862
diff changeset
564 #undef LONG_PTR_MASK
73668
8e6c4acaf530 Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
565 #endif /* STRINGLIB_IS_UNICODE */