@@ -92,57 +92,6 @@ _Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
92
92
}
93
93
94
94
95
- PyDoc_STRVAR_shared (_Py_isascii__doc__ ,
96
- "B.isascii() -> bool\n\
97
- \n\
98
- Return True if B is empty or all characters in B are ASCII,\n\
99
- False otherwise." );
100
-
101
- // Optimization is copied from ascii_decode in unicodeobject.c
102
- /* Mask to quickly check whether a C 'size_t' contains a
103
- non-ASCII, UTF8-encoded char. */
104
- #if (SIZEOF_SIZE_T == 8 )
105
- # define ASCII_CHAR_MASK 0x8080808080808080ULL
106
- #elif (SIZEOF_SIZE_T == 4 )
107
- # define ASCII_CHAR_MASK 0x80808080U
108
- #else
109
- # error C 'size_t' size should be either 4 or 8!
110
- #endif
111
-
112
- PyObject *
113
- _Py_bytes_isascii (const char * cptr , Py_ssize_t len )
114
- {
115
- const char * p = cptr ;
116
- const char * end = p + len ;
117
-
118
- while (p < end ) {
119
- /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h
120
- for an explanation. */
121
- if (_Py_IS_ALIGNED (p , ALIGNOF_SIZE_T )) {
122
- /* Help allocation */
123
- const char * _p = p ;
124
- while (_p + SIZEOF_SIZE_T <= end ) {
125
- size_t value = * (const size_t * ) _p ;
126
- if (value & ASCII_CHAR_MASK ) {
127
- Py_RETURN_FALSE ;
128
- }
129
- _p += SIZEOF_SIZE_T ;
130
- }
131
- p = _p ;
132
- if (_p == end )
133
- break ;
134
- }
135
- if ((unsigned char )* p & 0x80 ) {
136
- Py_RETURN_FALSE ;
137
- }
138
- p ++ ;
139
- }
140
- Py_RETURN_TRUE ;
141
- }
142
-
143
- #undef ASCII_CHAR_MASK
144
-
145
-
146
95
PyDoc_STRVAR_shared (_Py_isdigit__doc__ ,
147
96
"B.isdigit() -> bool\n\
148
97
\n\
@@ -438,6 +387,7 @@ _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to)
438
387
#include "stringlib/fastsearch.h"
439
388
#include "stringlib/count.h"
440
389
#include "stringlib/find.h"
390
+ #include "stringlib/find_max_char.h"
441
391
442
392
/*
443
393
Wraps stringlib_parse_args_finds() and additionally checks the first
@@ -765,3 +715,21 @@ _Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *subobj,
765
715
{
766
716
return _Py_bytes_tailmatch (str , len , "endswith" , subobj , start , end , +1 );
767
717
}
718
+
719
+ PyDoc_STRVAR_shared (_Py_isascii__doc__ ,
720
+ "B.isascii() -> bool\n\
721
+ \n\
722
+ Return True if B is empty or all characters in B are ASCII,\n\
723
+ False otherwise." );
724
+
725
+ PyObject *
726
+ _Py_bytes_isascii (const char * cptr , Py_ssize_t len )
727
+ {
728
+ const char * p = cptr ;
729
+ const char * end = p + len ;
730
+ Py_ssize_t max_char = stringlib_find_max_char (cptr , end );
731
+ if (max_char > 127 ) {
732
+ Py_RETURN_FALSE ;
733
+ }
734
+ Py_RETURN_TRUE ;
735
+ }
0 commit comments