3 <<memchr>>---search for character in memory
10 void * memchr(const void *<[s1]>, int <[c]>, size_t <[n]>);
14 void * memchr(<[s1]>, <[c]>, <[n]>);
20 This function scans the first <[n]> bytes of the memory pointed
21 to by <[s1]> for the character <[c]> (converted to a char).
24 Returns a pointer to the matching byte, or a null pointer if
25 <[c]> does not occur in <[s1]>.
30 <<memchr>> requires no supporting OS subroutines.
39 /* Nonzero if X is not aligned on a "long" boundary. */
40 #define UNALIGNED(X) ((long)X & (sizeof (long) - 1))
42 /* How many bytes are loaded each iteration of the word copy loop. */
43 #define LBLOCKSIZE (sizeof (long))
45 #if LONG_MAX == 2147483647L
46 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
48 #if LONG_MAX == 9223372036854775807L
49 /* Nonzero if X (a long int) contains a NULL byte. */
50 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
52 #error long int is not a 32bit or 64bit type.
56 /* DETECTCHAR returns nonzero if (long)X contains the byte used
57 to fill (long)MASK. */
58 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
61 _DEFUN (memchr
, (s1
, i
, n
),
65 _CONST
unsigned char *s
= (_CONST
unsigned char *)s1
;
66 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
67 unsigned char c
= (unsigned char)i
;
80 unsigned char c
= (unsigned char)i
;
82 unsigned long *aligned_addr
;
87 for (j
= 0; j
< LBLOCKSIZE
; j
++)
88 mask
= (mask
<< 8) | c
;
90 aligned_addr
= (unsigned long*)s
;
91 while ((!DETECTCHAR (*aligned_addr
, mask
)) && (n
>LBLOCKSIZE
))
97 /* The block of bytes currently pointed to by aligned_addr
98 may contain the target character or there may be less than
99 LBLOCKSIZE bytes left to search. We check the last few
100 bytes using the bytewise search. */
102 s
= (unsigned char*)aligned_addr
;
115 #endif /* not PREFER_SIZE_OVER_SPEED */