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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
/*
* strspn, strcspn
*/
#include <string.h>
#include <stddef.h>
#include <inttypes.h>
#include <limits.h>
#ifndef LONG_BIT
#define LONG_BIT (CHAR_BIT*sizeof(long))
#endif
static inline void
set_bit(unsigned long *bitmap, unsigned int bit)
{
bitmap[bit/LONG_BIT] |= 1UL << (bit%LONG_BIT);
}
static inline int
test_bit(unsigned long *bitmap, unsigned int bit)
{
return (int)(bitmap[bit/LONG_BIT] >> (bit%LONG_BIT)) & 1;
}
static size_t
strxspn(const char *s, const char *map, int parity)
{
unsigned long matchmap[((1 << CHAR_BIT)+LONG_BIT-1)/LONG_BIT];
size_t n = 0;
/* Create bitmap */
memset(matchmap, 0, sizeof matchmap);
while ( *map )
set_bit(matchmap, (unsigned char) *map++);
/* Make sure the null character never matches */
if ( parity )
set_bit(matchmap, 0);
/* Calculate span length */
while ( test_bit(matchmap, (unsigned char) *s++)^parity )
n++;
return n;
}
size_t
strspn(const char *s, const char *accept)
{
return strxspn(s, accept, 0);
}
size_t
strcspn(const char *s, const char *reject)
{
return strxspn(s, reject, 1);
}
char *
strpbrk(const char *s, const char *accept)
{
const char *ss = s+strxspn(s, accept, 1);
return *ss ? (char *)ss : NULL;
}
|