find.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* stringlib: find/index implementation */
  2. #ifndef STRINGLIB_FASTSEARCH_H
  3. #error must include "stringlib/fastsearch.h" before including this module
  4. #endif
  5. Py_LOCAL_INLINE(Py_ssize_t)
  6. STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
  7. const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
  8. Py_ssize_t offset)
  9. {
  10. Py_ssize_t pos;
  11. assert(str_len >= 0);
  12. if (sub_len == 0)
  13. return offset;
  14. pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
  15. if (pos >= 0)
  16. pos += offset;
  17. return pos;
  18. }
  19. Py_LOCAL_INLINE(Py_ssize_t)
  20. STRINGLIB(rfind)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
  21. const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
  22. Py_ssize_t offset)
  23. {
  24. Py_ssize_t pos;
  25. assert(str_len >= 0);
  26. if (sub_len == 0)
  27. return str_len + offset;
  28. pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
  29. if (pos >= 0)
  30. pos += offset;
  31. return pos;
  32. }
  33. Py_LOCAL_INLINE(Py_ssize_t)
  34. STRINGLIB(find_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
  35. const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
  36. Py_ssize_t start, Py_ssize_t end)
  37. {
  38. return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
  39. }
  40. Py_LOCAL_INLINE(Py_ssize_t)
  41. STRINGLIB(rfind_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
  42. const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
  43. Py_ssize_t start, Py_ssize_t end)
  44. {
  45. return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
  46. }
  47. #ifdef STRINGLIB_WANT_CONTAINS_OBJ
  48. Py_LOCAL_INLINE(int)
  49. STRINGLIB(contains_obj)(PyObject* str, PyObject* sub)
  50. {
  51. return STRINGLIB(find)(
  52. STRINGLIB_STR(str), STRINGLIB_LEN(str),
  53. STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
  54. ) != -1;
  55. }
  56. #endif /* STRINGLIB_WANT_CONTAINS_OBJ */
  57. /*
  58. This function is a helper for the "find" family (find, rfind, index,
  59. rindex) and for count, startswith and endswith, because they all have
  60. the same behaviour for the arguments.
  61. It does not touch the variables received until it knows everything
  62. is ok.
  63. */
  64. #define FORMAT_BUFFER_SIZE 50
  65. Py_LOCAL_INLINE(int)
  66. STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
  67. PyObject **subobj,
  68. Py_ssize_t *start, Py_ssize_t *end)
  69. {
  70. PyObject *tmp_subobj;
  71. Py_ssize_t tmp_start = 0;
  72. Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
  73. PyObject *obj_start=Py_None, *obj_end=Py_None;
  74. char format[FORMAT_BUFFER_SIZE] = "O|OO:";
  75. size_t len = strlen(format);
  76. strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
  77. format[FORMAT_BUFFER_SIZE - 1] = '\0';
  78. if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
  79. return 0;
  80. /* To support None in "start" and "end" arguments, meaning
  81. the same as if they were not passed.
  82. */
  83. if (obj_start != Py_None)
  84. if (!_PyEval_SliceIndex(obj_start, &tmp_start))
  85. return 0;
  86. if (obj_end != Py_None)
  87. if (!_PyEval_SliceIndex(obj_end, &tmp_end))
  88. return 0;
  89. *start = tmp_start;
  90. *end = tmp_end;
  91. *subobj = tmp_subobj;
  92. return 1;
  93. }
  94. #undef FORMAT_BUFFER_SIZE