summaryrefslogtreecommitdiff
path: root/src/libstddjb/case_str.c
blob: 59f157f4a6940b7930c8cf1c020107bf646d3889 (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/sysdeps.h>

#ifdef SKALIBS_HASSTRCASESTR

#include <skalibs/nonposix.h>
#include <string.h>
#include <skalibs/bytestr.h>

size_t case_str (char const *haystack, char const *needle)
{
  char *p = strcasestr(haystack, needle) ;
  return p ? p - haystack : strlen(haystack) ;
}

#else

#include <string.h>
#include <strings.h>
#include <skalibs/bytestr.h>

size_t case_str (char const *haystack, char const *needle)
{
  size_t nlen = strlen(needle) ;
  char const *p = haystack ;
  if (!nlen) return 0 ;
  for (; *p ; p++)
    if (!strncasecmp(p, needle, nlen)) return p - haystack ;
  return strlen(haystack) ;
}

#endif