libowfat/str/str_rchr.c

42 lines
924 B
C
Raw Normal View History

2001-02-02 17:54:47 +00:00
#include "str.h"
2006-11-07 17:56:05 +00:00
size_t str_rchr(const char *in, char needle) {
2024-02-12 17:18:44 +00:00
#if 1
size_t i,j=-1;
for (i=0; in[i]; ++i) if (in[i]==needle) j=i;
return i<j ? i : j;
#else
2001-02-02 17:54:47 +00:00
register const char* t=in;
register const char c=needle;
register const char* found=0;
for (;;) {
2016-04-27 14:07:49 +00:00
if (!*t) break;
if (*t==c) found=t;
++t;
if (!*t) break;
if (*t==c) found=t;
++t;
if (!*t) break;
if (*t==c) found=t;
++t;
if (!*t) break;
if (*t==c) found=t;
++t;
2001-02-02 17:54:47 +00:00
}
2014-03-14 02:15:38 +00:00
return (size_t)((found?found:t)-in);
2024-02-12 17:18:44 +00:00
#endif
2001-02-02 17:54:47 +00:00
}
2024-02-12 17:18:44 +00:00
#ifdef UNITTEST
#include <assert.h>
int main() {
assert(str_rchr("fnord",'r')==3);
assert(str_rchr("frord",'r')==3);
assert(str_rchr("fnord",'x')==5);
return 0;
}
#endif