blob: de34f96b3eb3c11d7583f6b9adc514a37dea842d [file] [log] [blame]
Fredrik Lundh58b5e842006-05-26 19:24:53 +00001/* stringlib: count implementation */
2
3#ifndef STRINGLIB_COUNT_H
4#define STRINGLIB_COUNT_H
5
6#ifndef STRINGLIB_FASTSEARCH_H
7#error must include "stringlib/fastsearch.h" before including this module
8#endif
9
Fredrik Lundhc2d29c52006-05-27 14:58:20 +000010Py_LOCAL_INLINE(Py_ssize_t)
Fredrik Lundh58b5e842006-05-26 19:24:53 +000011stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
Antoine Pitrou64672132010-01-13 07:55:48 +000012 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
13 Py_ssize_t maxcount)
Fredrik Lundh58b5e842006-05-26 19:24:53 +000014{
15 Py_ssize_t count;
16
Amaury Forgeot d'Arcfc5ea392008-09-26 22:34:08 +000017 if (str_len < 0)
18 return 0; /* start > len(str) */
19 if (sub_len == 0)
Antoine Pitrou64672132010-01-13 07:55:48 +000020 return (str_len < maxcount) ? str_len + 1 : maxcount;
Fredrik Lundh58b5e842006-05-26 19:24:53 +000021
Antoine Pitrou64672132010-01-13 07:55:48 +000022 count = fastsearch(str, str_len, sub, sub_len, maxcount, FAST_COUNT);
Fredrik Lundh58b5e842006-05-26 19:24:53 +000023
24 if (count < 0)
Antoine Pitrou64672132010-01-13 07:55:48 +000025 return 0; /* no match */
Fredrik Lundh58b5e842006-05-26 19:24:53 +000026
27 return count;
28}
29
30#endif