count.h 941 B

1234567891011121314151617181920212223242526272829303132
  1. /* stringlib: count implementation */
  2. #ifndef STRINGLIB_FASTSEARCH_H
  3. #error must include "stringlib/fastsearch.h" before including this module
  4. #endif
  5. // gh-97982: Implementing asciilib_count() is not worth it, FASTSEARCH() does
  6. // not specialize the code for ASCII strings. Use ucs1lib_count() for ASCII and
  7. // UCS1 strings: it's the same than asciilib_count().
  8. #if !STRINGLIB_IS_UNICODE || STRINGLIB_MAX_CHAR > 0x7Fu
  9. Py_LOCAL_INLINE(Py_ssize_t)
  10. STRINGLIB(count)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
  11. const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
  12. Py_ssize_t maxcount)
  13. {
  14. Py_ssize_t count;
  15. if (str_len < 0)
  16. return 0; /* start > len(str) */
  17. if (sub_len == 0)
  18. return (str_len < maxcount) ? str_len + 1 : maxcount;
  19. count = FASTSEARCH(str, str_len, sub, sub_len, maxcount, FAST_COUNT);
  20. if (count < 0)
  21. return 0; /* no match */
  22. return count;
  23. }
  24. #endif