]>
Commit | Line | Data |
---|---|---|
2a8867a1 | 1 | /* Scan memory for a character. Generic version |
26420023 | 2 | Copyright (C) 1991-2025 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
28f540f4 | 4 | |
6d52618b | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 9 | |
6d52618b UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
28f540f4 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
28f540f4 | 18 | |
2a8867a1 AZ |
19 | #include <libc-pointer-arith.h> |
20 | #include <string-fzb.h> | |
21 | #include <string-fzc.h> | |
22 | #include <string-fzi.h> | |
23 | #include <string-shift.h> | |
1570a72b | 24 | #include <string.h> |
7782ca54 | 25 | |
2a8867a1 | 26 | #undef memchr |
c2e14186 | 27 | |
2a8867a1 AZ |
28 | #ifdef MEMCHR |
29 | # define __memchr MEMCHR | |
7782ca54 JM |
30 | #endif |
31 | ||
2a8867a1 AZ |
32 | static __always_inline const char * |
33 | sadd (uintptr_t x, uintptr_t y) | |
34 | { | |
35 | return (const char *)(y > UINTPTR_MAX - x ? UINTPTR_MAX : x + y); | |
36 | } | |
1c62e6d9 | 37 | |
9a0a462c | 38 | /* Search no more than N bytes of S for C. */ |
1570a72b | 39 | void * |
2a8867a1 | 40 | __memchr (void const *s, int c_in, size_t n) |
28f540f4 | 41 | { |
2a8867a1 AZ |
42 | if (__glibc_unlikely (n == 0)) |
43 | return NULL; | |
44 | ||
45 | /* Read the first word, but munge it so that bytes before the array | |
46 | will not match goal. */ | |
47 | const op_t *word_ptr = PTR_ALIGN_DOWN (s, sizeof (op_t)); | |
48 | uintptr_t s_int = (uintptr_t) s; | |
49 | ||
50 | op_t word = *word_ptr; | |
51 | op_t repeated_c = repeat_bytes (c_in); | |
52 | /* Compute the address of the last byte taking in consideration possible | |
53 | overflow. */ | |
54 | const char *lbyte = sadd (s_int, n - 1); | |
55 | /* And also the address of the word containing the last byte. */ | |
56 | const op_t *lword = (const op_t *) PTR_ALIGN_DOWN (lbyte, sizeof (op_t)); | |
57 | ||
58 | find_t mask = shift_find (find_eq_all (word, repeated_c), s_int); | |
59 | if (mask != 0) | |
1570a72b | 60 | { |
2a8867a1 AZ |
61 | char *ret = (char *) s + index_first (mask); |
62 | return (ret <= lbyte) ? ret : NULL; | |
1570a72b | 63 | } |
2a8867a1 AZ |
64 | if (word_ptr == lword) |
65 | return NULL; | |
28f540f4 | 66 | |
2a8867a1 AZ |
67 | word = *++word_ptr; |
68 | while (word_ptr != lword) | |
28f540f4 | 69 | { |
2a8867a1 AZ |
70 | if (has_eq (word, repeated_c)) |
71 | return (char *) word_ptr + index_first_eq (word, repeated_c); | |
72 | word = *++word_ptr; | |
28f540f4 RM |
73 | } |
74 | ||
2a8867a1 | 75 | if (has_eq (word, repeated_c)) |
28f540f4 | 76 | { |
2a8867a1 AZ |
77 | /* We found a match, but it might be in a byte past the end of the |
78 | array. */ | |
79 | char *ret = (char *) word_ptr + index_first_eq (word, repeated_c); | |
80 | if (ret <= lbyte) | |
81 | return ret; | |
28f540f4 | 82 | } |
1570a72b | 83 | return NULL; |
28f540f4 | 84 | } |
2a8867a1 | 85 | #ifndef MEMCHR |
e97ed6dd | 86 | weak_alias (__memchr, memchr) |
85dd1003 | 87 | libc_hidden_builtin_def (memchr) |
2a8867a1 | 88 | #endif |