1 /* str/findnext.c - Find the next instance of a single character
2  * Copyright (C) 2001,2005  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18 #include <string.h>
19 #include "str.h"
20 
21 /** Find the next instance of the given character, on or after \c pos */
str_findnext(const str * s,char ch,unsigned pos)22 int str_findnext(const str* s, char ch, unsigned pos)
23 {
24   char* p;
25   if (pos >= s->len) return -1;
26   p = memchr(s->s+pos, ch, s->len-pos);
27   if (!p) return -1;
28   return p - s->s;
29 }
30 
31 #ifdef SELFTEST_MAIN
32 MAIN
33 {
34   str s = { "01234567890123456", 16, 0 };
35   obuf_puti(&outbuf, str_findnext(&s, '4', 0)); NL();
36   obuf_puti(&outbuf, str_findnext(&s, '4', 4)); NL();
37   obuf_puti(&outbuf, str_findnext(&s, '4', 5)); NL();
38   obuf_puti(&outbuf, str_findnext(&s, '6', 7)); NL();
39 }
40 #endif
41 #ifdef SELFTEST_EXP
42 4
43 4
44 14
45 -1
46 #endif
47