bytes_methods.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. #define PY_SSIZE_T_CLEAN
  2. #include "Python.h"
  3. #include "pycore_abstract.h" // _PyIndex_Check()
  4. #include "pycore_bytes_methods.h"
  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. PyObject*
  11. _Py_bytes_isspace(const char *cptr, Py_ssize_t len)
  12. {
  13. const unsigned char *p
  14. = (const unsigned char *) cptr;
  15. const unsigned char *e;
  16. /* Shortcut for single character strings */
  17. if (len == 1 && Py_ISSPACE(*p))
  18. Py_RETURN_TRUE;
  19. /* Special case for empty strings */
  20. if (len == 0)
  21. Py_RETURN_FALSE;
  22. e = p + len;
  23. for (; p < e; p++) {
  24. if (!Py_ISSPACE(*p))
  25. Py_RETURN_FALSE;
  26. }
  27. Py_RETURN_TRUE;
  28. }
  29. PyDoc_STRVAR_shared(_Py_isalpha__doc__,
  30. "B.isalpha() -> bool\n\
  31. \n\
  32. Return True if all characters in B are alphabetic\n\
  33. and there is at least one character in B, False otherwise.");
  34. PyObject*
  35. _Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
  36. {
  37. const unsigned char *p
  38. = (const unsigned char *) cptr;
  39. const unsigned char *e;
  40. /* Shortcut for single character strings */
  41. if (len == 1 && Py_ISALPHA(*p))
  42. Py_RETURN_TRUE;
  43. /* Special case for empty strings */
  44. if (len == 0)
  45. Py_RETURN_FALSE;
  46. e = p + len;
  47. for (; p < e; p++) {
  48. if (!Py_ISALPHA(*p))
  49. Py_RETURN_FALSE;
  50. }
  51. Py_RETURN_TRUE;
  52. }
  53. PyDoc_STRVAR_shared(_Py_isalnum__doc__,
  54. "B.isalnum() -> bool\n\
  55. \n\
  56. Return True if all characters in B are alphanumeric\n\
  57. and there is at least one character in B, False otherwise.");
  58. PyObject*
  59. _Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
  60. {
  61. const unsigned char *p
  62. = (const unsigned char *) cptr;
  63. const unsigned char *e;
  64. /* Shortcut for single character strings */
  65. if (len == 1 && Py_ISALNUM(*p))
  66. Py_RETURN_TRUE;
  67. /* Special case for empty strings */
  68. if (len == 0)
  69. Py_RETURN_FALSE;
  70. e = p + len;
  71. for (; p < e; p++) {
  72. if (!Py_ISALNUM(*p))
  73. Py_RETURN_FALSE;
  74. }
  75. Py_RETURN_TRUE;
  76. }
  77. PyDoc_STRVAR_shared(_Py_isascii__doc__,
  78. "B.isascii() -> bool\n\
  79. \n\
  80. Return True if B is empty or all characters in B are ASCII,\n\
  81. False otherwise.");
  82. // Optimization is copied from ascii_decode in unicodeobject.c
  83. /* Mask to quickly check whether a C 'size_t' contains a
  84. non-ASCII, UTF8-encoded char. */
  85. #if (SIZEOF_SIZE_T == 8)
  86. # define ASCII_CHAR_MASK 0x8080808080808080ULL
  87. #elif (SIZEOF_SIZE_T == 4)
  88. # define ASCII_CHAR_MASK 0x80808080U
  89. #else
  90. # error C 'size_t' size should be either 4 or 8!
  91. #endif
  92. PyObject*
  93. _Py_bytes_isascii(const char *cptr, Py_ssize_t len)
  94. {
  95. const char *p = cptr;
  96. const char *end = p + len;
  97. while (p < end) {
  98. /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h
  99. for an explanation. */
  100. if (_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) {
  101. /* Help allocation */
  102. const char *_p = p;
  103. while (_p + SIZEOF_SIZE_T <= end) {
  104. size_t value = *(const size_t *) _p;
  105. if (value & ASCII_CHAR_MASK) {
  106. Py_RETURN_FALSE;
  107. }
  108. _p += SIZEOF_SIZE_T;
  109. }
  110. p = _p;
  111. if (_p == end)
  112. break;
  113. }
  114. if ((unsigned char)*p & 0x80) {
  115. Py_RETURN_FALSE;
  116. }
  117. p++;
  118. }
  119. Py_RETURN_TRUE;
  120. }
  121. #undef ASCII_CHAR_MASK
  122. PyDoc_STRVAR_shared(_Py_isdigit__doc__,
  123. "B.isdigit() -> bool\n\
  124. \n\
  125. Return True if all characters in B are digits\n\
  126. and there is at least one character in B, False otherwise.");
  127. PyObject*
  128. _Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
  129. {
  130. const unsigned char *p
  131. = (const unsigned char *) cptr;
  132. const unsigned char *e;
  133. /* Shortcut for single character strings */
  134. if (len == 1 && Py_ISDIGIT(*p))
  135. Py_RETURN_TRUE;
  136. /* Special case for empty strings */
  137. if (len == 0)
  138. Py_RETURN_FALSE;
  139. e = p + len;
  140. for (; p < e; p++) {
  141. if (!Py_ISDIGIT(*p))
  142. Py_RETURN_FALSE;
  143. }
  144. Py_RETURN_TRUE;
  145. }
  146. PyDoc_STRVAR_shared(_Py_islower__doc__,
  147. "B.islower() -> bool\n\
  148. \n\
  149. Return True if all cased characters in B are lowercase and there is\n\
  150. at least one cased character in B, False otherwise.");
  151. PyObject*
  152. _Py_bytes_islower(const char *cptr, Py_ssize_t len)
  153. {
  154. const unsigned char *p
  155. = (const unsigned char *) cptr;
  156. const unsigned char *e;
  157. int cased;
  158. /* Shortcut for single character strings */
  159. if (len == 1)
  160. return PyBool_FromLong(Py_ISLOWER(*p));
  161. /* Special case for empty strings */
  162. if (len == 0)
  163. Py_RETURN_FALSE;
  164. e = p + len;
  165. cased = 0;
  166. for (; p < e; p++) {
  167. if (Py_ISUPPER(*p))
  168. Py_RETURN_FALSE;
  169. else if (!cased && Py_ISLOWER(*p))
  170. cased = 1;
  171. }
  172. return PyBool_FromLong(cased);
  173. }
  174. PyDoc_STRVAR_shared(_Py_isupper__doc__,
  175. "B.isupper() -> bool\n\
  176. \n\
  177. Return True if all cased characters in B are uppercase and there is\n\
  178. at least one cased character in B, False otherwise.");
  179. PyObject*
  180. _Py_bytes_isupper(const char *cptr, Py_ssize_t len)
  181. {
  182. const unsigned char *p
  183. = (const unsigned char *) cptr;
  184. const unsigned char *e;
  185. int cased;
  186. /* Shortcut for single character strings */
  187. if (len == 1)
  188. return PyBool_FromLong(Py_ISUPPER(*p));
  189. /* Special case for empty strings */
  190. if (len == 0)
  191. Py_RETURN_FALSE;
  192. e = p + len;
  193. cased = 0;
  194. for (; p < e; p++) {
  195. if (Py_ISLOWER(*p))
  196. Py_RETURN_FALSE;
  197. else if (!cased && Py_ISUPPER(*p))
  198. cased = 1;
  199. }
  200. return PyBool_FromLong(cased);
  201. }
  202. PyDoc_STRVAR_shared(_Py_istitle__doc__,
  203. "B.istitle() -> bool\n\
  204. \n\
  205. Return True if B is a titlecased string and there is at least one\n\
  206. character in B, i.e. uppercase characters may only follow uncased\n\
  207. characters and lowercase characters only cased ones. Return False\n\
  208. otherwise.");
  209. PyObject*
  210. _Py_bytes_istitle(const char *cptr, Py_ssize_t len)
  211. {
  212. const unsigned char *p
  213. = (const unsigned char *) cptr;
  214. const unsigned char *e;
  215. int cased, previous_is_cased;
  216. if (len == 1) {
  217. if (Py_ISUPPER(*p)) {
  218. Py_RETURN_TRUE;
  219. }
  220. Py_RETURN_FALSE;
  221. }
  222. /* Special case for empty strings */
  223. if (len == 0)
  224. Py_RETURN_FALSE;
  225. e = p + len;
  226. cased = 0;
  227. previous_is_cased = 0;
  228. for (; p < e; p++) {
  229. const unsigned char ch = *p;
  230. if (Py_ISUPPER(ch)) {
  231. if (previous_is_cased)
  232. Py_RETURN_FALSE;
  233. previous_is_cased = 1;
  234. cased = 1;
  235. }
  236. else if (Py_ISLOWER(ch)) {
  237. if (!previous_is_cased)
  238. Py_RETURN_FALSE;
  239. previous_is_cased = 1;
  240. cased = 1;
  241. }
  242. else
  243. previous_is_cased = 0;
  244. }
  245. return PyBool_FromLong(cased);
  246. }
  247. PyDoc_STRVAR_shared(_Py_lower__doc__,
  248. "B.lower() -> copy of B\n\
  249. \n\
  250. Return a copy of B with all ASCII characters converted to lowercase.");
  251. void
  252. _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
  253. {
  254. Py_ssize_t i;
  255. for (i = 0; i < len; i++) {
  256. result[i] = Py_TOLOWER((unsigned char) cptr[i]);
  257. }
  258. }
  259. PyDoc_STRVAR_shared(_Py_upper__doc__,
  260. "B.upper() -> copy of B\n\
  261. \n\
  262. Return a copy of B with all ASCII characters converted to uppercase.");
  263. void
  264. _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
  265. {
  266. Py_ssize_t i;
  267. for (i = 0; i < len; i++) {
  268. result[i] = Py_TOUPPER((unsigned char) cptr[i]);
  269. }
  270. }
  271. PyDoc_STRVAR_shared(_Py_title__doc__,
  272. "B.title() -> copy of B\n\
  273. \n\
  274. Return a titlecased version of B, i.e. ASCII words start with uppercase\n\
  275. characters, all remaining cased characters have lowercase.");
  276. void
  277. _Py_bytes_title(char *result, const char *s, Py_ssize_t len)
  278. {
  279. Py_ssize_t i;
  280. int previous_is_cased = 0;
  281. for (i = 0; i < len; i++) {
  282. int c = Py_CHARMASK(*s++);
  283. if (Py_ISLOWER(c)) {
  284. if (!previous_is_cased)
  285. c = Py_TOUPPER(c);
  286. previous_is_cased = 1;
  287. } else if (Py_ISUPPER(c)) {
  288. if (previous_is_cased)
  289. c = Py_TOLOWER(c);
  290. previous_is_cased = 1;
  291. } else
  292. previous_is_cased = 0;
  293. *result++ = c;
  294. }
  295. }
  296. PyDoc_STRVAR_shared(_Py_capitalize__doc__,
  297. "B.capitalize() -> copy of B\n\
  298. \n\
  299. Return a copy of B with only its first character capitalized (ASCII)\n\
  300. and the rest lower-cased.");
  301. void
  302. _Py_bytes_capitalize(char *result, const char *s, Py_ssize_t len)
  303. {
  304. if (len > 0) {
  305. *result = Py_TOUPPER(*s);
  306. _Py_bytes_lower(result + 1, s + 1, len - 1);
  307. }
  308. }
  309. PyDoc_STRVAR_shared(_Py_swapcase__doc__,
  310. "B.swapcase() -> copy of B\n\
  311. \n\
  312. Return a copy of B with uppercase ASCII characters converted\n\
  313. to lowercase ASCII and vice versa.");
  314. void
  315. _Py_bytes_swapcase(char *result, const char *s, Py_ssize_t len)
  316. {
  317. Py_ssize_t i;
  318. for (i = 0; i < len; i++) {
  319. int c = Py_CHARMASK(*s++);
  320. if (Py_ISLOWER(c)) {
  321. *result = Py_TOUPPER(c);
  322. }
  323. else if (Py_ISUPPER(c)) {
  324. *result = Py_TOLOWER(c);
  325. }
  326. else
  327. *result = c;
  328. result++;
  329. }
  330. }
  331. PyDoc_STRVAR_shared(_Py_maketrans__doc__,
  332. "B.maketrans(frm, to) -> translation table\n\
  333. \n\
  334. Return a translation table (a bytes object of length 256) suitable\n\
  335. for use in the bytes or bytearray translate method where each byte\n\
  336. in frm is mapped to the byte at the same position in to.\n\
  337. The bytes objects frm and to must be of the same length.");
  338. PyObject *
  339. _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to)
  340. {
  341. PyObject *res = NULL;
  342. Py_ssize_t i;
  343. char *p;
  344. if (frm->len != to->len) {
  345. PyErr_Format(PyExc_ValueError,
  346. "maketrans arguments must have same length");
  347. return NULL;
  348. }
  349. res = PyBytes_FromStringAndSize(NULL, 256);
  350. if (!res)
  351. return NULL;
  352. p = PyBytes_AS_STRING(res);
  353. for (i = 0; i < 256; i++)
  354. p[i] = (char) i;
  355. for (i = 0; i < frm->len; i++) {
  356. p[((unsigned char *)frm->buf)[i]] = ((char *)to->buf)[i];
  357. }
  358. return res;
  359. }
  360. #define FASTSEARCH fastsearch
  361. #define STRINGLIB(F) stringlib_##F
  362. #define STRINGLIB_CHAR char
  363. #define STRINGLIB_SIZEOF_CHAR 1
  364. #define STRINGLIB_FAST_MEMCHR memchr
  365. #include "stringlib/fastsearch.h"
  366. #include "stringlib/count.h"
  367. #include "stringlib/find.h"
  368. /*
  369. Wraps stringlib_parse_args_finds() and additionally checks the first
  370. argument type.
  371. In case the first argument is a bytes-like object, sets it to subobj,
  372. and doesn't touch the byte parameter.
  373. In case it is an integer in range(0, 256), writes the integer value
  374. to byte, and sets subobj to NULL.
  375. The other parameters are similar to those of
  376. stringlib_parse_args_finds().
  377. */
  378. Py_LOCAL_INLINE(int)
  379. parse_args_finds_byte(const char *function_name, PyObject *args,
  380. PyObject **subobj, char *byte,
  381. Py_ssize_t *start, Py_ssize_t *end)
  382. {
  383. PyObject *tmp_subobj;
  384. Py_ssize_t ival;
  385. if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj,
  386. start, end))
  387. return 0;
  388. if (PyObject_CheckBuffer(tmp_subobj)) {
  389. *subobj = tmp_subobj;
  390. return 1;
  391. }
  392. if (!_PyIndex_Check(tmp_subobj)) {
  393. PyErr_Format(PyExc_TypeError,
  394. "argument should be integer or bytes-like object, "
  395. "not '%.200s'",
  396. Py_TYPE(tmp_subobj)->tp_name);
  397. return 0;
  398. }
  399. ival = PyNumber_AsSsize_t(tmp_subobj, NULL);
  400. if (ival == -1 && PyErr_Occurred()) {
  401. return 0;
  402. }
  403. if (ival < 0 || ival > 255) {
  404. PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
  405. return 0;
  406. }
  407. *subobj = NULL;
  408. *byte = (char)ival;
  409. return 1;
  410. }
  411. /* helper macro to fixup start/end slice values */
  412. #define ADJUST_INDICES(start, end, len) \
  413. if (end > len) \
  414. end = len; \
  415. else if (end < 0) { \
  416. end += len; \
  417. if (end < 0) \
  418. end = 0; \
  419. } \
  420. if (start < 0) { \
  421. start += len; \
  422. if (start < 0) \
  423. start = 0; \
  424. }
  425. Py_LOCAL_INLINE(Py_ssize_t)
  426. find_internal(const char *str, Py_ssize_t len,
  427. const char *function_name, PyObject *args, int dir)
  428. {
  429. PyObject *subobj;
  430. char byte;
  431. Py_buffer subbuf;
  432. const char *sub;
  433. Py_ssize_t sub_len;
  434. Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
  435. Py_ssize_t res;
  436. if (!parse_args_finds_byte(function_name, args,
  437. &subobj, &byte, &start, &end))
  438. return -2;
  439. if (subobj) {
  440. if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
  441. return -2;
  442. sub = subbuf.buf;
  443. sub_len = subbuf.len;
  444. }
  445. else {
  446. sub = &byte;
  447. sub_len = 1;
  448. }
  449. ADJUST_INDICES(start, end, len);
  450. if (end - start < sub_len)
  451. res = -1;
  452. else if (sub_len == 1) {
  453. if (dir > 0)
  454. res = stringlib_find_char(
  455. str + start, end - start,
  456. *sub);
  457. else
  458. res = stringlib_rfind_char(
  459. str + start, end - start,
  460. *sub);
  461. if (res >= 0)
  462. res += start;
  463. }
  464. else {
  465. if (dir > 0)
  466. res = stringlib_find_slice(
  467. str, len,
  468. sub, sub_len, start, end);
  469. else
  470. res = stringlib_rfind_slice(
  471. str, len,
  472. sub, sub_len, start, end);
  473. }
  474. if (subobj)
  475. PyBuffer_Release(&subbuf);
  476. return res;
  477. }
  478. PyDoc_STRVAR_shared(_Py_find__doc__,
  479. "B.find(sub[, start[, end]]) -> int\n\
  480. \n\
  481. Return the lowest index in B where subsection sub is found,\n\
  482. such that sub is contained within B[start,end]. Optional\n\
  483. arguments start and end are interpreted as in slice notation.\n\
  484. \n\
  485. Return -1 on failure.");
  486. PyObject *
  487. _Py_bytes_find(const char *str, Py_ssize_t len, PyObject *args)
  488. {
  489. Py_ssize_t result = find_internal(str, len, "find", args, +1);
  490. if (result == -2)
  491. return NULL;
  492. return PyLong_FromSsize_t(result);
  493. }
  494. PyDoc_STRVAR_shared(_Py_index__doc__,
  495. "B.index(sub[, start[, end]]) -> int\n\
  496. \n\
  497. Return the lowest index in B where subsection sub is found,\n\
  498. such that sub is contained within B[start,end]. Optional\n\
  499. arguments start and end are interpreted as in slice notation.\n\
  500. \n\
  501. Raises ValueError when the subsection is not found.");
  502. PyObject *
  503. _Py_bytes_index(const char *str, Py_ssize_t len, PyObject *args)
  504. {
  505. Py_ssize_t result = find_internal(str, len, "index", args, +1);
  506. if (result == -2)
  507. return NULL;
  508. if (result == -1) {
  509. PyErr_SetString(PyExc_ValueError,
  510. "subsection not found");
  511. return NULL;
  512. }
  513. return PyLong_FromSsize_t(result);
  514. }
  515. PyDoc_STRVAR_shared(_Py_rfind__doc__,
  516. "B.rfind(sub[, start[, end]]) -> int\n\
  517. \n\
  518. Return the highest index in B where subsection sub is found,\n\
  519. such that sub is contained within B[start,end]. Optional\n\
  520. arguments start and end are interpreted as in slice notation.\n\
  521. \n\
  522. Return -1 on failure.");
  523. PyObject *
  524. _Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *args)
  525. {
  526. Py_ssize_t result = find_internal(str, len, "rfind", args, -1);
  527. if (result == -2)
  528. return NULL;
  529. return PyLong_FromSsize_t(result);
  530. }
  531. PyDoc_STRVAR_shared(_Py_rindex__doc__,
  532. "B.rindex(sub[, start[, end]]) -> int\n\
  533. \n\
  534. Return the highest index in B where subsection sub is found,\n\
  535. such that sub is contained within B[start,end]. Optional\n\
  536. arguments start and end are interpreted as in slice notation.\n\
  537. \n\
  538. Raise ValueError when the subsection is not found.");
  539. PyObject *
  540. _Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *args)
  541. {
  542. Py_ssize_t result = find_internal(str, len, "rindex", args, -1);
  543. if (result == -2)
  544. return NULL;
  545. if (result == -1) {
  546. PyErr_SetString(PyExc_ValueError,
  547. "subsection not found");
  548. return NULL;
  549. }
  550. return PyLong_FromSsize_t(result);
  551. }
  552. PyDoc_STRVAR_shared(_Py_count__doc__,
  553. "B.count(sub[, start[, end]]) -> int\n\
  554. \n\
  555. Return the number of non-overlapping occurrences of subsection sub in\n\
  556. bytes B[start:end]. Optional arguments start and end are interpreted\n\
  557. as in slice notation.");
  558. PyObject *
  559. _Py_bytes_count(const char *str, Py_ssize_t len, PyObject *args)
  560. {
  561. PyObject *sub_obj;
  562. const char *sub;
  563. Py_ssize_t sub_len;
  564. char byte;
  565. Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
  566. Py_buffer vsub;
  567. PyObject *count_obj;
  568. if (!parse_args_finds_byte("count", args,
  569. &sub_obj, &byte, &start, &end))
  570. return NULL;
  571. if (sub_obj) {
  572. if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
  573. return NULL;
  574. sub = vsub.buf;
  575. sub_len = vsub.len;
  576. }
  577. else {
  578. sub = &byte;
  579. sub_len = 1;
  580. }
  581. ADJUST_INDICES(start, end, len);
  582. count_obj = PyLong_FromSsize_t(
  583. stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
  584. );
  585. if (sub_obj)
  586. PyBuffer_Release(&vsub);
  587. return count_obj;
  588. }
  589. int
  590. _Py_bytes_contains(const char *str, Py_ssize_t len, PyObject *arg)
  591. {
  592. Py_ssize_t ival = PyNumber_AsSsize_t(arg, NULL);
  593. if (ival == -1 && PyErr_Occurred()) {
  594. Py_buffer varg;
  595. Py_ssize_t pos;
  596. PyErr_Clear();
  597. if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
  598. return -1;
  599. pos = stringlib_find(str, len,
  600. varg.buf, varg.len, 0);
  601. PyBuffer_Release(&varg);
  602. return pos >= 0;
  603. }
  604. if (ival < 0 || ival >= 256) {
  605. PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
  606. return -1;
  607. }
  608. return memchr(str, (int) ival, len) != NULL;
  609. }
  610. /* Matches the end (direction >= 0) or start (direction < 0) of the buffer
  611. * against substr, using the start and end arguments. Returns
  612. * -1 on error, 0 if not found and 1 if found.
  613. */
  614. static int
  615. tailmatch(const char *str, Py_ssize_t len, PyObject *substr,
  616. Py_ssize_t start, Py_ssize_t end, int direction)
  617. {
  618. Py_buffer sub_view = {NULL, NULL};
  619. const char *sub;
  620. Py_ssize_t slen;
  621. if (PyBytes_Check(substr)) {
  622. sub = PyBytes_AS_STRING(substr);
  623. slen = PyBytes_GET_SIZE(substr);
  624. }
  625. else {
  626. if (PyObject_GetBuffer(substr, &sub_view, PyBUF_SIMPLE) != 0)
  627. return -1;
  628. sub = sub_view.buf;
  629. slen = sub_view.len;
  630. }
  631. ADJUST_INDICES(start, end, len);
  632. if (direction < 0) {
  633. /* startswith */
  634. if (start > len - slen)
  635. goto notfound;
  636. } else {
  637. /* endswith */
  638. if (end - start < slen || start > len)
  639. goto notfound;
  640. if (end - slen > start)
  641. start = end - slen;
  642. }
  643. if (end - start < slen)
  644. goto notfound;
  645. if (memcmp(str + start, sub, slen) != 0)
  646. goto notfound;
  647. PyBuffer_Release(&sub_view);
  648. return 1;
  649. notfound:
  650. PyBuffer_Release(&sub_view);
  651. return 0;
  652. }
  653. static PyObject *
  654. _Py_bytes_tailmatch(const char *str, Py_ssize_t len,
  655. const char *function_name, PyObject *args,
  656. int direction)
  657. {
  658. Py_ssize_t start = 0;
  659. Py_ssize_t end = PY_SSIZE_T_MAX;
  660. PyObject *subobj = NULL;
  661. int result;
  662. if (!stringlib_parse_args_finds(function_name, args, &subobj, &start, &end))
  663. return NULL;
  664. if (PyTuple_Check(subobj)) {
  665. Py_ssize_t i;
  666. for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
  667. result = tailmatch(str, len, PyTuple_GET_ITEM(subobj, i),
  668. start, end, direction);
  669. if (result == -1)
  670. return NULL;
  671. else if (result) {
  672. Py_RETURN_TRUE;
  673. }
  674. }
  675. Py_RETURN_FALSE;
  676. }
  677. result = tailmatch(str, len, subobj, start, end, direction);
  678. if (result == -1) {
  679. if (PyErr_ExceptionMatches(PyExc_TypeError))
  680. PyErr_Format(PyExc_TypeError,
  681. "%s first arg must be bytes or a tuple of bytes, "
  682. "not %s",
  683. function_name, Py_TYPE(subobj)->tp_name);
  684. return NULL;
  685. }
  686. else
  687. return PyBool_FromLong(result);
  688. }
  689. PyDoc_STRVAR_shared(_Py_startswith__doc__,
  690. "B.startswith(prefix[, start[, end]]) -> bool\n\
  691. \n\
  692. Return True if B starts with the specified prefix, False otherwise.\n\
  693. With optional start, test B beginning at that position.\n\
  694. With optional end, stop comparing B at that position.\n\
  695. prefix can also be a tuple of bytes to try.");
  696. PyObject *
  697. _Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *args)
  698. {
  699. return _Py_bytes_tailmatch(str, len, "startswith", args, -1);
  700. }
  701. PyDoc_STRVAR_shared(_Py_endswith__doc__,
  702. "B.endswith(suffix[, start[, end]]) -> bool\n\
  703. \n\
  704. Return True if B ends with the specified suffix, False otherwise.\n\
  705. With optional start, test B beginning at that position.\n\
  706. With optional end, stop comparing B at that position.\n\
  707. suffix can also be a tuple of bytes to try.");
  708. PyObject *
  709. _Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *args)
  710. {
  711. return _Py_bytes_tailmatch(str, len, "endswith", args, +1);
  712. }