Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUNCTION |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 3 | <<strchr>>---search for character in string |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 4 | |
| 5 | INDEX |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 6 | strchr |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 7 | |
| 8 | ANSI_SYNOPSIS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 9 | #include <string.h> |
| 10 | char * strchr(const char *<[string]>, int <[c]>); |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 11 | |
| 12 | TRAD_SYNOPSIS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | char * strchr(<[string]>, <[c]>); |
| 15 | char *<[string]>; |
| 16 | int *<[c]>; |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 17 | |
| 18 | DESCRIPTION |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 19 | This function finds the first occurence of <[c]> (converted to |
| 20 | a char) in the string pointed to by <[string]> (including the |
| 21 | terminating null character). |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 22 | |
| 23 | RETURNS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 24 | Returns a pointer to the located character, or a null pointer |
| 25 | if <[c]> does not occur in <[string]>. |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +0000 | [diff] [blame] | 26 | |
| 27 | PORTABILITY |
| 28 | <<strchr>> is ANSI C. |
| 29 | |
| 30 | <<strchr>> requires no supporting OS subroutines. |
| 31 | |
| 32 | QUICKREF |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 33 | strchr ansi pure |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +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 */ |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +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 | char * |
| 62 | _DEFUN (strchr, (s1, i), |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 63 | _CONST char *s1 _AND |
| 64 | int i) |
Björn Stenberg | b69338f | 2002-05-30 21:03:17 +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 int)i; |
| 69 | |
| 70 | while (*s && *s != c) |
| 71 | { |
| 72 | s++; |
| 73 | } |
| 74 | |
| 75 | if (*s != c) |
| 76 | { |
| 77 | s = NULL; |
| 78 | } |
| 79 | |
| 80 | return (char *) s; |
| 81 | #else |
| 82 | unsigned char c = (unsigned char)i; |
| 83 | unsigned long mask,j; |
| 84 | unsigned long *aligned_addr; |
| 85 | |
| 86 | if (!UNALIGNED (s)) |
| 87 | { |
| 88 | mask = 0; |
| 89 | for (j = 0; j < LBLOCKSIZE; j++) |
| 90 | mask = (mask << 8) | c; |
| 91 | |
| 92 | aligned_addr = (unsigned long*)s; |
| 93 | while (!DETECTNULL (*aligned_addr) && !DETECTCHAR (*aligned_addr, mask)) |
| 94 | aligned_addr++; |
| 95 | |
| 96 | /* The block of bytes currently pointed to by aligned_addr |
| 97 | contains either a null or the target char, or both. We |
| 98 | catch it using the bytewise search. */ |
| 99 | |
| 100 | s = (unsigned char*)aligned_addr; |
| 101 | } |
| 102 | |
| 103 | while (*s && *s != c) |
| 104 | s++; |
| 105 | if (*s == c) |
| 106 | return (char *)s; |
| 107 | return NULL; |
| 108 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
| 109 | } |