blob: c2399ad176858d913130c9109e44dd8f6264f981 [file] [log] [blame]
Fredrik Lundh58b5e842006-05-26 19:24:53 +00001/* stringlib: find/index implementation */
2
3#ifndef STRINGLIB_FIND_H
4#define STRINGLIB_FIND_H
5
6#ifndef STRINGLIB_FASTSEARCH_H
7#error must include "stringlib/fastsearch.h" before including this module
8#endif
9
10Py_LOCAL(Py_ssize_t)
11stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
12 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
13{
14 if (sub_len == 0)
15 return 0;
16
17 return fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
18}
19
20Py_LOCAL(Py_ssize_t)
21stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
22 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
23{
24 Py_ssize_t pos;
25
26 /* XXX - create reversefastsearch helper! */
27 if (sub_len == 0)
28 pos = str_len;
29 else {
30 Py_ssize_t j;
31 pos = -1;
32 for (j = str_len - sub_len; j >= 0; --j)
33 if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
34 pos = j;
35 break;
36 }
37 }
38
39 return pos;
40}
41
42#endif
43
44/*
45Local variables:
46c-basic-offset: 4
47indent-tabs-mode: nil
48End:
49*/