Serhiy Storchaka | ab8bcb3 | 2016-07-03 13:26:52 +0300 | [diff] [blame] | 1 | #define PY_SSIZE_T_CLEAN |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2 | #include "Python.h" |
| 3 | #include "bytes_methods.h" |
| 4 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 5 | PyDoc_STRVAR_shared(_Py_isspace__doc__, |
| 6 | "B.isspace() -> bool\n\ |
| 7 | \n\ |
| 8 | Return True if all characters in B are whitespace\n\ |
| 9 | and there is at least one character in B, False otherwise."); |
| 10 | |
| 11 | PyObject* |
| 12 | _Py_bytes_isspace(const char *cptr, Py_ssize_t len) |
| 13 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 15 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 16 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 17 | |
| 18 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 19 | if (len == 1 && Py_ISSPACE(*p)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 20 | Py_RETURN_TRUE; |
| 21 | |
| 22 | /* Special case for empty strings */ |
| 23 | if (len == 0) |
| 24 | Py_RETURN_FALSE; |
| 25 | |
| 26 | e = p + len; |
| 27 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | if (!Py_ISSPACE(*p)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 29 | Py_RETURN_FALSE; |
| 30 | } |
| 31 | Py_RETURN_TRUE; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | PyDoc_STRVAR_shared(_Py_isalpha__doc__, |
| 36 | "B.isalpha() -> bool\n\ |
| 37 | \n\ |
| 38 | Return True if all characters in B are alphabetic\n\ |
| 39 | and there is at least one character in B, False otherwise."); |
| 40 | |
| 41 | PyObject* |
| 42 | _Py_bytes_isalpha(const char *cptr, Py_ssize_t len) |
| 43 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 44 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 45 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 46 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 47 | |
| 48 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 49 | if (len == 1 && Py_ISALPHA(*p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | Py_RETURN_TRUE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 51 | |
| 52 | /* Special case for empty strings */ |
| 53 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 55 | |
| 56 | e = p + len; |
| 57 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | if (!Py_ISALPHA(*p)) |
| 59 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 60 | } |
| 61 | Py_RETURN_TRUE; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | PyDoc_STRVAR_shared(_Py_isalnum__doc__, |
| 66 | "B.isalnum() -> bool\n\ |
| 67 | \n\ |
| 68 | Return True if all characters in B are alphanumeric\n\ |
| 69 | and there is at least one character in B, False otherwise."); |
| 70 | |
| 71 | PyObject* |
| 72 | _Py_bytes_isalnum(const char *cptr, Py_ssize_t len) |
| 73 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 74 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 75 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 76 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 77 | |
| 78 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 79 | if (len == 1 && Py_ISALNUM(*p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | Py_RETURN_TRUE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 81 | |
| 82 | /* Special case for empty strings */ |
| 83 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 85 | |
| 86 | e = p + len; |
| 87 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | if (!Py_ISALNUM(*p)) |
| 89 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 90 | } |
| 91 | Py_RETURN_TRUE; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | PyDoc_STRVAR_shared(_Py_isdigit__doc__, |
| 96 | "B.isdigit() -> bool\n\ |
| 97 | \n\ |
| 98 | Return True if all characters in B are digits\n\ |
| 99 | and there is at least one character in B, False otherwise."); |
| 100 | |
| 101 | PyObject* |
| 102 | _Py_bytes_isdigit(const char *cptr, Py_ssize_t len) |
| 103 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 104 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 105 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 106 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 107 | |
| 108 | /* Shortcut for single character strings */ |
Eric Smith | 6dc46f5 | 2009-04-27 20:39:49 +0000 | [diff] [blame] | 109 | if (len == 1 && Py_ISDIGIT(*p)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | Py_RETURN_TRUE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 111 | |
| 112 | /* Special case for empty strings */ |
| 113 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 115 | |
| 116 | e = p + len; |
| 117 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | if (!Py_ISDIGIT(*p)) |
| 119 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 120 | } |
| 121 | Py_RETURN_TRUE; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | PyDoc_STRVAR_shared(_Py_islower__doc__, |
| 126 | "B.islower() -> bool\n\ |
| 127 | \n\ |
| 128 | Return True if all cased characters in B are lowercase and there is\n\ |
| 129 | at least one cased character in B, False otherwise."); |
| 130 | |
| 131 | PyObject* |
| 132 | _Py_bytes_islower(const char *cptr, Py_ssize_t len) |
| 133 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 134 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 135 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 136 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 137 | int cased; |
| 138 | |
| 139 | /* Shortcut for single character strings */ |
| 140 | if (len == 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | return PyBool_FromLong(Py_ISLOWER(*p)); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 142 | |
| 143 | /* Special case for empty strings */ |
| 144 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 146 | |
| 147 | e = p + len; |
| 148 | cased = 0; |
| 149 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | if (Py_ISUPPER(*p)) |
| 151 | Py_RETURN_FALSE; |
| 152 | else if (!cased && Py_ISLOWER(*p)) |
| 153 | cased = 1; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 154 | } |
| 155 | return PyBool_FromLong(cased); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | PyDoc_STRVAR_shared(_Py_isupper__doc__, |
| 160 | "B.isupper() -> bool\n\ |
| 161 | \n\ |
| 162 | Return True if all cased characters in B are uppercase and there is\n\ |
| 163 | at least one cased character in B, False otherwise."); |
| 164 | |
| 165 | PyObject* |
| 166 | _Py_bytes_isupper(const char *cptr, Py_ssize_t len) |
| 167 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 168 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 169 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 170 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 171 | int cased; |
| 172 | |
| 173 | /* Shortcut for single character strings */ |
| 174 | if (len == 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | return PyBool_FromLong(Py_ISUPPER(*p)); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 176 | |
| 177 | /* Special case for empty strings */ |
| 178 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 180 | |
| 181 | e = p + len; |
| 182 | cased = 0; |
| 183 | for (; p < e; p++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | if (Py_ISLOWER(*p)) |
| 185 | Py_RETURN_FALSE; |
| 186 | else if (!cased && Py_ISUPPER(*p)) |
| 187 | cased = 1; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 188 | } |
| 189 | return PyBool_FromLong(cased); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | PyDoc_STRVAR_shared(_Py_istitle__doc__, |
| 194 | "B.istitle() -> bool\n\ |
| 195 | \n\ |
| 196 | Return True if B is a titlecased string and there is at least one\n\ |
| 197 | character in B, i.e. uppercase characters may only follow uncased\n\ |
| 198 | characters and lowercase characters only cased ones. Return False\n\ |
| 199 | otherwise."); |
| 200 | |
| 201 | PyObject* |
| 202 | _Py_bytes_istitle(const char *cptr, Py_ssize_t len) |
| 203 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 204 | const unsigned char *p |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 205 | = (unsigned char *) cptr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 206 | const unsigned char *e; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 207 | int cased, previous_is_cased; |
| 208 | |
| 209 | /* Shortcut for single character strings */ |
| 210 | if (len == 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | return PyBool_FromLong(Py_ISUPPER(*p)); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 212 | |
| 213 | /* Special case for empty strings */ |
| 214 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | Py_RETURN_FALSE; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 216 | |
| 217 | e = p + len; |
| 218 | cased = 0; |
| 219 | previous_is_cased = 0; |
| 220 | for (; p < e; p++) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 221 | const unsigned char ch = *p; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | if (Py_ISUPPER(ch)) { |
| 224 | if (previous_is_cased) |
| 225 | Py_RETURN_FALSE; |
| 226 | previous_is_cased = 1; |
| 227 | cased = 1; |
| 228 | } |
| 229 | else if (Py_ISLOWER(ch)) { |
| 230 | if (!previous_is_cased) |
| 231 | Py_RETURN_FALSE; |
| 232 | previous_is_cased = 1; |
| 233 | cased = 1; |
| 234 | } |
| 235 | else |
| 236 | previous_is_cased = 0; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 237 | } |
| 238 | return PyBool_FromLong(cased); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | PyDoc_STRVAR_shared(_Py_lower__doc__, |
| 243 | "B.lower() -> copy of B\n\ |
| 244 | \n\ |
| 245 | Return a copy of B with all ASCII characters converted to lowercase."); |
| 246 | |
| 247 | void |
| 248 | _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len) |
| 249 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 250 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 252 | for (i = 0; i < len; i++) { |
Antoine Pitrou | 94f6fa6 | 2012-01-08 16:22:46 +0100 | [diff] [blame] | 253 | result[i] = Py_TOLOWER((unsigned char) cptr[i]); |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 254 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | PyDoc_STRVAR_shared(_Py_upper__doc__, |
| 259 | "B.upper() -> copy of B\n\ |
| 260 | \n\ |
| 261 | Return a copy of B with all ASCII characters converted to uppercase."); |
| 262 | |
| 263 | void |
| 264 | _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len) |
| 265 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 266 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 268 | for (i = 0; i < len; i++) { |
Antoine Pitrou | 94f6fa6 | 2012-01-08 16:22:46 +0100 | [diff] [blame] | 269 | result[i] = Py_TOUPPER((unsigned char) cptr[i]); |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 270 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
| 274 | PyDoc_STRVAR_shared(_Py_title__doc__, |
| 275 | "B.title() -> copy of B\n\ |
| 276 | \n\ |
| 277 | Return a titlecased version of B, i.e. ASCII words start with uppercase\n\ |
| 278 | characters, all remaining cased characters have lowercase."); |
| 279 | |
| 280 | void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 281 | _Py_bytes_title(char *result, const char *s, Py_ssize_t len) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 282 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 283 | Py_ssize_t i; |
| 284 | int previous_is_cased = 0; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 286 | for (i = 0; i < len; i++) { |
| 287 | int c = Py_CHARMASK(*s++); |
| 288 | if (Py_ISLOWER(c)) { |
| 289 | if (!previous_is_cased) |
| 290 | c = Py_TOUPPER(c); |
| 291 | previous_is_cased = 1; |
| 292 | } else if (Py_ISUPPER(c)) { |
| 293 | if (previous_is_cased) |
| 294 | c = Py_TOLOWER(c); |
| 295 | previous_is_cased = 1; |
| 296 | } else |
| 297 | previous_is_cased = 0; |
| 298 | *result++ = c; |
| 299 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | |
| 303 | PyDoc_STRVAR_shared(_Py_capitalize__doc__, |
| 304 | "B.capitalize() -> copy of B\n\ |
| 305 | \n\ |
Senthil Kumaran | e51ee8a | 2010-07-05 12:00:56 +0000 | [diff] [blame] | 306 | Return a copy of B with only its first character capitalized (ASCII)\n\ |
| 307 | and the rest lower-cased."); |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 308 | |
| 309 | void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 310 | _Py_bytes_capitalize(char *result, const char *s, Py_ssize_t len) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 311 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 312 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 314 | if (0 < len) { |
| 315 | int c = Py_CHARMASK(*s++); |
| 316 | if (Py_ISLOWER(c)) |
| 317 | *result = Py_TOUPPER(c); |
| 318 | else |
| 319 | *result = c; |
| 320 | result++; |
| 321 | } |
| 322 | for (i = 1; i < len; i++) { |
| 323 | int c = Py_CHARMASK(*s++); |
| 324 | if (Py_ISUPPER(c)) |
| 325 | *result = Py_TOLOWER(c); |
| 326 | else |
| 327 | *result = c; |
| 328 | result++; |
| 329 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | PyDoc_STRVAR_shared(_Py_swapcase__doc__, |
| 334 | "B.swapcase() -> copy of B\n\ |
| 335 | \n\ |
| 336 | Return a copy of B with uppercase ASCII characters converted\n\ |
| 337 | to lowercase ASCII and vice versa."); |
| 338 | |
| 339 | void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 340 | _Py_bytes_swapcase(char *result, const char *s, Py_ssize_t len) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 341 | { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 342 | Py_ssize_t i; |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 344 | for (i = 0; i < len; i++) { |
| 345 | int c = Py_CHARMASK(*s++); |
| 346 | if (Py_ISLOWER(c)) { |
| 347 | *result = Py_TOUPPER(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | } |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 349 | else if (Py_ISUPPER(c)) { |
| 350 | *result = Py_TOLOWER(c); |
| 351 | } |
| 352 | else |
| 353 | *result = c; |
| 354 | result++; |
| 355 | } |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 358 | |
| 359 | PyDoc_STRVAR_shared(_Py_maketrans__doc__, |
| 360 | "B.maketrans(frm, to) -> translation table\n\ |
| 361 | \n\ |
Senthil Kumaran | 84e3ccc | 2011-06-27 09:06:45 -0700 | [diff] [blame] | 362 | Return a translation table (a bytes object of length 256) suitable\n\ |
| 363 | for use in the bytes or bytearray translate method where each byte\n\ |
| 364 | in frm is mapped to the byte at the same position in to.\n\ |
| 365 | The bytes objects frm and to must be of the same length."); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 366 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 367 | PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 368 | _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 369 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 370 | PyObject *res = NULL; |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 371 | Py_ssize_t i; |
| 372 | char *p; |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 373 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 374 | if (frm->len != to->len) { |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 375 | PyErr_Format(PyExc_ValueError, |
| 376 | "maketrans arguments must have same length"); |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 377 | return NULL; |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 378 | } |
| 379 | res = PyBytes_FromStringAndSize(NULL, 256); |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 380 | if (!res) |
| 381 | return NULL; |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 382 | p = PyBytes_AS_STRING(res); |
| 383 | for (i = 0; i < 256; i++) |
Antoine Pitrou | 47019e5 | 2010-08-15 17:41:31 +0000 | [diff] [blame] | 384 | p[i] = (char) i; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 385 | for (i = 0; i < frm->len; i++) { |
| 386 | p[((unsigned char *)frm->buf)[i]] = ((char *)to->buf)[i]; |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 387 | } |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | 9b49192 | 2010-08-15 17:38:46 +0000 | [diff] [blame] | 389 | return res; |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 390 | } |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 391 | |
| 392 | #define FASTSEARCH fastsearch |
| 393 | #define STRINGLIB(F) stringlib_##F |
| 394 | #define STRINGLIB_CHAR char |
| 395 | #define STRINGLIB_SIZEOF_CHAR 1 |
| 396 | |
| 397 | #include "stringlib/fastsearch.h" |
| 398 | #include "stringlib/count.h" |
| 399 | #include "stringlib/find.h" |
| 400 | |
| 401 | /* |
Oren Milman | 0042510 | 2017-03-13 00:37:05 +0200 | [diff] [blame] | 402 | Wraps stringlib_parse_args_finds() and additionally checks the first |
| 403 | argument type. |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 404 | |
Oren Milman | 0042510 | 2017-03-13 00:37:05 +0200 | [diff] [blame] | 405 | In case the first argument is a bytes-like object, sets it to subobj, |
| 406 | and doesn't touch the byte parameter. |
| 407 | In case it is an integer in range(0, 256), writes the integer value |
| 408 | to byte, and sets subobj to NULL. |
| 409 | |
| 410 | The other parameters are similar to those of |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 411 | stringlib_parse_args_finds(). |
| 412 | */ |
| 413 | |
| 414 | Py_LOCAL_INLINE(int) |
| 415 | parse_args_finds_byte(const char *function_name, PyObject *args, |
| 416 | PyObject **subobj, char *byte, |
| 417 | Py_ssize_t *start, Py_ssize_t *end) |
| 418 | { |
| 419 | PyObject *tmp_subobj; |
| 420 | Py_ssize_t ival; |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 421 | |
| 422 | if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj, |
| 423 | start, end)) |
| 424 | return 0; |
| 425 | |
Oren Milman | 0042510 | 2017-03-13 00:37:05 +0200 | [diff] [blame] | 426 | if (PyObject_CheckBuffer(tmp_subobj)) { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 427 | *subobj = tmp_subobj; |
| 428 | return 1; |
| 429 | } |
| 430 | |
Oren Milman | 0042510 | 2017-03-13 00:37:05 +0200 | [diff] [blame] | 431 | if (!PyIndex_Check(tmp_subobj)) { |
| 432 | PyErr_Format(PyExc_TypeError, |
| 433 | "argument should be integer or bytes-like object, " |
| 434 | "not '%.200s'", |
| 435 | Py_TYPE(tmp_subobj)->tp_name); |
| 436 | return 0; |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 437 | } |
| 438 | |
Oren Milman | 0042510 | 2017-03-13 00:37:05 +0200 | [diff] [blame] | 439 | ival = PyNumber_AsSsize_t(tmp_subobj, NULL); |
| 440 | if (ival == -1 && PyErr_Occurred()) { |
| 441 | return 0; |
| 442 | } |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 443 | if (ival < 0 || ival > 255) { |
| 444 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | *subobj = NULL; |
| 449 | *byte = (char)ival; |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | /* helper macro to fixup start/end slice values */ |
| 454 | #define ADJUST_INDICES(start, end, len) \ |
| 455 | if (end > len) \ |
| 456 | end = len; \ |
| 457 | else if (end < 0) { \ |
| 458 | end += len; \ |
| 459 | if (end < 0) \ |
| 460 | end = 0; \ |
| 461 | } \ |
| 462 | if (start < 0) { \ |
| 463 | start += len; \ |
| 464 | if (start < 0) \ |
| 465 | start = 0; \ |
| 466 | } |
| 467 | |
| 468 | Py_LOCAL_INLINE(Py_ssize_t) |
| 469 | find_internal(const char *str, Py_ssize_t len, |
| 470 | const char *function_name, PyObject *args, int dir) |
| 471 | { |
| 472 | PyObject *subobj; |
| 473 | char byte; |
| 474 | Py_buffer subbuf; |
| 475 | const char *sub; |
| 476 | Py_ssize_t sub_len; |
| 477 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
| 478 | Py_ssize_t res; |
| 479 | |
| 480 | if (!parse_args_finds_byte(function_name, args, |
| 481 | &subobj, &byte, &start, &end)) |
| 482 | return -2; |
| 483 | |
| 484 | if (subobj) { |
| 485 | if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0) |
| 486 | return -2; |
| 487 | |
| 488 | sub = subbuf.buf; |
| 489 | sub_len = subbuf.len; |
| 490 | } |
| 491 | else { |
| 492 | sub = &byte; |
| 493 | sub_len = 1; |
| 494 | } |
| 495 | |
| 496 | ADJUST_INDICES(start, end, len); |
| 497 | if (end - start < sub_len) |
| 498 | res = -1; |
| 499 | else if (sub_len == 1) { |
| 500 | if (dir > 0) |
| 501 | res = stringlib_find_char( |
| 502 | str + start, end - start, |
| 503 | *sub); |
| 504 | else |
| 505 | res = stringlib_rfind_char( |
| 506 | str + start, end - start, |
| 507 | *sub); |
| 508 | if (res >= 0) |
| 509 | res += start; |
| 510 | } |
| 511 | else { |
| 512 | if (dir > 0) |
| 513 | res = stringlib_find_slice( |
| 514 | str, len, |
| 515 | sub, sub_len, start, end); |
| 516 | else |
| 517 | res = stringlib_rfind_slice( |
| 518 | str, len, |
| 519 | sub, sub_len, start, end); |
| 520 | } |
| 521 | |
| 522 | if (subobj) |
| 523 | PyBuffer_Release(&subbuf); |
| 524 | |
| 525 | return res; |
| 526 | } |
| 527 | |
| 528 | PyDoc_STRVAR_shared(_Py_find__doc__, |
| 529 | "B.find(sub[, start[, end]]) -> int\n\ |
| 530 | \n\ |
| 531 | Return the lowest index in B where subsection sub is found,\n\ |
| 532 | such that sub is contained within B[start,end]. Optional\n\ |
| 533 | arguments start and end are interpreted as in slice notation.\n\ |
| 534 | \n\ |
| 535 | Return -1 on failure."); |
| 536 | |
| 537 | PyObject * |
| 538 | _Py_bytes_find(const char *str, Py_ssize_t len, PyObject *args) |
| 539 | { |
| 540 | Py_ssize_t result = find_internal(str, len, "find", args, +1); |
| 541 | if (result == -2) |
| 542 | return NULL; |
| 543 | return PyLong_FromSsize_t(result); |
| 544 | } |
| 545 | |
| 546 | PyDoc_STRVAR_shared(_Py_index__doc__, |
| 547 | "B.index(sub[, start[, end]]) -> int\n\ |
| 548 | \n\ |
Lisa Roach | 43ba886 | 2017-04-04 22:36:22 -0700 | [diff] [blame] | 549 | Return the lowest index in B where subsection sub is found,\n\ |
| 550 | such that sub is contained within B[start,end]. Optional\n\ |
| 551 | arguments start and end are interpreted as in slice notation.\n\ |
| 552 | \n\ |
| 553 | Raises ValueError when the subsection is not found."); |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 554 | |
| 555 | PyObject * |
| 556 | _Py_bytes_index(const char *str, Py_ssize_t len, PyObject *args) |
| 557 | { |
| 558 | Py_ssize_t result = find_internal(str, len, "index", args, +1); |
| 559 | if (result == -2) |
| 560 | return NULL; |
| 561 | if (result == -1) { |
| 562 | PyErr_SetString(PyExc_ValueError, |
| 563 | "subsection not found"); |
| 564 | return NULL; |
| 565 | } |
| 566 | return PyLong_FromSsize_t(result); |
| 567 | } |
| 568 | |
| 569 | PyDoc_STRVAR_shared(_Py_rfind__doc__, |
| 570 | "B.rfind(sub[, start[, end]]) -> int\n\ |
| 571 | \n\ |
| 572 | Return the highest index in B where subsection sub is found,\n\ |
| 573 | such that sub is contained within B[start,end]. Optional\n\ |
| 574 | arguments start and end are interpreted as in slice notation.\n\ |
| 575 | \n\ |
| 576 | Return -1 on failure."); |
| 577 | |
| 578 | PyObject * |
| 579 | _Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *args) |
| 580 | { |
| 581 | Py_ssize_t result = find_internal(str, len, "rfind", args, -1); |
| 582 | if (result == -2) |
| 583 | return NULL; |
| 584 | return PyLong_FromSsize_t(result); |
| 585 | } |
| 586 | |
| 587 | PyDoc_STRVAR_shared(_Py_rindex__doc__, |
| 588 | "B.rindex(sub[, start[, end]]) -> int\n\ |
| 589 | \n\ |
Lisa Roach | 43ba886 | 2017-04-04 22:36:22 -0700 | [diff] [blame] | 590 | Return the highest index in B where subsection sub is found,\n\ |
| 591 | such that sub is contained within B[start,end]. Optional\n\ |
| 592 | arguments start and end are interpreted as in slice notation.\n\ |
| 593 | \n\ |
| 594 | Raise ValueError when the subsection is not found."); |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 595 | |
| 596 | PyObject * |
| 597 | _Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *args) |
| 598 | { |
| 599 | Py_ssize_t result = find_internal(str, len, "rindex", args, -1); |
| 600 | if (result == -2) |
| 601 | return NULL; |
| 602 | if (result == -1) { |
| 603 | PyErr_SetString(PyExc_ValueError, |
| 604 | "subsection not found"); |
| 605 | return NULL; |
| 606 | } |
| 607 | return PyLong_FromSsize_t(result); |
| 608 | } |
| 609 | |
| 610 | PyDoc_STRVAR_shared(_Py_count__doc__, |
| 611 | "B.count(sub[, start[, end]]) -> int\n\ |
| 612 | \n\ |
| 613 | Return the number of non-overlapping occurrences of subsection sub in\n\ |
| 614 | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
| 615 | as in slice notation."); |
| 616 | |
| 617 | PyObject * |
| 618 | _Py_bytes_count(const char *str, Py_ssize_t len, PyObject *args) |
| 619 | { |
| 620 | PyObject *sub_obj; |
| 621 | const char *sub; |
| 622 | Py_ssize_t sub_len; |
| 623 | char byte; |
| 624 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
| 625 | |
| 626 | Py_buffer vsub; |
| 627 | PyObject *count_obj; |
| 628 | |
| 629 | if (!parse_args_finds_byte("count", args, |
| 630 | &sub_obj, &byte, &start, &end)) |
| 631 | return NULL; |
| 632 | |
| 633 | if (sub_obj) { |
| 634 | if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0) |
| 635 | return NULL; |
| 636 | |
| 637 | sub = vsub.buf; |
| 638 | sub_len = vsub.len; |
| 639 | } |
| 640 | else { |
| 641 | sub = &byte; |
| 642 | sub_len = 1; |
| 643 | } |
| 644 | |
| 645 | ADJUST_INDICES(start, end, len); |
| 646 | |
| 647 | count_obj = PyLong_FromSsize_t( |
| 648 | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
| 649 | ); |
| 650 | |
| 651 | if (sub_obj) |
| 652 | PyBuffer_Release(&vsub); |
| 653 | |
| 654 | return count_obj; |
| 655 | } |
| 656 | |
| 657 | int |
| 658 | _Py_bytes_contains(const char *str, Py_ssize_t len, PyObject *arg) |
| 659 | { |
Serhiy Storchaka | f9efb8b | 2016-07-10 12:37:30 +0300 | [diff] [blame] | 660 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, NULL); |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 661 | if (ival == -1 && PyErr_Occurred()) { |
| 662 | Py_buffer varg; |
| 663 | Py_ssize_t pos; |
| 664 | PyErr_Clear(); |
| 665 | if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) |
| 666 | return -1; |
| 667 | pos = stringlib_find(str, len, |
| 668 | varg.buf, varg.len, 0); |
| 669 | PyBuffer_Release(&varg); |
| 670 | return pos >= 0; |
| 671 | } |
| 672 | if (ival < 0 || ival >= 256) { |
| 673 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 674 | return -1; |
| 675 | } |
| 676 | |
| 677 | return memchr(str, (int) ival, len) != NULL; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* Matches the end (direction >= 0) or start (direction < 0) of the buffer |
| 682 | * against substr, using the start and end arguments. Returns |
| 683 | * -1 on error, 0 if not found and 1 if found. |
| 684 | */ |
Benjamin Peterson | 621b430 | 2016-09-09 13:54:34 -0700 | [diff] [blame] | 685 | static int |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 686 | tailmatch(const char *str, Py_ssize_t len, PyObject *substr, |
| 687 | Py_ssize_t start, Py_ssize_t end, int direction) |
| 688 | { |
| 689 | Py_buffer sub_view = {NULL, NULL}; |
| 690 | const char *sub; |
| 691 | Py_ssize_t slen; |
| 692 | |
| 693 | if (PyBytes_Check(substr)) { |
| 694 | sub = PyBytes_AS_STRING(substr); |
| 695 | slen = PyBytes_GET_SIZE(substr); |
| 696 | } |
| 697 | else { |
| 698 | if (PyObject_GetBuffer(substr, &sub_view, PyBUF_SIMPLE) != 0) |
| 699 | return -1; |
| 700 | sub = sub_view.buf; |
| 701 | slen = sub_view.len; |
| 702 | } |
| 703 | |
| 704 | ADJUST_INDICES(start, end, len); |
| 705 | |
| 706 | if (direction < 0) { |
| 707 | /* startswith */ |
| 708 | if (start + slen > len) |
| 709 | goto notfound; |
| 710 | } else { |
| 711 | /* endswith */ |
| 712 | if (end - start < slen || start > len) |
| 713 | goto notfound; |
| 714 | |
| 715 | if (end - slen > start) |
| 716 | start = end - slen; |
| 717 | } |
| 718 | if (end - start < slen) |
| 719 | goto notfound; |
| 720 | if (memcmp(str + start, sub, slen) != 0) |
| 721 | goto notfound; |
| 722 | |
| 723 | PyBuffer_Release(&sub_view); |
| 724 | return 1; |
| 725 | |
| 726 | notfound: |
| 727 | PyBuffer_Release(&sub_view); |
| 728 | return 0; |
| 729 | } |
| 730 | |
Benjamin Peterson | 621b430 | 2016-09-09 13:54:34 -0700 | [diff] [blame] | 731 | static PyObject * |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 732 | _Py_bytes_tailmatch(const char *str, Py_ssize_t len, |
| 733 | const char *function_name, PyObject *args, |
| 734 | int direction) |
| 735 | { |
| 736 | Py_ssize_t start = 0; |
| 737 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 738 | PyObject *subobj; |
| 739 | int result; |
| 740 | |
| 741 | if (!stringlib_parse_args_finds(function_name, args, &subobj, &start, &end)) |
| 742 | return NULL; |
| 743 | if (PyTuple_Check(subobj)) { |
| 744 | Py_ssize_t i; |
| 745 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 746 | result = tailmatch(str, len, PyTuple_GET_ITEM(subobj, i), |
| 747 | start, end, direction); |
| 748 | if (result == -1) |
| 749 | return NULL; |
| 750 | else if (result) { |
| 751 | Py_RETURN_TRUE; |
| 752 | } |
| 753 | } |
| 754 | Py_RETURN_FALSE; |
| 755 | } |
| 756 | result = tailmatch(str, len, subobj, start, end, direction); |
| 757 | if (result == -1) { |
| 758 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 759 | PyErr_Format(PyExc_TypeError, |
| 760 | "%s first arg must be bytes or a tuple of bytes, " |
| 761 | "not %s", |
| 762 | function_name, Py_TYPE(subobj)->tp_name); |
| 763 | return NULL; |
| 764 | } |
| 765 | else |
| 766 | return PyBool_FromLong(result); |
| 767 | } |
| 768 | |
| 769 | PyDoc_STRVAR_shared(_Py_startswith__doc__, |
| 770 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
| 771 | \n\ |
| 772 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 773 | With optional start, test B beginning at that position.\n\ |
| 774 | With optional end, stop comparing B at that position.\n\ |
| 775 | prefix can also be a tuple of bytes to try."); |
| 776 | |
| 777 | PyObject * |
| 778 | _Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *args) |
| 779 | { |
| 780 | return _Py_bytes_tailmatch(str, len, "startswith", args, -1); |
| 781 | } |
| 782 | |
| 783 | PyDoc_STRVAR_shared(_Py_endswith__doc__, |
| 784 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
| 785 | \n\ |
| 786 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 787 | With optional start, test B beginning at that position.\n\ |
| 788 | With optional end, stop comparing B at that position.\n\ |
| 789 | suffix can also be a tuple of bytes to try."); |
| 790 | |
| 791 | PyObject * |
| 792 | _Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *args) |
| 793 | { |
| 794 | return _Py_bytes_tailmatch(str, len, "endswith", args, +1); |
| 795 | } |
| 796 | |
| 797 | PyDoc_STRVAR_shared(_Py_expandtabs__doc__, |
| 798 | "B.expandtabs(tabsize=8) -> copy of B\n\ |
| 799 | \n\ |
| 800 | Return a copy of B where all tab characters are expanded using spaces.\n\ |
| 801 | If tabsize is not given, a tab size of 8 characters is assumed."); |
| 802 | |
| 803 | PyDoc_STRVAR_shared(_Py_ljust__doc__, |
| 804 | "B.ljust(width[, fillchar]) -> copy of B\n" |
| 805 | "\n" |
| 806 | "Return B left justified in a string of length width. Padding is\n" |
| 807 | "done using the specified fill character (default is a space)."); |
| 808 | |
| 809 | PyDoc_STRVAR_shared(_Py_rjust__doc__, |
| 810 | "B.rjust(width[, fillchar]) -> copy of B\n" |
| 811 | "\n" |
| 812 | "Return B right justified in a string of length width. Padding is\n" |
| 813 | "done using the specified fill character (default is a space)"); |
| 814 | |
| 815 | PyDoc_STRVAR_shared(_Py_center__doc__, |
| 816 | "B.center(width[, fillchar]) -> copy of B\n" |
| 817 | "\n" |
| 818 | "Return B centered in a string of length width. Padding is\n" |
| 819 | "done using the specified fill character (default is a space)."); |
| 820 | |
| 821 | PyDoc_STRVAR_shared(_Py_zfill__doc__, |
| 822 | "B.zfill(width) -> copy of B\n" |
| 823 | "\n" |
| 824 | "Pad a numeric string B with zeros on the left, to fill a field\n" |
| 825 | "of the specified width. B is never truncated."); |