Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "bytes_methods.h" |
| 3 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 4 | PyDoc_STRVAR_shared(_Py_isspace__doc__, |
| 5 | "B.isspace() -> bool\n\ |
| 6 | \n\ |
| 7 | Return True if all characters in B are whitespace\n\ |
| 8 | and there is at least one character in B, False otherwise."); |
| 9 | |
| 10 | PyObject* |
| 11 | _Py_bytes_isspace(const char *cptr, Py_ssize_t len) |
| 12 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 13 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 14 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 16 | |
| 17 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 18 | if (len == 1 && Py_ISSPACE(*p)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 19 | 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++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | if (!Py_ISSPACE(*p)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 28 | Py_RETURN_FALSE; |
| 29 | } |
| 30 | Py_RETURN_TRUE; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | PyDoc_STRVAR_shared(_Py_isalpha__doc__, |
| 35 | "B.isalpha() -> bool\n\ |
| 36 | \n\ |
| 37 | Return True if all characters in B are alphabetic\n\ |
| 38 | and there is at least one character in B, False otherwise."); |
| 39 | |
| 40 | PyObject* |
| 41 | _Py_bytes_isalpha(const char *cptr, Py_ssize_t len) |
| 42 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 43 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 44 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 45 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 46 | |
| 47 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 48 | if (len == 1 && Py_ISALPHA(*p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | Py_RETURN_TRUE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 50 | |
| 51 | /* Special case for empty strings */ |
| 52 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 54 | |
| 55 | e = p + len; |
| 56 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | if (!Py_ISALPHA(*p)) |
| 58 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 59 | } |
| 60 | Py_RETURN_TRUE; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | PyDoc_STRVAR_shared(_Py_isalnum__doc__, |
| 65 | "B.isalnum() -> bool\n\ |
| 66 | \n\ |
| 67 | Return True if all characters in B are alphanumeric\n\ |
| 68 | and there is at least one character in B, False otherwise."); |
| 69 | |
| 70 | PyObject* |
| 71 | _Py_bytes_isalnum(const char *cptr, Py_ssize_t len) |
| 72 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 73 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 74 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 75 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 76 | |
| 77 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 78 | if (len == 1 && Py_ISALNUM(*p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | Py_RETURN_TRUE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 80 | |
| 81 | /* Special case for empty strings */ |
| 82 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 84 | |
| 85 | e = p + len; |
| 86 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | if (!Py_ISALNUM(*p)) |
| 88 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 89 | } |
| 90 | Py_RETURN_TRUE; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | PyDoc_STRVAR_shared(_Py_isdigit__doc__, |
| 95 | "B.isdigit() -> bool\n\ |
| 96 | \n\ |
| 97 | Return True if all characters in B are digits\n\ |
| 98 | and there is at least one character in B, False otherwise."); |
| 99 | |
| 100 | PyObject* |
| 101 | _Py_bytes_isdigit(const char *cptr, Py_ssize_t len) |
| 102 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 103 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 104 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 105 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 106 | |
| 107 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 108 | if (len == 1 && Py_ISDIGIT(*p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | Py_RETURN_TRUE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 110 | |
| 111 | /* Special case for empty strings */ |
| 112 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 114 | |
| 115 | e = p + len; |
| 116 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | if (!Py_ISDIGIT(*p)) |
| 118 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 119 | } |
| 120 | Py_RETURN_TRUE; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | PyDoc_STRVAR_shared(_Py_islower__doc__, |
| 125 | "B.islower() -> bool\n\ |
| 126 | \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."); |
| 129 | |
| 130 | PyObject* |
| 131 | _Py_bytes_islower(const char *cptr, Py_ssize_t len) |
| 132 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 133 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 134 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 135 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 136 | int cased; |
| 137 | |
| 138 | /* Shortcut for single character strings */ |
| 139 | if (len == 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | return PyBool_FromLong(Py_ISLOWER(*p)); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 141 | |
| 142 | /* Special case for empty strings */ |
| 143 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 145 | |
| 146 | e = p + len; |
| 147 | cased = 0; |
| 148 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | if (Py_ISUPPER(*p)) |
| 150 | Py_RETURN_FALSE; |
| 151 | else if (!cased && Py_ISLOWER(*p)) |
| 152 | cased = 1; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 153 | } |
| 154 | return PyBool_FromLong(cased); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | PyDoc_STRVAR_shared(_Py_isupper__doc__, |
| 159 | "B.isupper() -> bool\n\ |
| 160 | \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."); |
| 163 | |
| 164 | PyObject* |
| 165 | _Py_bytes_isupper(const char *cptr, Py_ssize_t len) |
| 166 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 167 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 168 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 169 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 170 | int cased; |
| 171 | |
| 172 | /* Shortcut for single character strings */ |
| 173 | if (len == 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | return PyBool_FromLong(Py_ISUPPER(*p)); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 175 | |
| 176 | /* Special case for empty strings */ |
| 177 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 179 | |
| 180 | e = p + len; |
| 181 | cased = 0; |
| 182 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | if (Py_ISLOWER(*p)) |
| 184 | Py_RETURN_FALSE; |
| 185 | else if (!cased && Py_ISUPPER(*p)) |
| 186 | cased = 1; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 187 | } |
| 188 | return PyBool_FromLong(cased); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | PyDoc_STRVAR_shared(_Py_istitle__doc__, |
| 193 | "B.istitle() -> bool\n\ |
| 194 | \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\ |
| 198 | otherwise."); |
| 199 | |
| 200 | PyObject* |
| 201 | _Py_bytes_istitle(const char *cptr, Py_ssize_t len) |
| 202 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 203 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 204 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 205 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 206 | int cased, previous_is_cased; |
| 207 | |
| 208 | /* Shortcut for single character strings */ |
| 209 | if (len == 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | return PyBool_FromLong(Py_ISUPPER(*p)); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 211 | |
| 212 | /* Special case for empty strings */ |
| 213 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 215 | |
| 216 | e = p + len; |
| 217 | cased = 0; |
| 218 | previous_is_cased = 0; |
| 219 | for (; p < e; p++) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 220 | const unsigned char ch = *p; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | if (Py_ISUPPER(ch)) { |
| 223 | if (previous_is_cased) |
| 224 | Py_RETURN_FALSE; |
| 225 | previous_is_cased = 1; |
| 226 | cased = 1; |
| 227 | } |
| 228 | else if (Py_ISLOWER(ch)) { |
| 229 | if (!previous_is_cased) |
| 230 | Py_RETURN_FALSE; |
| 231 | previous_is_cased = 1; |
| 232 | cased = 1; |
| 233 | } |
| 234 | else |
| 235 | previous_is_cased = 0; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 236 | } |
| 237 | return PyBool_FromLong(cased); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | PyDoc_STRVAR_shared(_Py_lower__doc__, |
| 242 | "B.lower() -> copy of B\n\ |
| 243 | \n\ |
| 244 | Return a copy of B with all ASCII characters converted to lowercase."); |
| 245 | |
| 246 | void |
| 247 | _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len) |
| 248 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 249 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 251 | for (i = 0; i < len; i++) { |
Antoine Pitrou | 94f6fa6 | 2012-01-08 16:22:46 +0100 | [diff] [blame] | 252 | result[i] = Py_TOLOWER((unsigned char) cptr[i]); |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 253 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | PyDoc_STRVAR_shared(_Py_upper__doc__, |
| 258 | "B.upper() -> copy of B\n\ |
| 259 | \n\ |
| 260 | Return a copy of B with all ASCII characters converted to uppercase."); |
| 261 | |
| 262 | void |
| 263 | _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len) |
| 264 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 265 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 267 | for (i = 0; i < len; i++) { |
Antoine Pitrou | 94f6fa6 | 2012-01-08 16:22:46 +0100 | [diff] [blame] | 268 | result[i] = Py_TOUPPER((unsigned char) cptr[i]); |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 269 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | PyDoc_STRVAR_shared(_Py_title__doc__, |
| 274 | "B.title() -> copy of B\n\ |
| 275 | \n\ |
| 276 | Return a titlecased version of B, i.e. ASCII words start with uppercase\n\ |
| 277 | characters, all remaining cased characters have lowercase."); |
| 278 | |
| 279 | void |
| 280 | _Py_bytes_title(char *result, char *s, Py_ssize_t len) |
| 281 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 282 | Py_ssize_t i; |
| 283 | int previous_is_cased = 0; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 285 | for (i = 0; i < len; i++) { |
| 286 | int c = Py_CHARMASK(*s++); |
| 287 | if (Py_ISLOWER(c)) { |
| 288 | if (!previous_is_cased) |
| 289 | c = Py_TOUPPER(c); |
| 290 | previous_is_cased = 1; |
| 291 | } else if (Py_ISUPPER(c)) { |
| 292 | if (previous_is_cased) |
| 293 | c = Py_TOLOWER(c); |
| 294 | previous_is_cased = 1; |
| 295 | } else |
| 296 | previous_is_cased = 0; |
| 297 | *result++ = c; |
| 298 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | PyDoc_STRVAR_shared(_Py_capitalize__doc__, |
| 303 | "B.capitalize() -> copy of B\n\ |
| 304 | \n\ |
Senthil Kumaran | e51ee8a | 2010-07-05 12:00:56 +0000 | [diff] [blame] | 305 | Return a copy of B with only its first character capitalized (ASCII)\n\ |
| 306 | and the rest lower-cased."); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 307 | |
| 308 | void |
| 309 | _Py_bytes_capitalize(char *result, char *s, Py_ssize_t len) |
| 310 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 311 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 313 | if (0 < len) { |
| 314 | int c = Py_CHARMASK(*s++); |
| 315 | if (Py_ISLOWER(c)) |
| 316 | *result = Py_TOUPPER(c); |
| 317 | else |
| 318 | *result = c; |
| 319 | result++; |
| 320 | } |
| 321 | for (i = 1; i < len; i++) { |
| 322 | int c = Py_CHARMASK(*s++); |
| 323 | if (Py_ISUPPER(c)) |
| 324 | *result = Py_TOLOWER(c); |
| 325 | else |
| 326 | *result = c; |
| 327 | result++; |
| 328 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | PyDoc_STRVAR_shared(_Py_swapcase__doc__, |
| 333 | "B.swapcase() -> copy of B\n\ |
| 334 | \n\ |
| 335 | Return a copy of B with uppercase ASCII characters converted\n\ |
| 336 | to lowercase ASCII and vice versa."); |
| 337 | |
| 338 | void |
| 339 | _Py_bytes_swapcase(char *result, char *s, Py_ssize_t len) |
| 340 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 341 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 343 | for (i = 0; i < len; i++) { |
| 344 | int c = Py_CHARMASK(*s++); |
| 345 | if (Py_ISLOWER(c)) { |
| 346 | *result = Py_TOUPPER(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | } |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 348 | else if (Py_ISUPPER(c)) { |
| 349 | *result = Py_TOLOWER(c); |
| 350 | } |
| 351 | else |
| 352 | *result = c; |
| 353 | result++; |
| 354 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 357 | |
| 358 | PyDoc_STRVAR_shared(_Py_maketrans__doc__, |
| 359 | "B.maketrans(frm, to) -> translation table\n\ |
| 360 | \n\ |
Senthil Kumaran | 84e3ccc | 2011-06-27 09:06:45 -0700 | [diff] [blame] | 361 | Return a translation table (a bytes object of length 256) suitable\n\ |
| 362 | for use in the bytes or bytearray translate method where each byte\n\ |
| 363 | in frm is mapped to the byte at the same position in to.\n\ |
| 364 | The bytes objects frm and to must be of the same length."); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 365 | |
| 366 | static Py_ssize_t |
| 367 | _getbuffer(PyObject *obj, Py_buffer *view) |
| 368 | { |
| 369 | PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
| 370 | |
| 371 | if (buffer == NULL || buffer->bf_getbuffer == NULL) |
| 372 | { |
| 373 | PyErr_Format(PyExc_TypeError, |
| 374 | "Type %.100s doesn't support the buffer API", |
| 375 | Py_TYPE(obj)->tp_name); |
| 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 380 | return -1; |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 381 | return view->len; |
| 382 | } |
| 383 | |
| 384 | PyObject * |
| 385 | _Py_bytes_maketrans(PyObject *args) |
| 386 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 387 | PyObject *frm, *to, *res = NULL; |
| 388 | Py_buffer bfrm, bto; |
| 389 | Py_ssize_t i; |
| 390 | char *p; |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 391 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 392 | bfrm.len = -1; |
| 393 | bto.len = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 395 | if (!PyArg_ParseTuple(args, "OO:maketrans", &frm, &to)) |
| 396 | return NULL; |
| 397 | if (_getbuffer(frm, &bfrm) < 0) |
| 398 | return NULL; |
| 399 | if (_getbuffer(to, &bto) < 0) |
| 400 | goto done; |
| 401 | if (bfrm.len != bto.len) { |
| 402 | PyErr_Format(PyExc_ValueError, |
| 403 | "maketrans arguments must have same length"); |
| 404 | goto done; |
| 405 | } |
| 406 | res = PyBytes_FromStringAndSize(NULL, 256); |
| 407 | if (!res) { |
| 408 | goto done; |
| 409 | } |
| 410 | p = PyBytes_AS_STRING(res); |
| 411 | for (i = 0; i < 256; i++) |
Antoine Pitrou | 47019e5 | 2010-08-15 17:41:31 +0000 | [diff] [blame] | 412 | p[i] = (char) i; |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 413 | for (i = 0; i < bfrm.len; i++) { |
| 414 | p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i]; |
| 415 | } |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 417 | done: |
| 418 | if (bfrm.len != -1) |
| 419 | PyBuffer_Release(&bfrm); |
| 420 | if (bto.len != -1) |
| 421 | PyBuffer_Release(&bto); |
| 422 | return res; |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 423 | } |