Adding upstream version 3.50~pre5.
[syslinux-debian/hramrach.git] / com32 / lib / strspn.c
blobd569e02948565e16bbedc37ecff4d48e21ad7af1
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 inline void
15 set_bit(unsigned long *bitmap, unsigned int bit)
17 bitmap[bit/LONG_BIT] |= 1UL << (bit%LONG_BIT);
20 static inline int
21 test_bit(unsigned long *bitmap, unsigned int bit)
23 return (int)(bitmap[bit/LONG_BIT] >> (bit%LONG_BIT)) & 1;
26 static size_t
27 strxspn(const char *s, const char *map, int parity)
29 unsigned long matchmap[((1 << CHAR_BIT)+LONG_BIT-1)/LONG_BIT];
30 size_t n = 0;
32 /* Create bitmap */
33 memset(matchmap, 0, sizeof matchmap);
34 while ( *map )
35 set_bit(matchmap, (unsigned char) *map++);
37 /* Make sure the null character never matches */
38 if ( parity )
39 set_bit(matchmap, 0);
41 /* Calculate span length */
42 while ( test_bit(matchmap, (unsigned char) *s++)^parity )
43 n++;
45 return n;
48 size_t
49 strspn(const char *s, const char *accept)
51 return strxspn(s, accept, 0);
54 size_t
55 strcspn(const char *s, const char *reject)
57 return strxspn(s, reject, 1);
60 char *
61 strpbrk(const char *s, const char *accept)
63 const char *ss = s+strxspn(s, accept, 1);
65 return *ss ? (char *)ss : NULL;