]>
Commit | Line | Data |
---|---|---|
53b2ba57 DM |
1 | #include "Python.h"\r |
2 | #include "bytes_methods.h"\r | |
3 | \r | |
4 | PyDoc_STRVAR_shared(_Py_isspace__doc__,\r | |
5 | "B.isspace() -> bool\n\\r | |
6 | \n\\r | |
7 | Return True if all characters in B are whitespace\n\\r | |
8 | and there is at least one character in B, False otherwise.");\r | |
9 | \r | |
10 | PyObject*\r | |
11 | _Py_bytes_isspace(const char *cptr, Py_ssize_t len)\r | |
12 | {\r | |
13 | register const unsigned char *p\r | |
14 | = (unsigned char *) cptr;\r | |
15 | register const unsigned char *e;\r | |
16 | \r | |
17 | /* Shortcut for single character strings */\r | |
18 | if (len == 1 && Py_ISSPACE(*p))\r | |
19 | Py_RETURN_TRUE;\r | |
20 | \r | |
21 | /* Special case for empty strings */\r | |
22 | if (len == 0)\r | |
23 | Py_RETURN_FALSE;\r | |
24 | \r | |
25 | e = p + len;\r | |
26 | for (; p < e; p++) {\r | |
27 | if (!Py_ISSPACE(*p))\r | |
28 | Py_RETURN_FALSE;\r | |
29 | }\r | |
30 | Py_RETURN_TRUE;\r | |
31 | }\r | |
32 | \r | |
33 | \r | |
34 | PyDoc_STRVAR_shared(_Py_isalpha__doc__,\r | |
35 | "B.isalpha() -> bool\n\\r | |
36 | \n\\r | |
37 | Return True if all characters in B are alphabetic\n\\r | |
38 | and there is at least one character in B, False otherwise.");\r | |
39 | \r | |
40 | PyObject*\r | |
41 | _Py_bytes_isalpha(const char *cptr, Py_ssize_t len)\r | |
42 | {\r | |
43 | register const unsigned char *p\r | |
44 | = (unsigned char *) cptr;\r | |
45 | register const unsigned char *e;\r | |
46 | \r | |
47 | /* Shortcut for single character strings */\r | |
48 | if (len == 1 && Py_ISALPHA(*p))\r | |
49 | Py_RETURN_TRUE;\r | |
50 | \r | |
51 | /* Special case for empty strings */\r | |
52 | if (len == 0)\r | |
53 | Py_RETURN_FALSE;\r | |
54 | \r | |
55 | e = p + len;\r | |
56 | for (; p < e; p++) {\r | |
57 | if (!Py_ISALPHA(*p))\r | |
58 | Py_RETURN_FALSE;\r | |
59 | }\r | |
60 | Py_RETURN_TRUE;\r | |
61 | }\r | |
62 | \r | |
63 | \r | |
64 | PyDoc_STRVAR_shared(_Py_isalnum__doc__,\r | |
65 | "B.isalnum() -> bool\n\\r | |
66 | \n\\r | |
67 | Return True if all characters in B are alphanumeric\n\\r | |
68 | and there is at least one character in B, False otherwise.");\r | |
69 | \r | |
70 | PyObject*\r | |
71 | _Py_bytes_isalnum(const char *cptr, Py_ssize_t len)\r | |
72 | {\r | |
73 | register const unsigned char *p\r | |
74 | = (unsigned char *) cptr;\r | |
75 | register const unsigned char *e;\r | |
76 | \r | |
77 | /* Shortcut for single character strings */\r | |
78 | if (len == 1 && Py_ISALNUM(*p))\r | |
79 | Py_RETURN_TRUE;\r | |
80 | \r | |
81 | /* Special case for empty strings */\r | |
82 | if (len == 0)\r | |
83 | Py_RETURN_FALSE;\r | |
84 | \r | |
85 | e = p + len;\r | |
86 | for (; p < e; p++) {\r | |
87 | if (!Py_ISALNUM(*p))\r | |
88 | Py_RETURN_FALSE;\r | |
89 | }\r | |
90 | Py_RETURN_TRUE;\r | |
91 | }\r | |
92 | \r | |
93 | \r | |
94 | PyDoc_STRVAR_shared(_Py_isdigit__doc__,\r | |
95 | "B.isdigit() -> bool\n\\r | |
96 | \n\\r | |
97 | Return True if all characters in B are digits\n\\r | |
98 | and there is at least one character in B, False otherwise.");\r | |
99 | \r | |
100 | PyObject*\r | |
101 | _Py_bytes_isdigit(const char *cptr, Py_ssize_t len)\r | |
102 | {\r | |
103 | register const unsigned char *p\r | |
104 | = (unsigned char *) cptr;\r | |
105 | register const unsigned char *e;\r | |
106 | \r | |
107 | /* Shortcut for single character strings */\r | |
108 | if (len == 1 && Py_ISDIGIT(*p))\r | |
109 | Py_RETURN_TRUE;\r | |
110 | \r | |
111 | /* Special case for empty strings */\r | |
112 | if (len == 0)\r | |
113 | Py_RETURN_FALSE;\r | |
114 | \r | |
115 | e = p + len;\r | |
116 | for (; p < e; p++) {\r | |
117 | if (!Py_ISDIGIT(*p))\r | |
118 | Py_RETURN_FALSE;\r | |
119 | }\r | |
120 | Py_RETURN_TRUE;\r | |
121 | }\r | |
122 | \r | |
123 | \r | |
124 | PyDoc_STRVAR_shared(_Py_islower__doc__,\r | |
125 | "B.islower() -> bool\n\\r | |
126 | \n\\r | |
127 | Return True if all cased characters in B are lowercase and there is\n\\r | |
128 | at least one cased character in B, False otherwise.");\r | |
129 | \r | |
130 | PyObject*\r | |
131 | _Py_bytes_islower(const char *cptr, Py_ssize_t len)\r | |
132 | {\r | |
133 | register const unsigned char *p\r | |
134 | = (unsigned char *) cptr;\r | |
135 | register const unsigned char *e;\r | |
136 | int cased;\r | |
137 | \r | |
138 | /* Shortcut for single character strings */\r | |
139 | if (len == 1)\r | |
140 | return PyBool_FromLong(Py_ISLOWER(*p));\r | |
141 | \r | |
142 | /* Special case for empty strings */\r | |
143 | if (len == 0)\r | |
144 | Py_RETURN_FALSE;\r | |
145 | \r | |
146 | e = p + len;\r | |
147 | cased = 0;\r | |
148 | for (; p < e; p++) {\r | |
149 | if (Py_ISUPPER(*p))\r | |
150 | Py_RETURN_FALSE;\r | |
151 | else if (!cased && Py_ISLOWER(*p))\r | |
152 | cased = 1;\r | |
153 | }\r | |
154 | return PyBool_FromLong(cased);\r | |
155 | }\r | |
156 | \r | |
157 | \r | |
158 | PyDoc_STRVAR_shared(_Py_isupper__doc__,\r | |
159 | "B.isupper() -> bool\n\\r | |
160 | \n\\r | |
161 | Return True if all cased characters in B are uppercase and there is\n\\r | |
162 | at least one cased character in B, False otherwise.");\r | |
163 | \r | |
164 | PyObject*\r | |
165 | _Py_bytes_isupper(const char *cptr, Py_ssize_t len)\r | |
166 | {\r | |
167 | register const unsigned char *p\r | |
168 | = (unsigned char *) cptr;\r | |
169 | register const unsigned char *e;\r | |
170 | int cased;\r | |
171 | \r | |
172 | /* Shortcut for single character strings */\r | |
173 | if (len == 1)\r | |
174 | return PyBool_FromLong(Py_ISUPPER(*p));\r | |
175 | \r | |
176 | /* Special case for empty strings */\r | |
177 | if (len == 0)\r | |
178 | Py_RETURN_FALSE;\r | |
179 | \r | |
180 | e = p + len;\r | |
181 | cased = 0;\r | |
182 | for (; p < e; p++) {\r | |
183 | if (Py_ISLOWER(*p))\r | |
184 | Py_RETURN_FALSE;\r | |
185 | else if (!cased && Py_ISUPPER(*p))\r | |
186 | cased = 1;\r | |
187 | }\r | |
188 | return PyBool_FromLong(cased);\r | |
189 | }\r | |
190 | \r | |
191 | \r | |
192 | PyDoc_STRVAR_shared(_Py_istitle__doc__,\r | |
193 | "B.istitle() -> bool\n\\r | |
194 | \n\\r | |
195 | Return True if B is a titlecased string and there is at least one\n\\r | |
196 | character in B, i.e. uppercase characters may only follow uncased\n\\r | |
197 | characters and lowercase characters only cased ones. Return False\n\\r | |
198 | otherwise.");\r | |
199 | \r | |
200 | PyObject*\r | |
201 | _Py_bytes_istitle(const char *cptr, Py_ssize_t len)\r | |
202 | {\r | |
203 | register const unsigned char *p\r | |
204 | = (unsigned char *) cptr;\r | |
205 | register const unsigned char *e;\r | |
206 | int cased, previous_is_cased;\r | |
207 | \r | |
208 | /* Shortcut for single character strings */\r | |
209 | if (len == 1)\r | |
210 | return PyBool_FromLong(Py_ISUPPER(*p));\r | |
211 | \r | |
212 | /* Special case for empty strings */\r | |
213 | if (len == 0)\r | |
214 | Py_RETURN_FALSE;\r | |
215 | \r | |
216 | e = p + len;\r | |
217 | cased = 0;\r | |
218 | previous_is_cased = 0;\r | |
219 | for (; p < e; p++) {\r | |
220 | register const unsigned char ch = *p;\r | |
221 | \r | |
222 | if (Py_ISUPPER(ch)) {\r | |
223 | if (previous_is_cased)\r | |
224 | Py_RETURN_FALSE;\r | |
225 | previous_is_cased = 1;\r | |
226 | cased = 1;\r | |
227 | }\r | |
228 | else if (Py_ISLOWER(ch)) {\r | |
229 | if (!previous_is_cased)\r | |
230 | Py_RETURN_FALSE;\r | |
231 | previous_is_cased = 1;\r | |
232 | cased = 1;\r | |
233 | }\r | |
234 | else\r | |
235 | previous_is_cased = 0;\r | |
236 | }\r | |
237 | return PyBool_FromLong(cased);\r | |
238 | }\r | |
239 | \r | |
240 | \r | |
241 | PyDoc_STRVAR_shared(_Py_lower__doc__,\r | |
242 | "B.lower() -> copy of B\n\\r | |
243 | \n\\r | |
244 | Return a copy of B with all ASCII characters converted to lowercase.");\r | |
245 | \r | |
246 | void\r | |
247 | _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)\r | |
248 | {\r | |
249 | Py_ssize_t i;\r | |
250 | \r | |
251 | /*\r | |
252 | newobj = PyString_FromStringAndSize(NULL, len);\r | |
253 | if (!newobj)\r | |
254 | return NULL;\r | |
255 | \r | |
256 | s = PyString_AS_STRING(newobj);\r | |
257 | */\r | |
258 | \r | |
259 | Py_MEMCPY(result, cptr, len);\r | |
260 | \r | |
261 | for (i = 0; i < len; i++) {\r | |
262 | int c = Py_CHARMASK(result[i]);\r | |
263 | if (Py_ISUPPER(c))\r | |
264 | result[i] = Py_TOLOWER(c);\r | |
265 | }\r | |
266 | }\r | |
267 | \r | |
268 | \r | |
269 | PyDoc_STRVAR_shared(_Py_upper__doc__,\r | |
270 | "B.upper() -> copy of B\n\\r | |
271 | \n\\r | |
272 | Return a copy of B with all ASCII characters converted to uppercase.");\r | |
273 | \r | |
274 | void\r | |
275 | _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)\r | |
276 | {\r | |
277 | Py_ssize_t i;\r | |
278 | \r | |
279 | /*\r | |
280 | newobj = PyString_FromStringAndSize(NULL, len);\r | |
281 | if (!newobj)\r | |
282 | return NULL;\r | |
283 | \r | |
284 | s = PyString_AS_STRING(newobj);\r | |
285 | */\r | |
286 | \r | |
287 | Py_MEMCPY(result, cptr, len);\r | |
288 | \r | |
289 | for (i = 0; i < len; i++) {\r | |
290 | int c = Py_CHARMASK(result[i]);\r | |
291 | if (Py_ISLOWER(c))\r | |
292 | result[i] = Py_TOUPPER(c);\r | |
293 | }\r | |
294 | }\r | |
295 | \r | |
296 | \r | |
297 | PyDoc_STRVAR_shared(_Py_title__doc__,\r | |
298 | "B.title() -> copy of B\n\\r | |
299 | \n\\r | |
300 | Return a titlecased version of B, i.e. ASCII words start with uppercase\n\\r | |
301 | characters, all remaining cased characters have lowercase.");\r | |
302 | \r | |
303 | void\r | |
304 | _Py_bytes_title(char *result, char *s, Py_ssize_t len)\r | |
305 | {\r | |
306 | Py_ssize_t i;\r | |
307 | int previous_is_cased = 0;\r | |
308 | \r | |
309 | /*\r | |
310 | newobj = PyString_FromStringAndSize(NULL, len);\r | |
311 | if (newobj == NULL)\r | |
312 | return NULL;\r | |
313 | s_new = PyString_AsString(newobj);\r | |
314 | */\r | |
315 | for (i = 0; i < len; i++) {\r | |
316 | int c = Py_CHARMASK(*s++);\r | |
317 | if (Py_ISLOWER(c)) {\r | |
318 | if (!previous_is_cased)\r | |
319 | c = Py_TOUPPER(c);\r | |
320 | previous_is_cased = 1;\r | |
321 | } else if (Py_ISUPPER(c)) {\r | |
322 | if (previous_is_cased)\r | |
323 | c = Py_TOLOWER(c);\r | |
324 | previous_is_cased = 1;\r | |
325 | } else\r | |
326 | previous_is_cased = 0;\r | |
327 | *result++ = c;\r | |
328 | }\r | |
329 | }\r | |
330 | \r | |
331 | \r | |
332 | PyDoc_STRVAR_shared(_Py_capitalize__doc__,\r | |
333 | "B.capitalize() -> copy of B\n\\r | |
334 | \n\\r | |
335 | Return a copy of B with only its first character capitalized (ASCII)\n\\r | |
336 | and the rest lower-cased.");\r | |
337 | \r | |
338 | void\r | |
339 | _Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)\r | |
340 | {\r | |
341 | Py_ssize_t i;\r | |
342 | \r | |
343 | /*\r | |
344 | newobj = PyString_FromStringAndSize(NULL, len);\r | |
345 | if (newobj == NULL)\r | |
346 | return NULL;\r | |
347 | s_new = PyString_AsString(newobj);\r | |
348 | */\r | |
349 | if (0 < len) {\r | |
350 | int c = Py_CHARMASK(*s++);\r | |
351 | if (Py_ISLOWER(c))\r | |
352 | *result = Py_TOUPPER(c);\r | |
353 | else\r | |
354 | *result = c;\r | |
355 | result++;\r | |
356 | }\r | |
357 | for (i = 1; i < len; i++) {\r | |
358 | int c = Py_CHARMASK(*s++);\r | |
359 | if (Py_ISUPPER(c))\r | |
360 | *result = Py_TOLOWER(c);\r | |
361 | else\r | |
362 | *result = c;\r | |
363 | result++;\r | |
364 | }\r | |
365 | }\r | |
366 | \r | |
367 | \r | |
368 | PyDoc_STRVAR_shared(_Py_swapcase__doc__,\r | |
369 | "B.swapcase() -> copy of B\n\\r | |
370 | \n\\r | |
371 | Return a copy of B with uppercase ASCII characters converted\n\\r | |
372 | to lowercase ASCII and vice versa.");\r | |
373 | \r | |
374 | void\r | |
375 | _Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)\r | |
376 | {\r | |
377 | Py_ssize_t i;\r | |
378 | \r | |
379 | /*\r | |
380 | newobj = PyString_FromStringAndSize(NULL, len);\r | |
381 | if (newobj == NULL)\r | |
382 | return NULL;\r | |
383 | s_new = PyString_AsString(newobj);\r | |
384 | */\r | |
385 | for (i = 0; i < len; i++) {\r | |
386 | int c = Py_CHARMASK(*s++);\r | |
387 | if (Py_ISLOWER(c)) {\r | |
388 | *result = Py_TOUPPER(c);\r | |
389 | }\r | |
390 | else if (Py_ISUPPER(c)) {\r | |
391 | *result = Py_TOLOWER(c);\r | |
392 | }\r | |
393 | else\r | |
394 | *result = c;\r | |
395 | result++;\r | |
396 | }\r | |
397 | }\r | |
398 | \r |