PageRenderTime 38ms CodeModel.GetById 10ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Objects/bytes_methods.c

http://unladen-swallow.googlecode.com/
C | 610 lines | 490 code | 73 blank | 47 comment | 74 complexity | 7661c091501f5cf621d57e6f458b47e3 MD5 | raw file
  1#include "Python.h"
  2#include "bytes_methods.h"
  3
  4/* Our own locale-independent ctype.h-like macros */
  5
  6const unsigned int _Py_ctype_table[256] = {
  7    0, /* 0x0 '\x00' */
  8    0, /* 0x1 '\x01' */
  9    0, /* 0x2 '\x02' */
 10    0, /* 0x3 '\x03' */
 11    0, /* 0x4 '\x04' */
 12    0, /* 0x5 '\x05' */
 13    0, /* 0x6 '\x06' */
 14    0, /* 0x7 '\x07' */
 15    0, /* 0x8 '\x08' */
 16    FLAG_SPACE, /* 0x9 '\t' */
 17    FLAG_SPACE, /* 0xa '\n' */
 18    FLAG_SPACE, /* 0xb '\v' */
 19    FLAG_SPACE, /* 0xc '\f' */
 20    FLAG_SPACE, /* 0xd '\r' */
 21    0, /* 0xe '\x0e' */
 22    0, /* 0xf '\x0f' */
 23    0, /* 0x10 '\x10' */
 24    0, /* 0x11 '\x11' */
 25    0, /* 0x12 '\x12' */
 26    0, /* 0x13 '\x13' */
 27    0, /* 0x14 '\x14' */
 28    0, /* 0x15 '\x15' */
 29    0, /* 0x16 '\x16' */
 30    0, /* 0x17 '\x17' */
 31    0, /* 0x18 '\x18' */
 32    0, /* 0x19 '\x19' */
 33    0, /* 0x1a '\x1a' */
 34    0, /* 0x1b '\x1b' */
 35    0, /* 0x1c '\x1c' */
 36    0, /* 0x1d '\x1d' */
 37    0, /* 0x1e '\x1e' */
 38    0, /* 0x1f '\x1f' */
 39    FLAG_SPACE, /* 0x20 ' ' */
 40    0, /* 0x21 '!' */
 41    0, /* 0x22 '"' */
 42    0, /* 0x23 '#' */
 43    0, /* 0x24 '$' */
 44    0, /* 0x25 '%' */
 45    0, /* 0x26 '&' */
 46    0, /* 0x27 "'" */
 47    0, /* 0x28 '(' */
 48    0, /* 0x29 ')' */
 49    0, /* 0x2a '*' */
 50    0, /* 0x2b '+' */
 51    0, /* 0x2c ',' */
 52    0, /* 0x2d '-' */
 53    0, /* 0x2e '.' */
 54    0, /* 0x2f '/' */
 55    FLAG_DIGIT|FLAG_XDIGIT, /* 0x30 '0' */
 56    FLAG_DIGIT|FLAG_XDIGIT, /* 0x31 '1' */
 57    FLAG_DIGIT|FLAG_XDIGIT, /* 0x32 '2' */
 58    FLAG_DIGIT|FLAG_XDIGIT, /* 0x33 '3' */
 59    FLAG_DIGIT|FLAG_XDIGIT, /* 0x34 '4' */
 60    FLAG_DIGIT|FLAG_XDIGIT, /* 0x35 '5' */
 61    FLAG_DIGIT|FLAG_XDIGIT, /* 0x36 '6' */
 62    FLAG_DIGIT|FLAG_XDIGIT, /* 0x37 '7' */
 63    FLAG_DIGIT|FLAG_XDIGIT, /* 0x38 '8' */
 64    FLAG_DIGIT|FLAG_XDIGIT, /* 0x39 '9' */
 65    0, /* 0x3a ':' */
 66    0, /* 0x3b ';' */
 67    0, /* 0x3c '<' */
 68    0, /* 0x3d '=' */
 69    0, /* 0x3e '>' */
 70    0, /* 0x3f '?' */
 71    0, /* 0x40 '@' */
 72    FLAG_UPPER|FLAG_XDIGIT, /* 0x41 'A' */
 73    FLAG_UPPER|FLAG_XDIGIT, /* 0x42 'B' */
 74    FLAG_UPPER|FLAG_XDIGIT, /* 0x43 'C' */
 75    FLAG_UPPER|FLAG_XDIGIT, /* 0x44 'D' */
 76    FLAG_UPPER|FLAG_XDIGIT, /* 0x45 'E' */
 77    FLAG_UPPER|FLAG_XDIGIT, /* 0x46 'F' */
 78    FLAG_UPPER, /* 0x47 'G' */
 79    FLAG_UPPER, /* 0x48 'H' */
 80    FLAG_UPPER, /* 0x49 'I' */
 81    FLAG_UPPER, /* 0x4a 'J' */
 82    FLAG_UPPER, /* 0x4b 'K' */
 83    FLAG_UPPER, /* 0x4c 'L' */
 84    FLAG_UPPER, /* 0x4d 'M' */
 85    FLAG_UPPER, /* 0x4e 'N' */
 86    FLAG_UPPER, /* 0x4f 'O' */
 87    FLAG_UPPER, /* 0x50 'P' */
 88    FLAG_UPPER, /* 0x51 'Q' */
 89    FLAG_UPPER, /* 0x52 'R' */
 90    FLAG_UPPER, /* 0x53 'S' */
 91    FLAG_UPPER, /* 0x54 'T' */
 92    FLAG_UPPER, /* 0x55 'U' */
 93    FLAG_UPPER, /* 0x56 'V' */
 94    FLAG_UPPER, /* 0x57 'W' */
 95    FLAG_UPPER, /* 0x58 'X' */
 96    FLAG_UPPER, /* 0x59 'Y' */
 97    FLAG_UPPER, /* 0x5a 'Z' */
 98    0, /* 0x5b '[' */
 99    0, /* 0x5c '\\' */
100    0, /* 0x5d ']' */
101    0, /* 0x5e '^' */
102    0, /* 0x5f '_' */
103    0, /* 0x60 '`' */
104    FLAG_LOWER|FLAG_XDIGIT, /* 0x61 'a' */
105    FLAG_LOWER|FLAG_XDIGIT, /* 0x62 'b' */
106    FLAG_LOWER|FLAG_XDIGIT, /* 0x63 'c' */
107    FLAG_LOWER|FLAG_XDIGIT, /* 0x64 'd' */
108    FLAG_LOWER|FLAG_XDIGIT, /* 0x65 'e' */
109    FLAG_LOWER|FLAG_XDIGIT, /* 0x66 'f' */
110    FLAG_LOWER, /* 0x67 'g' */
111    FLAG_LOWER, /* 0x68 'h' */
112    FLAG_LOWER, /* 0x69 'i' */
113    FLAG_LOWER, /* 0x6a 'j' */
114    FLAG_LOWER, /* 0x6b 'k' */
115    FLAG_LOWER, /* 0x6c 'l' */
116    FLAG_LOWER, /* 0x6d 'm' */
117    FLAG_LOWER, /* 0x6e 'n' */
118    FLAG_LOWER, /* 0x6f 'o' */
119    FLAG_LOWER, /* 0x70 'p' */
120    FLAG_LOWER, /* 0x71 'q' */
121    FLAG_LOWER, /* 0x72 'r' */
122    FLAG_LOWER, /* 0x73 's' */
123    FLAG_LOWER, /* 0x74 't' */
124    FLAG_LOWER, /* 0x75 'u' */
125    FLAG_LOWER, /* 0x76 'v' */
126    FLAG_LOWER, /* 0x77 'w' */
127    FLAG_LOWER, /* 0x78 'x' */
128    FLAG_LOWER, /* 0x79 'y' */
129    FLAG_LOWER, /* 0x7a 'z' */
130    0, /* 0x7b '{' */
131    0, /* 0x7c '|' */
132    0, /* 0x7d '}' */
133    0, /* 0x7e '~' */
134    0, /* 0x7f '\x7f' */
135    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143};
144
145
146const unsigned char _Py_ctype_tolower[256] = {
147    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
148    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
149    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
150    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
151    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
152    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
153    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
154    0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
155    0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
156    0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
157    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
158    0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
159    0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
160    0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
161    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
162    0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
163    0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
164    0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
165    0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
166    0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
167    0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
168    0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
169    0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
170    0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
171    0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
172    0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
173    0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
174    0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
175    0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
176    0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
177    0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
178    0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
179};
180
181const unsigned char _Py_ctype_toupper[256] = {
182    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
184    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
185    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
186    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
187    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
188    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
189    0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
190    0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
191    0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
192    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
193    0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
194    0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
195    0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
196    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
197    0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
198    0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
199    0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
200    0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
201    0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
202    0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
203    0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
204    0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
205    0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
206    0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
207    0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
208    0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
209    0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
210    0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
211    0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
212    0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
213    0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
214};
215
216
217PyDoc_STRVAR_shared(_Py_isspace__doc__,
218"B.isspace() -> bool\n\
219\n\
220Return True if all characters in B are whitespace\n\
221and there is at least one character in B, False otherwise.");
222
223PyObject*
224_Py_bytes_isspace(const char *cptr, Py_ssize_t len)
225{
226    register const unsigned char *p
227        = (unsigned char *) cptr;
228    register const unsigned char *e;
229
230    /* Shortcut for single character strings */
231    if (len == 1 && ISSPACE(*p))
232        Py_RETURN_TRUE;
233
234    /* Special case for empty strings */
235    if (len == 0)
236        Py_RETURN_FALSE;
237
238    e = p + len;
239    for (; p < e; p++) {
240	if (!ISSPACE(*p))
241            Py_RETURN_FALSE;
242    }
243    Py_RETURN_TRUE;
244}
245
246
247PyDoc_STRVAR_shared(_Py_isalpha__doc__,
248"B.isalpha() -> bool\n\
249\n\
250Return True if all characters in B are alphabetic\n\
251and there is at least one character in B, False otherwise.");
252
253PyObject*
254_Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
255{
256    register const unsigned char *p
257        = (unsigned char *) cptr;
258    register const unsigned char *e;
259
260    /* Shortcut for single character strings */
261    if (len == 1 && ISALPHA(*p))
262	Py_RETURN_TRUE;
263
264    /* Special case for empty strings */
265    if (len == 0)
266	Py_RETURN_FALSE;
267
268    e = p + len;
269    for (; p < e; p++) {
270	if (!ISALPHA(*p))
271	    Py_RETURN_FALSE;
272    }
273    Py_RETURN_TRUE;
274}
275
276
277PyDoc_STRVAR_shared(_Py_isalnum__doc__,
278"B.isalnum() -> bool\n\
279\n\
280Return True if all characters in B are alphanumeric\n\
281and there is at least one character in B, False otherwise.");
282
283PyObject*
284_Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
285{
286    register const unsigned char *p
287        = (unsigned char *) cptr;
288    register const unsigned char *e;
289
290    /* Shortcut for single character strings */
291    if (len == 1 && ISALNUM(*p))
292	Py_RETURN_TRUE;
293
294    /* Special case for empty strings */
295    if (len == 0)
296	Py_RETURN_FALSE;
297
298    e = p + len;
299    for (; p < e; p++) {
300	if (!ISALNUM(*p))
301	    Py_RETURN_FALSE;
302    }
303    Py_RETURN_TRUE;
304}
305
306
307PyDoc_STRVAR_shared(_Py_isdigit__doc__,
308"B.isdigit() -> bool\n\
309\n\
310Return True if all characters in B are digits\n\
311and there is at least one character in B, False otherwise.");
312
313PyObject*
314_Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
315{
316    register const unsigned char *p
317        = (unsigned char *) cptr;
318    register const unsigned char *e;
319
320    /* Shortcut for single character strings */
321    if (len == 1 && ISDIGIT(*p))
322	Py_RETURN_TRUE;
323
324    /* Special case for empty strings */
325    if (len == 0)
326	Py_RETURN_FALSE;
327
328    e = p + len;
329    for (; p < e; p++) {
330	if (!ISDIGIT(*p))
331	    Py_RETURN_FALSE;
332    }
333    Py_RETURN_TRUE;
334}
335
336
337PyDoc_STRVAR_shared(_Py_islower__doc__,
338"B.islower() -> bool\n\
339\n\
340Return True if all cased characters in B are lowercase and there is\n\
341at least one cased character in B, False otherwise.");
342
343PyObject*
344_Py_bytes_islower(const char *cptr, Py_ssize_t len)
345{
346    register const unsigned char *p
347        = (unsigned char *) cptr;
348    register const unsigned char *e;
349    int cased;
350
351    /* Shortcut for single character strings */
352    if (len == 1)
353	return PyBool_FromLong(ISLOWER(*p));
354
355    /* Special case for empty strings */
356    if (len == 0)
357	Py_RETURN_FALSE;
358
359    e = p + len;
360    cased = 0;
361    for (; p < e; p++) {
362	if (ISUPPER(*p))
363	    Py_RETURN_FALSE;
364	else if (!cased && ISLOWER(*p))
365	    cased = 1;
366    }
367    return PyBool_FromLong(cased);
368}
369
370
371PyDoc_STRVAR_shared(_Py_isupper__doc__,
372"B.isupper() -> bool\n\
373\n\
374Return True if all cased characters in B are uppercase and there is\n\
375at least one cased character in B, False otherwise.");
376
377PyObject*
378_Py_bytes_isupper(const char *cptr, Py_ssize_t len)
379{
380    register const unsigned char *p
381        = (unsigned char *) cptr;
382    register const unsigned char *e;
383    int cased;
384
385    /* Shortcut for single character strings */
386    if (len == 1)
387	return PyBool_FromLong(ISUPPER(*p));
388
389    /* Special case for empty strings */
390    if (len == 0)
391	Py_RETURN_FALSE;
392
393    e = p + len;
394    cased = 0;
395    for (; p < e; p++) {
396	if (ISLOWER(*p))
397	    Py_RETURN_FALSE;
398	else if (!cased && ISUPPER(*p))
399	    cased = 1;
400    }
401    return PyBool_FromLong(cased);
402}
403
404
405PyDoc_STRVAR_shared(_Py_istitle__doc__,
406"B.istitle() -> bool\n\
407\n\
408Return True if B is a titlecased string and there is at least one\n\
409character in B, i.e. uppercase characters may only follow uncased\n\
410characters and lowercase characters only cased ones. Return False\n\
411otherwise.");
412
413PyObject*
414_Py_bytes_istitle(const char *cptr, Py_ssize_t len)
415{
416    register const unsigned char *p
417        = (unsigned char *) cptr;
418    register const unsigned char *e;
419    int cased, previous_is_cased;
420
421    /* Shortcut for single character strings */
422    if (len == 1)
423	return PyBool_FromLong(ISUPPER(*p));
424
425    /* Special case for empty strings */
426    if (len == 0)
427	Py_RETURN_FALSE;
428
429    e = p + len;
430    cased = 0;
431    previous_is_cased = 0;
432    for (; p < e; p++) {
433	register const unsigned char ch = *p;
434
435	if (ISUPPER(ch)) {
436	    if (previous_is_cased)
437		Py_RETURN_FALSE;
438	    previous_is_cased = 1;
439	    cased = 1;
440	}
441	else if (ISLOWER(ch)) {
442	    if (!previous_is_cased)
443		Py_RETURN_FALSE;
444	    previous_is_cased = 1;
445	    cased = 1;
446	}
447	else
448	    previous_is_cased = 0;
449    }
450    return PyBool_FromLong(cased);
451}
452
453
454PyDoc_STRVAR_shared(_Py_lower__doc__,
455"B.lower() -> copy of B\n\
456\n\
457Return a copy of B with all ASCII characters converted to lowercase.");
458
459void
460_Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
461{
462	Py_ssize_t i;
463
464        /*
465	newobj = PyString_FromStringAndSize(NULL, len);
466	if (!newobj)
467		return NULL;
468
469	s = PyString_AS_STRING(newobj);
470        */
471
472	Py_MEMCPY(result, cptr, len);
473
474	for (i = 0; i < len; i++) {
475		int c = Py_CHARMASK(result[i]);
476		if (ISUPPER(c))
477			result[i] = TOLOWER(c);
478	}
479}
480
481
482PyDoc_STRVAR_shared(_Py_upper__doc__,
483"B.upper() -> copy of B\n\
484\n\
485Return a copy of B with all ASCII characters converted to uppercase.");
486
487void
488_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
489{
490	Py_ssize_t i;
491
492        /*
493	newobj = PyString_FromStringAndSize(NULL, len);
494	if (!newobj)
495		return NULL;
496
497	s = PyString_AS_STRING(newobj);
498        */
499
500	Py_MEMCPY(result, cptr, len);
501
502	for (i = 0; i < len; i++) {
503		int c = Py_CHARMASK(result[i]);
504		if (ISLOWER(c))
505			result[i] = TOUPPER(c);
506	}
507}
508
509
510PyDoc_STRVAR_shared(_Py_title__doc__,
511"B.title() -> copy of B\n\
512\n\
513Return a titlecased version of B, i.e. ASCII words start with uppercase\n\
514characters, all remaining cased characters have lowercase.");
515
516void
517_Py_bytes_title(char *result, char *s, Py_ssize_t len)
518{
519	Py_ssize_t i;
520	int previous_is_cased = 0;
521
522        /*
523	newobj = PyString_FromStringAndSize(NULL, len);
524	if (newobj == NULL)
525		return NULL;
526	s_new = PyString_AsString(newobj);
527        */
528	for (i = 0; i < len; i++) {
529		int c = Py_CHARMASK(*s++);
530		if (ISLOWER(c)) {
531			if (!previous_is_cased)
532			    c = TOUPPER(c);
533			previous_is_cased = 1;
534		} else if (ISUPPER(c)) {
535			if (previous_is_cased)
536			    c = TOLOWER(c);
537			previous_is_cased = 1;
538		} else
539			previous_is_cased = 0;
540		*result++ = c;
541	}
542}
543
544
545PyDoc_STRVAR_shared(_Py_capitalize__doc__,
546"B.capitalize() -> copy of B\n\
547\n\
548Return a copy of B with only its first character capitalized (ASCII).");
549
550void
551_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
552{
553	Py_ssize_t i;
554
555        /*
556	newobj = PyString_FromStringAndSize(NULL, len);
557	if (newobj == NULL)
558		return NULL;
559	s_new = PyString_AsString(newobj);
560        */
561	if (0 < len) {
562		int c = Py_CHARMASK(*s++);
563		if (ISLOWER(c))
564			*result = TOUPPER(c);
565		else
566			*result = c;
567		result++;
568	}
569	for (i = 1; i < len; i++) {
570		int c = Py_CHARMASK(*s++);
571		if (ISUPPER(c))
572			*result = TOLOWER(c);
573		else
574			*result = c;
575		result++;
576	}
577}
578
579
580PyDoc_STRVAR_shared(_Py_swapcase__doc__,
581"B.swapcase() -> copy of B\n\
582\n\
583Return a copy of B with uppercase ASCII characters converted\n\
584to lowercase ASCII and vice versa.");
585
586void
587_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
588{
589	Py_ssize_t i;
590
591        /*
592	newobj = PyString_FromStringAndSize(NULL, len);
593	if (newobj == NULL)
594		return NULL;
595	s_new = PyString_AsString(newobj);
596        */
597	for (i = 0; i < len; i++) {
598		int c = Py_CHARMASK(*s++);
599		if (ISLOWER(c)) {
600			*result = TOUPPER(c);
601		}
602		else if (ISUPPER(c)) {
603			*result = TOLOWER(c);
604		}
605		else
606			*result = c;
607		result++;
608	}
609}
610