Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1 | /* 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 Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 10 | Py_LOCAL_INLINE(Py_ssize_t) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 11 | stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 12 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len) |
| 13 | { |
| 14 | Py_ssize_t count; |
| 15 | |
Amaury Forgeot d'Arc | fc5ea39 | 2008-09-26 22:34:08 +0000 | [diff] [blame] | 16 | if (str_len < 0) |
| 17 | return 0; /* start > len(str) */ |
| 18 | if (sub_len == 0) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 19 | return str_len + 1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 20 | |
| 21 | count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT); |
| 22 | |
| 23 | if (count < 0) |
| 24 | count = 0; /* no match */ |
| 25 | |
| 26 | return count; |
| 27 | } |
| 28 | |
| 29 | #endif |
| 30 | |
| 31 | /* |
| 32 | Local variables: |
| 33 | c-basic-offset: 4 |
| 34 | indent-tabs-mode: nil |
| 35 | End: |
| 36 | */ |