annotate Objects/stringlib/codecs.h @ 79770:2efcaec45697 3.3

Merge for issue #15744: add tests for the writelines() method of file objects.
author Antoine Pitrou <solipsis@pitrou.net>
date Tue, 16 Oct 2012 23:05:01 +0200
parents 99112b851b25
children 5962f192a483 d2127cdec10e
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 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
6 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
7 #if (SIZEOF_LONG == 8)
77966
ab9e824116eb Use correct types for ASCII_CHAR_MASK integer constants.
Mark Dickinson <mdickinson@enthought.com>
parents: 77649
diff changeset
8 # define ASCII_CHAR_MASK 0x8080808080808080UL
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
9 #elif (SIZEOF_LONG == 4)
77966
ab9e824116eb Use correct types for ASCII_CHAR_MASK integer constants.
Mark Dickinson <mdickinson@enthought.com>
parents: 77649
diff changeset
10 # define ASCII_CHAR_MASK 0x80808080UL
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
11 #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
12 # 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
13 #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
14
77649
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
15 /* 10xxxxxx */
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
16 #define IS_CONTINUATION_BYTE(ch) ((ch) >= 0x80 && (ch) < 0xC0)
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
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;
79065
99112b851b25 Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77966
diff changeset
25 const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG);
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 */
79065
99112b851b25 Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77966
diff changeset
39 if (_Py_IS_ALIGNED(s, SIZEOF_LONG)) {
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
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];
77649
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
110 if (!IS_CONTINUATION_BYTE(ch2))
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
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];
77649
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
134 if (!IS_CONTINUATION_BYTE(ch2) ||
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
135 !IS_CONTINUATION_BYTE(ch3)) {
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
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];
77649
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
175 if (!IS_CONTINUATION_BYTE(ch2) ||
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
176 !IS_CONTINUATION_BYTE(ch3) ||
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
177 !IS_CONTINUATION_BYTE(ch4)) {
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
77649
3214c9ebcf5e Issue #14923: Optimize continuation-byte check in UTF-8 decoding. Patch by Serhiy Storchaka.
Mark Dickinson <mdickinson@enthought.com>
parents: 77448
diff changeset
219 #undef IS_CONTINUATION_BYTE
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
220
74032
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
221
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
222 /* 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
231 #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
232
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
233 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
234 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
235 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
236 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
237 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
238 #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
239 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
240 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
241 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
242 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
243 #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
244 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
245 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
246 #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
247 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
248 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
249 #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
250 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
251 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
252 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
253
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
254 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
255
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
256 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
257 /* 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
258 * 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
259 * 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
260 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
261 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
262 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
263 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
264 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
265 else {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
266 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
267 /* 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
268 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
269 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
270 /* 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
271 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
272 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
273 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
274 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
275 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
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
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
278 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
279 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
280
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
281 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
282 /* 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
283 *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
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 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
286 else
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 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
288 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
289 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
290 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
291 /* 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
292 *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
293 *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
294 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
295 #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
296 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
297 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
298 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
299 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
300 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
301 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
302 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
303 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
304 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
305
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
306 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
307 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
308 else
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
309 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
310
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
311 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
312 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
313
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
314 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
315 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
316 else
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
317 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
318
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
319 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
320 /* 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
321 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
322 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
323 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
324 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
325 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
326 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
327 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
328 } else {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
329 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
330 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
331 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
332 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
333 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
334 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
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
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
337 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
338 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
339 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
340 *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
341 } 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
342 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
343 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
344
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
345 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
346 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
347 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
348 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
349
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
350 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
351 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
352 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
353 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
354 unicode,
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
355 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
356 "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
357 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
358 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
359 *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
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 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
362 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
363 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
364 else
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 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
366 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
367 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
368 {
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)(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
370 *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
371 *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
372 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
373 #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
374 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
375 {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
376 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
377 /* 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
378 *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
379 *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
380 *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
381 *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
382 }
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 > 2 */
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
384 #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
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
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
387 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
388 /* 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
389 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
390 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
391 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
392 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
393 else {
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
394 /* 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
395 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
396 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
397 _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
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
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
400 #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
401 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
402 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
403 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
404 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
405
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
406 #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
407 error:
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(rep);
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(errorHandler);
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(exc);
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
411 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
412 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
413 #endif
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
414
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
415 #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
416 }
fbd797fc3809 Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73668
diff changeset
417
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
418 /* 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
419 #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
420 # 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
421 #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
422 # 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
423 #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
424 # 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
425 #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
426
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 /* 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
428 #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
429 /* 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
430 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
431 # 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
432 #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
433 /* 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
434 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
435 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
436 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
437 */
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 # 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
439 #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
440 /* 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
441 #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
442 /* 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
443 #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
444 (((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
445
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 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
447 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
448 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
449 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
450 {
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 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
452 const unsigned char *aligned_end =
79065
99112b851b25 Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77966
diff changeset
453 (const unsigned char *) _Py_ALIGN_DOWN(e, SIZEOF_LONG);
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
454 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
455 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
456 /* 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
457 #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
458 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
459 #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
460 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
461 #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
462 --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
463
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 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
465 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
466 /* 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
467 reads are more expensive, better to defer to another iteration. */
79065
99112b851b25 Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77966
diff changeset
468 if (_Py_IS_ALIGNED(q, SIZEOF_LONG)) {
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
469 /* 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
470 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
471 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
472 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
473 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
474 /* 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
475 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
476 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
477 }
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 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
479 /* 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
480 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
481 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
482 #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
483 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
484 #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
485 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
486 #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
487 }
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 #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
489 # 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
490 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
491 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
492 # 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
493 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
494 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
495 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
496 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
497 # 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
498 #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
499 # 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
500 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
501 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
502 # 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
503 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
504 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
505 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
506 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
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 #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
509 _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
510 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
511 }
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 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
513 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
514 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
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
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 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
518 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
519 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
520 #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
521 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
522 /* 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
523 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
524 #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
525 *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
526 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
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
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 /* 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
530 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
531 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
532 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
533 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
534 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
535 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
536 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
537 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
538 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
539 #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
540 /* 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
541 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
542 #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
543 *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
544 #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
545 }
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 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
547 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
548 *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
549 *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
550 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
551 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
552 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
553 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
554 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
555 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
556 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
557 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
558 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
559 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
560 }
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 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
562 #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
563 #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
564 #undef SWAB
77448
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
565
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
566
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
567 Py_LOCAL_INLINE(void)
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
568 STRINGLIB(utf16_encode)(unsigned short *out,
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
569 const STRINGLIB_CHAR *in,
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
570 Py_ssize_t len,
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
571 int native_ordering)
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
572 {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
573 const STRINGLIB_CHAR *end = in + len;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
574 #if STRINGLIB_SIZEOF_CHAR == 1
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
575 # define SWAB2(CH) ((CH) << 8)
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
576 #else
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
577 # define SWAB2(CH) (((CH) << 8) | ((CH) >> 8))
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
578 #endif
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
579 #if STRINGLIB_MAX_CHAR < 0x10000
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
580 if (native_ordering) {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
581 # if STRINGLIB_SIZEOF_CHAR == 2
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
582 Py_MEMCPY(out, in, 2 * len);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
583 # else
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
584 _PyUnicode_CONVERT_BYTES(STRINGLIB_CHAR, unsigned short, in, end, out);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
585 # endif
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
586 } else {
79065
99112b851b25 Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77966
diff changeset
587 const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
77448
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
588 while (in < unrolled_end) {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
589 out[0] = SWAB2(in[0]);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
590 out[1] = SWAB2(in[1]);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
591 out[2] = SWAB2(in[2]);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
592 out[3] = SWAB2(in[3]);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
593 in += 4; out += 4;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
594 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
595 while (in < end) {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
596 *out++ = SWAB2(*in);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
597 ++in;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
598 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
599 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
600 #else
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
601 if (native_ordering) {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
602 while (in < end) {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
603 Py_UCS4 ch = *in++;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
604 if (ch < 0x10000)
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
605 *out++ = ch;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
606 else {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
607 out[0] = Py_UNICODE_HIGH_SURROGATE(ch);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
608 out[1] = Py_UNICODE_LOW_SURROGATE(ch);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
609 out += 2;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
610 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
611 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
612 } else {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
613 while (in < end) {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
614 Py_UCS4 ch = *in++;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
615 if (ch < 0x10000)
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
616 *out++ = SWAB2((Py_UCS2)ch);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
617 else {
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
618 Py_UCS2 ch1 = Py_UNICODE_HIGH_SURROGATE(ch);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
619 Py_UCS2 ch2 = Py_UNICODE_LOW_SURROGATE(ch);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
620 out[0] = SWAB2(ch1);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
621 out[1] = SWAB2(ch2);
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
622 out += 2;
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
623 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
624 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
625 }
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
626 #endif
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
627 #undef SWAB2
acca141fda80 Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76971
diff changeset
628 }
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
629 #endif /* STRINGLIB_IS_UNICODE */