PIPS
|
#include <config.h>
#include <string.h>
Go to the source code of this file.
Functions | |
char * | strchrnul (const char *s, int c_in) |
Searching in a string. More... | |
char* strchrnul | ( | const char * | s, |
int | c_in | ||
) |
Searching in a string.
Copyright (C) 2003, 2007-2014 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Specification.
Find the first occurrence of C in S or the final NUL byte.
On 32-bit hardware, choosing longword to be a 32-bit unsigned long instead of a 64-bit uintmax_t tends to give better performance. On 64-bit hardware, unsigned long is generally 64 bits already. Change this typedef to experiment with performance.
Handle the first few bytes by reading one byte at a time. Do this until CHAR_PTR is aligned on a longword boundary.
All these elucidatory comments refer to 4-byte longwords, but the theory applies equally well to any size longwords.
Compute auxiliary longword values: repeated_one is a value which has a 1 in every byte. repeated_c has c in every byte.
Instead of the traditional loop which tests each byte, we will test a longword at a time. The tricky part is testing if any of the four bytes in the longword in question are equal to NUL or c. We first use an xor with repeated_c. This reduces the task to testing whether any of the four bytes in longword1 or longword2 is zero.
Let's consider longword1. We compute tmp = ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7). That is, we perform the following operations:
The test whether any byte in longword1 or longword2 is zero is equivalent to testing whether tmp1 is nonzero or tmp2 is nonzero. We can combine this into a single test, whether (tmp1 | tmp2) is nonzero.
This test can read more than one byte beyond the end of a string, depending on where the terminating NUL is encountered. However, this is considered safe since the initialization phase ensured that the read will be aligned, therefore, the read will not cross page boundaries and will not cause a fault.
At this point, we know that one of the sizeof (longword) bytes starting at char_ptr is == 0 or == c. On little-endian machines, we could determine the first such byte without any further memory accesses, just by looking at the tmp result from the last loop iteration. But this does not work on big-endian machines. Choose code that works in both cases.
Definition at line 24 of file strchrnul.c.
References c_in, and rawmemchr().
Referenced by retrieve_a_missing_file_using_external_resolver().