hdt: Dumping ACPI structures
[syslinux.git] / com32 / lib / strspn.c
blob7a24820595dddef4c37e360fd5ef02e384be98c8
1 /*
2 * strspn, strcspn
3 */
5 #include <string.h>
6 #include <stddef.h>
7 #include <inttypes.h>
8 #include <limits.h>
10 #ifndef LONG_BIT
11 #define LONG_BIT (CHAR_BIT*sizeof(long))
12 #endif
14 static void set_bit(unsigned long *bitmap, unsigned int bit)
16 bitmap[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
19 static int test_bit(unsigned long *bitmap, unsigned int bit)
21 return (int)(bitmap[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1;
24 static size_t strxspn(const char *s, const char *map, int parity)
26 unsigned long matchmap[((1 << CHAR_BIT) + LONG_BIT - 1) / LONG_BIT];
27 size_t n = 0;
29 /* Create bitmap */
30 memset(matchmap, 0, sizeof matchmap);
31 while (*map)
32 set_bit(matchmap, (unsigned char)*map++);
34 /* Make sure the null character never matches */
35 if (parity)
36 set_bit(matchmap, 0);
38 /* Calculate span length */
39 while (test_bit(matchmap, (unsigned char)*s++) ^ parity)
40 n++;
42 return n;
45 size_t strspn(const char *s, const char *accept)
47 return strxspn(s, accept, 0);
50 size_t strcspn(const char *s, const char *reject)
52 return strxspn(s, reject, 1);
55 char *strpbrk(const char *s, const char *accept)
57 const char *ss = s + strxspn(s, accept, 1);
59 return *ss ? (char *)ss : NULL;