blob: 4d255207c4817b4ab232ca52b22dab66257fa9e0 [file] [log] [blame]
Christian Heimes1a6387e2008-03-26 12:49:49 +00001#include "Python.h"
2#include "bytes_methods.h"
3
Christian Heimes1a6387e2008-03-26 12:49:49 +00004PyDoc_STRVAR_shared(_Py_isspace__doc__,
5"B.isspace() -> bool\n\
6\n\
7Return True if all characters in B are whitespace\n\
8and there is at least one character in B, False otherwise.");
9
10PyObject*
11_Py_bytes_isspace(const char *cptr, Py_ssize_t len)
12{
13 register const unsigned char *p
14 = (unsigned char *) cptr;
15 register const unsigned char *e;
16
17 /* Shortcut for single character strings */
Eric Smithcac7af62009-04-27 19:04:37 +000018 if (len == 1 && Py_ISSPACE(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +000019 Py_RETURN_TRUE;
20
21 /* Special case for empty strings */
22 if (len == 0)
23 Py_RETURN_FALSE;
24
25 e = p + len;
26 for (; p < e; p++) {
Eric Smithcac7af62009-04-27 19:04:37 +000027 if (!Py_ISSPACE(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +000028 Py_RETURN_FALSE;
29 }
30 Py_RETURN_TRUE;
31}
32
33
34PyDoc_STRVAR_shared(_Py_isalpha__doc__,
35"B.isalpha() -> bool\n\
36\n\
37Return True if all characters in B are alphabetic\n\
38and there is at least one character in B, False otherwise.");
39
40PyObject*
41_Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
42{
43 register const unsigned char *p
44 = (unsigned char *) cptr;
45 register const unsigned char *e;
46
47 /* Shortcut for single character strings */
Eric Smithcac7af62009-04-27 19:04:37 +000048 if (len == 1 && Py_ISALPHA(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +000049 Py_RETURN_TRUE;
50
51 /* Special case for empty strings */
52 if (len == 0)
53 Py_RETURN_FALSE;
54
55 e = p + len;
56 for (; p < e; p++) {
Eric Smithcac7af62009-04-27 19:04:37 +000057 if (!Py_ISALPHA(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +000058 Py_RETURN_FALSE;
59 }
60 Py_RETURN_TRUE;
61}
62
63
64PyDoc_STRVAR_shared(_Py_isalnum__doc__,
65"B.isalnum() -> bool\n\
66\n\
67Return True if all characters in B are alphanumeric\n\
68and there is at least one character in B, False otherwise.");
69
70PyObject*
71_Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
72{
73 register const unsigned char *p
74 = (unsigned char *) cptr;
75 register const unsigned char *e;
76
77 /* Shortcut for single character strings */
Eric Smithcac7af62009-04-27 19:04:37 +000078 if (len == 1 && Py_ISALNUM(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +000079 Py_RETURN_TRUE;
80
81 /* Special case for empty strings */
82 if (len == 0)
83 Py_RETURN_FALSE;
84
85 e = p + len;
86 for (; p < e; p++) {
Eric Smithcac7af62009-04-27 19:04:37 +000087 if (!Py_ISALNUM(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +000088 Py_RETURN_FALSE;
89 }
90 Py_RETURN_TRUE;
91}
92
93
94PyDoc_STRVAR_shared(_Py_isdigit__doc__,
95"B.isdigit() -> bool\n\
96\n\
97Return True if all characters in B are digits\n\
98and there is at least one character in B, False otherwise.");
99
100PyObject*
101_Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
102{
103 register const unsigned char *p
104 = (unsigned char *) cptr;
105 register const unsigned char *e;
106
107 /* Shortcut for single character strings */
Eric Smithcac7af62009-04-27 19:04:37 +0000108 if (len == 1 && Py_ISDIGIT(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000109 Py_RETURN_TRUE;
110
111 /* Special case for empty strings */
112 if (len == 0)
113 Py_RETURN_FALSE;
114
115 e = p + len;
116 for (; p < e; p++) {
Eric Smithcac7af62009-04-27 19:04:37 +0000117 if (!Py_ISDIGIT(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000118 Py_RETURN_FALSE;
119 }
120 Py_RETURN_TRUE;
121}
122
123
124PyDoc_STRVAR_shared(_Py_islower__doc__,
125"B.islower() -> bool\n\
126\n\
127Return True if all cased characters in B are lowercase and there is\n\
128at least one cased character in B, False otherwise.");
129
130PyObject*
131_Py_bytes_islower(const char *cptr, Py_ssize_t len)
132{
133 register const unsigned char *p
134 = (unsigned char *) cptr;
135 register const unsigned char *e;
136 int cased;
137
138 /* Shortcut for single character strings */
139 if (len == 1)
Eric Smithcac7af62009-04-27 19:04:37 +0000140 return PyBool_FromLong(Py_ISLOWER(*p));
Christian Heimes1a6387e2008-03-26 12:49:49 +0000141
142 /* Special case for empty strings */
143 if (len == 0)
144 Py_RETURN_FALSE;
145
146 e = p + len;
147 cased = 0;
148 for (; p < e; p++) {
Eric Smithcac7af62009-04-27 19:04:37 +0000149 if (Py_ISUPPER(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000150 Py_RETURN_FALSE;
Eric Smithcac7af62009-04-27 19:04:37 +0000151 else if (!cased && Py_ISLOWER(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000152 cased = 1;
153 }
154 return PyBool_FromLong(cased);
155}
156
157
158PyDoc_STRVAR_shared(_Py_isupper__doc__,
159"B.isupper() -> bool\n\
160\n\
161Return True if all cased characters in B are uppercase and there is\n\
162at least one cased character in B, False otherwise.");
163
164PyObject*
165_Py_bytes_isupper(const char *cptr, Py_ssize_t len)
166{
167 register const unsigned char *p
168 = (unsigned char *) cptr;
169 register const unsigned char *e;
170 int cased;
171
172 /* Shortcut for single character strings */
173 if (len == 1)
Eric Smithcac7af62009-04-27 19:04:37 +0000174 return PyBool_FromLong(Py_ISUPPER(*p));
Christian Heimes1a6387e2008-03-26 12:49:49 +0000175
176 /* Special case for empty strings */
177 if (len == 0)
178 Py_RETURN_FALSE;
179
180 e = p + len;
181 cased = 0;
182 for (; p < e; p++) {
Eric Smithcac7af62009-04-27 19:04:37 +0000183 if (Py_ISLOWER(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000184 Py_RETURN_FALSE;
Eric Smithcac7af62009-04-27 19:04:37 +0000185 else if (!cased && Py_ISUPPER(*p))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000186 cased = 1;
187 }
188 return PyBool_FromLong(cased);
189}
190
191
192PyDoc_STRVAR_shared(_Py_istitle__doc__,
193"B.istitle() -> bool\n\
194\n\
195Return True if B is a titlecased string and there is at least one\n\
196character in B, i.e. uppercase characters may only follow uncased\n\
197characters and lowercase characters only cased ones. Return False\n\
198otherwise.");
199
200PyObject*
201_Py_bytes_istitle(const char *cptr, Py_ssize_t len)
202{
203 register const unsigned char *p
204 = (unsigned char *) cptr;
205 register const unsigned char *e;
206 int cased, previous_is_cased;
207
208 /* Shortcut for single character strings */
209 if (len == 1)
Eric Smithcac7af62009-04-27 19:04:37 +0000210 return PyBool_FromLong(Py_ISUPPER(*p));
Christian Heimes1a6387e2008-03-26 12:49:49 +0000211
212 /* Special case for empty strings */
213 if (len == 0)
214 Py_RETURN_FALSE;
215
216 e = p + len;
217 cased = 0;
218 previous_is_cased = 0;
219 for (; p < e; p++) {
220 register const unsigned char ch = *p;
221
Eric Smithcac7af62009-04-27 19:04:37 +0000222 if (Py_ISUPPER(ch)) {
Christian Heimes1a6387e2008-03-26 12:49:49 +0000223 if (previous_is_cased)
224 Py_RETURN_FALSE;
225 previous_is_cased = 1;
226 cased = 1;
227 }
Eric Smithcac7af62009-04-27 19:04:37 +0000228 else if (Py_ISLOWER(ch)) {
Christian Heimes1a6387e2008-03-26 12:49:49 +0000229 if (!previous_is_cased)
230 Py_RETURN_FALSE;
231 previous_is_cased = 1;
232 cased = 1;
233 }
234 else
235 previous_is_cased = 0;
236 }
237 return PyBool_FromLong(cased);
238}
239
240
241PyDoc_STRVAR_shared(_Py_lower__doc__,
242"B.lower() -> copy of B\n\
243\n\
244Return a copy of B with all ASCII characters converted to lowercase.");
245
246void
247_Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
248{
249 Py_ssize_t i;
250
251 /*
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000252 newobj = PyString_FromStringAndSize(NULL, len);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000253 if (!newobj)
254 return NULL;
255
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000256 s = PyString_AS_STRING(newobj);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000257 */
258
259 Py_MEMCPY(result, cptr, len);
260
261 for (i = 0; i < len; i++) {
262 int c = Py_CHARMASK(result[i]);
Eric Smithcac7af62009-04-27 19:04:37 +0000263 if (Py_ISUPPER(c))
264 result[i] = Py_TOLOWER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000265 }
266}
267
268
269PyDoc_STRVAR_shared(_Py_upper__doc__,
270"B.upper() -> copy of B\n\
271\n\
272Return a copy of B with all ASCII characters converted to uppercase.");
273
274void
275_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
276{
277 Py_ssize_t i;
278
279 /*
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000280 newobj = PyString_FromStringAndSize(NULL, len);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000281 if (!newobj)
282 return NULL;
283
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000284 s = PyString_AS_STRING(newobj);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000285 */
286
287 Py_MEMCPY(result, cptr, len);
288
289 for (i = 0; i < len; i++) {
290 int c = Py_CHARMASK(result[i]);
Eric Smithcac7af62009-04-27 19:04:37 +0000291 if (Py_ISLOWER(c))
292 result[i] = Py_TOUPPER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000293 }
294}
295
296
297PyDoc_STRVAR_shared(_Py_title__doc__,
298"B.title() -> copy of B\n\
299\n\
300Return a titlecased version of B, i.e. ASCII words start with uppercase\n\
301characters, all remaining cased characters have lowercase.");
302
303void
304_Py_bytes_title(char *result, char *s, Py_ssize_t len)
305{
306 Py_ssize_t i;
307 int previous_is_cased = 0;
308
309 /*
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000310 newobj = PyString_FromStringAndSize(NULL, len);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000311 if (newobj == NULL)
312 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000313 s_new = PyString_AsString(newobj);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000314 */
315 for (i = 0; i < len; i++) {
316 int c = Py_CHARMASK(*s++);
Eric Smithcac7af62009-04-27 19:04:37 +0000317 if (Py_ISLOWER(c)) {
Christian Heimes1a6387e2008-03-26 12:49:49 +0000318 if (!previous_is_cased)
Eric Smithcac7af62009-04-27 19:04:37 +0000319 c = Py_TOUPPER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000320 previous_is_cased = 1;
Eric Smithcac7af62009-04-27 19:04:37 +0000321 } else if (Py_ISUPPER(c)) {
Christian Heimes1a6387e2008-03-26 12:49:49 +0000322 if (previous_is_cased)
Eric Smithcac7af62009-04-27 19:04:37 +0000323 c = Py_TOLOWER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000324 previous_is_cased = 1;
325 } else
326 previous_is_cased = 0;
327 *result++ = c;
328 }
329}
330
331
332PyDoc_STRVAR_shared(_Py_capitalize__doc__,
333"B.capitalize() -> copy of B\n\
334\n\
335Return a copy of B with only its first character capitalized (ASCII).");
336
337void
338_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
339{
340 Py_ssize_t i;
341
342 /*
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000343 newobj = PyString_FromStringAndSize(NULL, len);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000344 if (newobj == NULL)
345 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000346 s_new = PyString_AsString(newobj);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000347 */
348 if (0 < len) {
349 int c = Py_CHARMASK(*s++);
Eric Smithcac7af62009-04-27 19:04:37 +0000350 if (Py_ISLOWER(c))
351 *result = Py_TOUPPER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000352 else
353 *result = c;
354 result++;
355 }
356 for (i = 1; i < len; i++) {
357 int c = Py_CHARMASK(*s++);
Eric Smithcac7af62009-04-27 19:04:37 +0000358 if (Py_ISUPPER(c))
359 *result = Py_TOLOWER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000360 else
361 *result = c;
362 result++;
363 }
364}
365
366
367PyDoc_STRVAR_shared(_Py_swapcase__doc__,
368"B.swapcase() -> copy of B\n\
369\n\
370Return a copy of B with uppercase ASCII characters converted\n\
371to lowercase ASCII and vice versa.");
372
373void
374_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
375{
376 Py_ssize_t i;
377
378 /*
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000379 newobj = PyString_FromStringAndSize(NULL, len);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000380 if (newobj == NULL)
381 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000382 s_new = PyString_AsString(newobj);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000383 */
384 for (i = 0; i < len; i++) {
385 int c = Py_CHARMASK(*s++);
Eric Smithcac7af62009-04-27 19:04:37 +0000386 if (Py_ISLOWER(c)) {
387 *result = Py_TOUPPER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000388 }
Eric Smithcac7af62009-04-27 19:04:37 +0000389 else if (Py_ISUPPER(c)) {
390 *result = Py_TOLOWER(c);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000391 }
392 else
393 *result = c;
394 result++;
395 }
396}
397