Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUNCTION |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 3 | <<memchr>>---search for character in memory |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 4 | |
| 5 | INDEX |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 6 | memchr |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 7 | |
| 8 | ANSI_SYNOPSIS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 9 | #include <string.h> |
| 10 | void * memchr(const void *<[s1]>, int <[c]>, size_t <[n]>); |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 11 | |
| 12 | TRAD_SYNOPSIS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | void * memchr(<[s1]>, <[c]>, <[n]>); |
| 15 | void *<[string]>; |
| 16 | int *<[c]>; |
| 17 | size_t *<[n]>; |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 18 | |
| 19 | DESCRIPTION |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 20 | This function scans the first <[n]> bytes of the memory pointed |
| 21 | to by <[s1]> for the character <[c]> (converted to a char). |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 22 | |
| 23 | RETURNS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 24 | Returns a pointer to the matching byte, or a null pointer if |
| 25 | <[c]> does not occur in <[s1]>. |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 26 | |
| 27 | PORTABILITY |
| 28 | <<memchr>> is ANSI C. |
| 29 | |
| 30 | <<memchr>> requires no supporting OS subroutines. |
| 31 | |
| 32 | QUICKREF |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 33 | memchr ansi pure |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <string.h> |
| 37 | #include <limits.h> |
Thomas Martitz | f32bd59 | 2010-06-22 18:34:03 +0000 | [diff] [blame] | 38 | #include "_ansi.h" /* for _DEFUN */ |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 39 | |
| 40 | /* Nonzero if X is not aligned on a "long" boundary. */ |
| 41 | #define UNALIGNED(X) ((long)X & (sizeof (long) - 1)) |
| 42 | |
| 43 | /* How many bytes are loaded each iteration of the word copy loop. */ |
| 44 | #define LBLOCKSIZE (sizeof (long)) |
| 45 | |
| 46 | #if LONG_MAX == 2147483647L |
| 47 | #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) |
| 48 | #else |
| 49 | #if LONG_MAX == 9223372036854775807L |
| 50 | /* Nonzero if X (a long int) contains a NULL byte. */ |
| 51 | #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) |
| 52 | #else |
| 53 | #error long int is not a 32bit or 64bit type. |
| 54 | #endif |
| 55 | #endif |
| 56 | |
| 57 | /* DETECTCHAR returns nonzero if (long)X contains the byte used |
| 58 | to fill (long)MASK. */ |
| 59 | #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK)) |
| 60 | |
| 61 | void * |
| 62 | _DEFUN (memchr, (s1, i, n), |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 63 | _CONST void *s1 _AND |
| 64 | int i _AND size_t n) |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 65 | { |
| 66 | _CONST unsigned char *s = (_CONST unsigned char *)s1; |
| 67 | #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) |
| 68 | unsigned char c = (unsigned char)i; |
| 69 | |
| 70 | while (n-- > 0) |
| 71 | { |
| 72 | if (*s == c) |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 73 | { |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 74 | return (void *)s; |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 75 | } |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 76 | s++; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | #else |
| 81 | unsigned char c = (unsigned char)i; |
| 82 | unsigned long mask,j; |
| 83 | unsigned long *aligned_addr; |
| 84 | |
| 85 | if (!UNALIGNED (s)) |
| 86 | { |
| 87 | mask = 0; |
| 88 | for (j = 0; j < LBLOCKSIZE; j++) |
| 89 | mask = (mask << 8) | c; |
| 90 | |
| 91 | aligned_addr = (unsigned long*)s; |
| 92 | while ((!DETECTCHAR (*aligned_addr, mask)) && (n>LBLOCKSIZE)) |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 93 | { |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 94 | aligned_addr++; |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 95 | n -= LBLOCKSIZE; |
| 96 | } |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 97 | |
| 98 | /* The block of bytes currently pointed to by aligned_addr |
| 99 | may contain the target character or there may be less than |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 100 | LBLOCKSIZE bytes left to search. We check the last few |
| 101 | bytes using the bytewise search. */ |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 102 | |
| 103 | s = (unsigned char*)aligned_addr; |
| 104 | } |
| 105 | |
| 106 | while (n-- > 0) |
| 107 | { |
| 108 | if (*s == c) |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 109 | { |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 110 | return (void *)s; |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 111 | } |
Linus Nielsen Feltzing | eaf8b2d | 2005-07-05 08:43:36 +0000 | [diff] [blame] | 112 | s++; |
| 113 | } |
| 114 | |
| 115 | return NULL; |
| 116 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
| 117 | } |