3 <<strchr>>---search for character in string
10 char * strchr(const char *<[string]>, int <[c]>);
13 This function finds the first occurence of <[c]> (converted to
14 a char) in the string pointed to by <[string]> (including the
15 terminating null character).
18 Returns a pointer to the located character, or a null pointer
19 if <[c]> does not occur in <[string]>.
24 <<strchr>> requires no supporting OS subroutines.
33 /* Nonzero if X is not aligned on a "long" boundary. */
34 #define UNALIGNED(X) ((long)X & (sizeof (long) - 1))
36 /* How many bytes are loaded each iteration of the word copy loop. */
37 #define LBLOCKSIZE (sizeof (long))
39 #if LONG_MAX == 2147483647L
40 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
42 #if LONG_MAX == 9223372036854775807L
43 /* Nonzero if X (a long int) contains a NULL byte. */
44 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
46 #error long int is not a 32bit or 64bit type.
50 /* DETECTCHAR returns nonzero if (long)X contains the byte used
51 to fill (long)MASK. */
52 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
55 strchr (const char *s1
,
58 const unsigned char *s
= (const unsigned char *)s1
;
61 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
63 unsigned long *aligned_addr
;
65 /* Special case for finding 0. */
74 /* Operate a word at a time. */
75 aligned_addr
= (unsigned long *) s
;
76 while (!DETECTNULL (*aligned_addr
))
78 /* Found the end of string. */
79 s
= (const unsigned char *) aligned_addr
;
85 /* All other bytes. Align the pointer, then search a long at a time. */
96 for (j
= 8; j
< LBLOCKSIZE
* 8; j
<<= 1)
97 mask
= (mask
<< j
) | mask
;
99 aligned_addr
= (unsigned long *) s
;
100 while (!DETECTNULL (*aligned_addr
) && !DETECTCHAR (*aligned_addr
, mask
))
103 /* The block of bytes currently pointed to by aligned_addr
104 contains either a null or the target char, or both. We
105 catch it using the bytewise search. */
107 s
= (unsigned char *) aligned_addr
;
109 #endif /* not PREFER_SIZE_OVER_SPEED */
111 while (*s
&& *s
!= c
)