1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<strchr>>---search for character in string
20
21 INDEX
22 strchr
23
24 SYNOPSIS
25 #include <string.h>
26 char * strchr(const char *<[string]>, int <[c]>);
27
28 DESCRIPTION
29 This function finds the first occurence of <[c]> (converted to
30 a char) in the string pointed to by <[string]> (including the
31 terminating null character).
32
33 RETURNS
34 Returns a pointer to the located character, or a null pointer
35 if <[c]> does not occur in <[string]>.
36
37 PORTABILITY
38 <<strchr>> is ANSI C.
39
40 <<strchr>> requires no supporting OS subroutines.
41
42 QUICKREF
43 strchr ansi pure
44 */
45
46 #include <string.h>
47 #include <limits.h>
48 #include <stdint.h>
49
50 /* Nonzero if X is not aligned on a "long" boundary. */
51 #define UNALIGNED(X) ((uintptr_t)X & (sizeof (long) - 1))
52
53 /* How many bytes are loaded each iteration of the word copy loop. */
54 #define LBLOCKSIZE (sizeof (long))
55
56 #if LONG_MAX == 2147483647L
57 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
58 #else
59 #if LONG_MAX == 9223372036854775807L
60 /* Nonzero if X (a long int) contains a NULL byte. */
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66
67 /* DETECTCHAR returns nonzero if (long)X contains the byte used
68 to fill (long)MASK. */
69 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
70
71 char *
strchr(const char * s1,int i)72 strchr (const char *s1,
73 int i)
74 {
75 const unsigned char *s = (const unsigned char *)s1;
76 unsigned char c = i;
77
78 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
79 unsigned long mask,j;
80 unsigned long *aligned_addr;
81
82 /* Special case for finding 0. */
83 if (!c)
84 {
85 while (UNALIGNED (s))
86 {
87 if (!*s)
88 return (char *) s;
89 s++;
90 }
91 /* Operate a word at a time. */
92 aligned_addr = (unsigned long *) s;
93 while (!DETECTNULL (*aligned_addr))
94 aligned_addr++;
95 /* Found the end of string. */
96 s = (const unsigned char *) aligned_addr;
97 while (*s)
98 s++;
99 return (char *) s;
100 }
101
102 /* All other bytes. Align the pointer, then search a long at a time. */
103 while (UNALIGNED (s))
104 {
105 if (!*s)
106 return NULL;
107 if (*s == c)
108 return (char *) s;
109 s++;
110 }
111
112 mask = c;
113 for (j = 8; j < LBLOCKSIZE * 8; j <<= 1)
114 mask = (mask << j) | mask;
115
116 aligned_addr = (unsigned long *) s;
117 while (!DETECTNULL (*aligned_addr) && !DETECTCHAR (*aligned_addr, mask))
118 aligned_addr++;
119
120 /* The block of bytes currently pointed to by aligned_addr
121 contains either a null or the target char, or both. We
122 catch it using the bytewise search. */
123
124 s = (unsigned char *) aligned_addr;
125
126 #endif /* not PREFER_SIZE_OVER_SPEED */
127
128 while (*s && *s != c)
129 s++;
130 if (*s == c)
131 return (char *)s;
132 return NULL;
133 }
134