summaryrefslogtreecommitdiff
path: root/src/libstddjb/byte_chr.c
blob: e9caef54c9a73be0a02b3fe161c11dfa4407cc06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* ISC license. */

#include <skalibs/config.h>
#include <skalibs/bytestr.h>

#ifndef SKALIBS_FLAG_REPLACE_LIBC

#include <string.h>

unsigned int byte_chr (char const *s, unsigned int n, int c)
{
  register void *p = memchr(s, c, n) ;
  return p ? (unsigned int)((char *)p - s) : n ;
}

#else

unsigned int byte_chr (char const *s, unsigned int n, int c)
{
  register char ch = c ;
  register char const *t = s ;

  for (;;)
  {
    if (!n) break; if (*t == ch) break; ++t; --n;
    if (!n) break; if (*t == ch) break; ++t; --n;
    if (!n) break; if (*t == ch) break; ++t; --n;
    if (!n) break; if (*t == ch) break; ++t; --n;
  }
  return t - s ;
}

#endif