Mercurial > cpython-lru_cache-weakref
annotate Objects/stringlib/codecs.h @ 79063:2bdc8c8ea42e
Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Thu, 20 Sep 2012 12:42:54 +0200 |
| parents | ab9e824116eb |
| children | 99112b851b25 |
| rev | line source |
|---|---|
|
73668
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
1 /* stringlib: codec implementations */ |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
2 |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
3 #if STRINGLIB_IS_UNICODE |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
4 |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
5 /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
6 #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
7 |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
8 /* Mask to quickly check whether a C 'long' contains a |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
9 non-ASCII, UTF8-encoded char. */ |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
10 #if (SIZEOF_LONG == 8) |
|
77966
ab9e824116eb
Use correct types for ASCII_CHAR_MASK integer constants.
Mark Dickinson <mdickinson@enthought.com>
parents:
77649
diff
changeset
|
11 # 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
|
12 #elif (SIZEOF_LONG == 4) |
|
77966
ab9e824116eb
Use correct types for ASCII_CHAR_MASK integer constants.
Mark Dickinson <mdickinson@enthought.com>
parents:
77649
diff
changeset
|
13 # 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
|
14 #else |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
15 # error C 'long' size should be either 4 or 8! |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
16 #endif |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
17 |
|
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
|
18 /* 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
|
19 #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
|
20 |
|
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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 { |
|
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 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
|
27 const char *s = *inptr; |
|
73668
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
28 const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK); |
|
76862
e08c3791f035
Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74032
diff
changeset
|
29 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
|
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 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
|
32 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
|
33 |
|
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 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
|
35 /* 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 */ |
|
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 if (!((size_t) s & LONG_PTR_MASK)) { |
|
8e6c4acaf530
Issue #13417: speed up utf-8 decoding by around 2x for the non-fully-ASCII case.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff
changeset
|
43 /* 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
|
44 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
|
45 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
|
46 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
|
47 /* 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 #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
|
54 _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
|
55 _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
|
56 _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
|
57 _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
|
58 # 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
|
59 _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
|
60 _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
|
61 _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
|
62 _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
|
63 # 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
|
64 #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
|
65 # 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
|
66 _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
|
67 _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
|
68 _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
|
69 _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
|
70 _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
|
71 _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
|
72 _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
|
73 _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
|
74 # 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
|
75 _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
|
76 _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
|
77 _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
|
78 _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
|
79 # 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
|
80 #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
|
81 _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
|
82 _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
|
83 } |
|
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 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 } |
|
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
|
90 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
|
91 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
|
92 *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
|
93 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
|
94 } |
|
e08c3791f035
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 } |
|
e08c3791f035
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 |
|
e08c3791f035
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 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
|
98 /* 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
|
99 \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
|
100 \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
|
101 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
|
102 } |
|
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
|
103 |
|
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
|
104 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
|
105 /* \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
|
106 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
|
107 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
|
108 /* 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
|
109 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
|
110 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
|
111 } |
|
e08c3791f035
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 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
|
113 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
|
114 /* 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
|
115 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
|
116 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
|
117 ((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
|
118 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
|
119 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
|
120 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
|
121 (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
|
122 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
|
123 *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
|
124 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
|
125 } |
|
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
|
126 |
|
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
|
127 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
|
128 /* \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
|
129 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
|
130 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
|
131 /* 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
|
132 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
|
133 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
|
134 } |
|
e08c3791f035
Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74032
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 !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
|
139 /* 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
|
140 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
|
141 } |
|
e08c3791f035
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 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
|
143 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
|
144 /* 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
|
145 \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
|
146 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
|
147 } |
|
e08c3791f035
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 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
|
149 /* 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
|
150 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
|
151 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
|
152 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
|
153 (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
|
154 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
|
155 } |
|
e08c3791f035
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 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
|
157 ((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
|
158 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
|
159 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
|
160 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
|
161 (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
|
162 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
|
163 *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
|
164 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
|
165 } |
|
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
|
166 |
|
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
|
167 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
|
168 /* \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
|
169 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
|
170 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
|
171 /* 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
|
172 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
|
173 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
|
174 } |
|
e08c3791f035
Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy Storchaka.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74032
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 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
|
179 !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
|
180 !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
|
181 /* 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
|
182 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
|
183 } |
|
e08c3791f035
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 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
|
185 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
|
186 /* 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
|
187 \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
|
188 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
|
189 } |
|
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
|
190 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
|
191 /* 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
|
192 \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
|
193 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
|
194 } |
|
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
|
195 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
|
196 ((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
|
197 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
|
198 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
|
199 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
|
200 (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
|
201 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
|
202 *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
|
203 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
|
204 } |
|
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
|
205 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
|
206 } |
|
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
|
207 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
|
208 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
|
209 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
|
210 *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
|
211 *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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 } |
|
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 |
|
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
|
221 #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
|
222 #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
|
223 |
|
74032
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
224 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
225 /* 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
234 #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
|
235 |
|
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 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 #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
|
242 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
|
243 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
|
244 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
|
245 #endif |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
246 #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
|
247 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
|
248 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
|
249 #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
|
250 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
|
251 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
|
252 #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
|
253 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
|
254 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
|
255 #endif |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
256 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
257 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
|
258 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
259 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
|
260 /* 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
|
261 * 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
|
262 * 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
|
263 */ |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
268 else { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
269 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
|
270 /* 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
|
271 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
|
272 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
273 /* 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 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
|
279 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
280 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
281 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
|
282 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
|
283 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
284 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
|
285 /* 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
|
286 *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
|
287 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
288 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
289 else |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
290 #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
|
291 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
|
292 #endif |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
293 { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
294 /* 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
|
295 *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
|
296 *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
|
297 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
298 #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
|
299 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
|
300 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
|
301 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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
308 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
309 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
|
310 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
|
311 else |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
312 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
|
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 (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
|
315 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
|
316 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
317 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
|
318 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
|
319 else |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
320 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
|
321 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
322 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
|
323 /* 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
|
324 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
|
325 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
|
326 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
327 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
|
328 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
|
329 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
|
330 goto error; |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
331 } else { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
332 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
|
333 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
|
334 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
|
335 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
|
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 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
|
338 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
339 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
340 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
|
341 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
|
342 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
|
343 *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
|
344 } 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
|
345 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
|
346 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
|
347 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
348 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
|
349 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
|
350 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
|
351 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
|
352 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
353 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
|
354 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
|
355 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
|
356 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
|
357 unicode, |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
358 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
|
359 "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
|
360 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
|
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 *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
|
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 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
365 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
|
366 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
367 else |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
368 #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
|
369 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
|
370 #endif |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
371 { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
372 *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
|
373 *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
|
374 *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
|
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 #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
|
377 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
|
378 { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
379 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
|
380 /* 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
|
381 *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
|
382 *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
|
383 *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
|
384 *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
|
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 #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
|
387 #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
|
388 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
389 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
390 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
|
391 /* 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
|
392 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
|
393 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
|
394 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
|
395 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
396 else { |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
397 /* 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
|
398 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
|
399 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
|
400 _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
|
401 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
402 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
403 #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
|
404 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
|
405 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
|
406 #endif |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
407 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
|
408 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
409 #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
|
410 error: |
|
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(rep); |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
412 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
|
413 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
|
414 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
|
415 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
|
416 #endif |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
417 |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
418 #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
|
419 } |
|
fbd797fc3809
Issue #13624: Write a specialized UTF-8 encoder to allow more optimization
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73668
diff
changeset
|
420 |
|
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
|
421 /* 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
|
422 #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
|
423 # 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
|
424 #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
|
425 # 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
|
426 #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
|
427 # 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
|
428 #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
|
429 |
|
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 /* 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
|
431 #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
|
432 /* 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
|
433 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
|
434 # 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
|
435 #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
|
436 /* 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
|
437 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
|
438 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
|
439 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
|
440 */ |
|
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 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
|
442 #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
|
443 /* 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
|
444 #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
|
445 /* 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
|
446 #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
|
447 (((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
|
448 |
|
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 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
|
450 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
|
451 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
|
452 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
|
453 { |
|
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 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
|
455 const unsigned char *aligned_end = |
|
cdcc816dea85
Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76862
diff
changeset
|
456 (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
|
cdcc816dea85
Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76862
diff
changeset
|
457 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
|
458 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
|
459 /* 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
|
460 #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
|
461 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
|
462 #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
|
463 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
|
464 #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
|
465 --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
|
466 |
|
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 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
|
468 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
|
469 /* 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
|
470 reads are more expensive, better to defer to another iteration. */ |
|
cdcc816dea85
Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76862
diff
changeset
|
471 if (!((size_t) q & LONG_PTR_MASK)) { |
|
cdcc816dea85
Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76862
diff
changeset
|
472 /* 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
|
473 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
|
474 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
|
475 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
|
476 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
|
477 /* 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
|
478 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
|
479 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
|
480 } |
|
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 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
|
482 /* 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
|
483 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
|
484 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
|
485 #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
|
486 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
|
487 #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
|
488 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
|
489 #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
|
490 } |
|
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 #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
|
492 # 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
|
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); |
|
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 # 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
|
496 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
|
497 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
|
498 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
|
499 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
|
500 # 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
|
501 #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
|
502 # 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
|
503 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
|
504 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
|
505 # 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
|
506 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
|
507 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
|
508 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
|
509 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
|
510 # 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
|
511 #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
|
512 _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
|
513 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
|
514 } |
|
cdcc816dea85
Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76862
diff
changeset
|
515 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
|
516 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
|
517 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
|
518 } |
|
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 |
|
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 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
|
521 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
|
522 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
|
523 #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
|
524 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
|
525 /* 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
|
526 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
|
527 #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
|
528 *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
|
529 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
|
530 } |
|
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 |
|
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 /* 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
|
533 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 #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
|
543 /* 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
|
544 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
|
545 #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
|
546 *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
|
547 #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
|
548 } |
|
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 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
|
550 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
|
551 *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
|
552 *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
|
553 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
|
554 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
|
555 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
|
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 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
|
558 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
|
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 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
|
561 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
|
562 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
|
563 } |
|
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 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
|
565 #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
|
566 #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
|
567 #undef SWAB |
|
cdcc816dea85
Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76862
diff
changeset
|
568 #undef LONG_PTR_MASK |
|
77448
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
569 |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
570 |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
571 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 { |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
577 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
|
578 #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
|
579 # 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
|
580 #else |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
581 # 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
|
582 #endif |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
583 #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
|
584 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
|
585 # 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
|
586 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
|
587 # else |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
588 _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
|
589 # endif |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
590 } else { |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
591 const STRINGLIB_CHAR *unrolled_end = in + (len & ~ (Py_ssize_t) 3); |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
592 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
|
593 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
|
594 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
|
595 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
|
596 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
|
597 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
|
598 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
599 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
|
600 *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
|
601 ++in; |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
602 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
603 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
604 #else |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
605 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
|
606 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
|
607 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
|
608 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
|
609 *out++ = ch; |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
610 else { |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
611 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
|
612 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
|
613 out += 2; |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
614 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
615 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
616 } else { |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
617 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
|
618 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
|
619 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
|
620 *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
|
621 else { |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
622 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
|
623 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
|
624 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
|
625 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
|
626 out += 2; |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
627 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
628 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
629 } |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
630 #endif |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
631 #undef SWAB2 |
|
acca141fda80
Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
Antoine Pitrou <solipsis@pitrou.net>
parents:
76971
diff
changeset
|
632 } |
|
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
|
633 #endif /* STRINGLIB_IS_UNICODE */ |
