Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | /* stringlib: count implementation */ |
| 2 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3 | #ifndef STRINGLIB_FASTSEARCH_H |
| 4 | #error must include "stringlib/fastsearch.h" before including this module |
| 5 | #endif |
| 6 | |
| 7 | Py_LOCAL_INLINE(Py_ssize_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8 | STRINGLIB(count)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, |
| 10 | Py_ssize_t maxcount) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11 | { |
| 12 | Py_ssize_t count; |
| 13 | |
Amaury Forgeot d'Arc | f2e9368 | 2008-09-26 22:48:41 +0000 | [diff] [blame] | 14 | if (str_len < 0) |
| 15 | return 0; /* start > len(str) */ |
| 16 | if (sub_len == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 17 | return (str_len < maxcount) ? str_len + 1 : maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 19 | count = FASTSEARCH(str, str_len, sub, sub_len, maxcount, FAST_COUNT); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 20 | |
| 21 | if (count < 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 22 | return 0; /* no match */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 23 | |
| 24 | return count; |
| 25 | } |
| 26 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 27 | |