blob: f48500bf561f2c23944b9a6158da8d89d3ce1c65 [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001/* stringlib: count implementation */
2
Thomas Wouters477c8d52006-05-27 19:21:47 +00003#ifndef STRINGLIB_FASTSEARCH_H
4#error must include "stringlib/fastsearch.h" before including this module
5#endif
6
7Py_LOCAL_INLINE(Py_ssize_t)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008STRINGLIB(count)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
Antoine Pitrouf2c54842010-01-13 08:07:53 +00009 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
10 Py_ssize_t maxcount)
Thomas Wouters477c8d52006-05-27 19:21:47 +000011{
12 Py_ssize_t count;
13
Amaury Forgeot d'Arcf2e93682008-09-26 22:48:41 +000014 if (str_len < 0)
15 return 0; /* start > len(str) */
16 if (sub_len == 0)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000017 return (str_len < maxcount) ? str_len + 1 : maxcount;
Thomas Wouters477c8d52006-05-27 19:21:47 +000018
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020019 count = FASTSEARCH(str, str_len, sub, sub_len, maxcount, FAST_COUNT);
Thomas Wouters477c8d52006-05-27 19:21:47 +000020
21 if (count < 0)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000022 return 0; /* no match */
Thomas Wouters477c8d52006-05-27 19:21:47 +000023
24 return count;
25}
26
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020027