2 #include "bytes_methods.h"
4 PyDoc_STRVAR_shared(_Py_isspace__doc__
,
5 "B.isspace() -> bool\n\
7 Return True if all characters in B are whitespace\n\
8 and there is at least one character in B, False otherwise.");
11 _Py_bytes_isspace(const char *cptr
, Py_ssize_t len
)
13 register const unsigned char *p
14 = (unsigned char *) cptr
;
15 register const unsigned char *e
;
17 /* Shortcut for single character strings */
18 if (len
== 1 && Py_ISSPACE(*p
))
21 /* Special case for empty strings */
34 PyDoc_STRVAR_shared(_Py_isalpha__doc__
,
35 "B.isalpha() -> bool\n\
37 Return True if all characters in B are alphabetic\n\
38 and there is at least one character in B, False otherwise.");
41 _Py_bytes_isalpha(const char *cptr
, Py_ssize_t len
)
43 register const unsigned char *p
44 = (unsigned char *) cptr
;
45 register const unsigned char *e
;
47 /* Shortcut for single character strings */
48 if (len
== 1 && Py_ISALPHA(*p
))
51 /* Special case for empty strings */
64 PyDoc_STRVAR_shared(_Py_isalnum__doc__
,
65 "B.isalnum() -> bool\n\
67 Return True if all characters in B are alphanumeric\n\
68 and there is at least one character in B, False otherwise.");
71 _Py_bytes_isalnum(const char *cptr
, Py_ssize_t len
)
73 register const unsigned char *p
74 = (unsigned char *) cptr
;
75 register const unsigned char *e
;
77 /* Shortcut for single character strings */
78 if (len
== 1 && Py_ISALNUM(*p
))
81 /* Special case for empty strings */
94 PyDoc_STRVAR_shared(_Py_isdigit__doc__
,
95 "B.isdigit() -> bool\n\
97 Return True if all characters in B are digits\n\
98 and there is at least one character in B, False otherwise.");
101 _Py_bytes_isdigit(const char *cptr
, Py_ssize_t len
)
103 register const unsigned char *p
104 = (unsigned char *) cptr
;
105 register const unsigned char *e
;
107 /* Shortcut for single character strings */
108 if (len
== 1 && Py_ISDIGIT(*p
))
111 /* Special case for empty strings */
124 PyDoc_STRVAR_shared(_Py_islower__doc__
,
125 "B.islower() -> bool\n\
127 Return True if all cased characters in B are lowercase and there is\n\
128 at least one cased character in B, False otherwise.");
131 _Py_bytes_islower(const char *cptr
, Py_ssize_t len
)
133 register const unsigned char *p
134 = (unsigned char *) cptr
;
135 register const unsigned char *e
;
138 /* Shortcut for single character strings */
140 return PyBool_FromLong(Py_ISLOWER(*p
));
142 /* Special case for empty strings */
151 else if (!cased
&& Py_ISLOWER(*p
))
154 return PyBool_FromLong(cased
);
158 PyDoc_STRVAR_shared(_Py_isupper__doc__
,
159 "B.isupper() -> bool\n\
161 Return True if all cased characters in B are uppercase and there is\n\
162 at least one cased character in B, False otherwise.");
165 _Py_bytes_isupper(const char *cptr
, Py_ssize_t len
)
167 register const unsigned char *p
168 = (unsigned char *) cptr
;
169 register const unsigned char *e
;
172 /* Shortcut for single character strings */
174 return PyBool_FromLong(Py_ISUPPER(*p
));
176 /* Special case for empty strings */
185 else if (!cased
&& Py_ISUPPER(*p
))
188 return PyBool_FromLong(cased
);
192 PyDoc_STRVAR_shared(_Py_istitle__doc__
,
193 "B.istitle() -> bool\n\
195 Return True if B is a titlecased string and there is at least one\n\
196 character in B, i.e. uppercase characters may only follow uncased\n\
197 characters and lowercase characters only cased ones. Return False\n\
201 _Py_bytes_istitle(const char *cptr
, Py_ssize_t len
)
203 register const unsigned char *p
204 = (unsigned char *) cptr
;
205 register const unsigned char *e
;
206 int cased
, previous_is_cased
;
208 /* Shortcut for single character strings */
210 return PyBool_FromLong(Py_ISUPPER(*p
));
212 /* Special case for empty strings */
218 previous_is_cased
= 0;
220 register const unsigned char ch
= *p
;
222 if (Py_ISUPPER(ch
)) {
223 if (previous_is_cased
)
225 previous_is_cased
= 1;
228 else if (Py_ISLOWER(ch
)) {
229 if (!previous_is_cased
)
231 previous_is_cased
= 1;
235 previous_is_cased
= 0;
237 return PyBool_FromLong(cased
);
241 PyDoc_STRVAR_shared(_Py_lower__doc__
,
242 "B.lower() -> copy of B\n\
244 Return a copy of B with all ASCII characters converted to lowercase.");
247 _Py_bytes_lower(char *result
, const char *cptr
, Py_ssize_t len
)
252 newobj = PyString_FromStringAndSize(NULL, len);
256 s = PyString_AS_STRING(newobj);
259 Py_MEMCPY(result
, cptr
, len
);
261 for (i
= 0; i
< len
; i
++) {
262 int c
= Py_CHARMASK(result
[i
]);
264 result
[i
] = Py_TOLOWER(c
);
269 PyDoc_STRVAR_shared(_Py_upper__doc__
,
270 "B.upper() -> copy of B\n\
272 Return a copy of B with all ASCII characters converted to uppercase.");
275 _Py_bytes_upper(char *result
, const char *cptr
, Py_ssize_t len
)
280 newobj = PyString_FromStringAndSize(NULL, len);
284 s = PyString_AS_STRING(newobj);
287 Py_MEMCPY(result
, cptr
, len
);
289 for (i
= 0; i
< len
; i
++) {
290 int c
= Py_CHARMASK(result
[i
]);
292 result
[i
] = Py_TOUPPER(c
);
297 PyDoc_STRVAR_shared(_Py_title__doc__
,
298 "B.title() -> copy of B\n\
300 Return a titlecased version of B, i.e. ASCII words start with uppercase\n\
301 characters, all remaining cased characters have lowercase.");
304 _Py_bytes_title(char *result
, char *s
, Py_ssize_t len
)
307 int previous_is_cased
= 0;
310 newobj = PyString_FromStringAndSize(NULL, len);
313 s_new = PyString_AsString(newobj);
315 for (i
= 0; i
< len
; i
++) {
316 int c
= Py_CHARMASK(*s
++);
318 if (!previous_is_cased
)
320 previous_is_cased
= 1;
321 } else if (Py_ISUPPER(c
)) {
322 if (previous_is_cased
)
324 previous_is_cased
= 1;
326 previous_is_cased
= 0;
332 PyDoc_STRVAR_shared(_Py_capitalize__doc__
,
333 "B.capitalize() -> copy of B\n\
335 Return a copy of B with only its first character capitalized (ASCII).");
338 _Py_bytes_capitalize(char *result
, char *s
, Py_ssize_t len
)
343 newobj = PyString_FromStringAndSize(NULL, len);
346 s_new = PyString_AsString(newobj);
349 int c
= Py_CHARMASK(*s
++);
351 *result
= Py_TOUPPER(c
);
356 for (i
= 1; i
< len
; i
++) {
357 int c
= Py_CHARMASK(*s
++);
359 *result
= Py_TOLOWER(c
);
367 PyDoc_STRVAR_shared(_Py_swapcase__doc__
,
368 "B.swapcase() -> copy of B\n\
370 Return a copy of B with uppercase ASCII characters converted\n\
371 to lowercase ASCII and vice versa.");
374 _Py_bytes_swapcase(char *result
, char *s
, Py_ssize_t len
)
379 newobj = PyString_FromStringAndSize(NULL, len);
382 s_new = PyString_AsString(newobj);
384 for (i
= 0; i
< len
; i
++) {
385 int c
= Py_CHARMASK(*s
++);
387 *result
= Py_TOUPPER(c
);
389 else if (Py_ISUPPER(c
)) {
390 *result
= Py_TOLOWER(c
);