headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / string / strnlen.cpp
blob9b22ccbbe1e0de6c040e8b65b410f2873a556d6d
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
6 #include <string.h>
7 #include <SupportDefs.h>
10 /* From Bit twiddling hacks:
11 http://graphics.stanford.edu/~seander/bithacks.html */
12 #define LACKS_ZERO_BYTE(value) \
13 (((value - 0x01010101) & ~value & 0x80808080) == 0)
16 size_t
17 strnlen(const char* string, size_t count)
19 size_t length = 0;
20 /* Align access for four byte reads */
21 for (; (((addr_t)string + length) & 3) != 0; length++) {
22 if (length == count || string[length] == '\0')
23 return length;
26 /* Check four bytes for zero char */
27 const uint32* kMaxScanPosition = (uint32*)(string + count - 4);
28 uint32* valuePointer = (uint32*)(string + length);
29 for (; valuePointer <= kMaxScanPosition && LACKS_ZERO_BYTE(*valuePointer);
30 valuePointer++)
33 /* Find the exact length */
34 for (length = ((char*)valuePointer) - string; length < count
35 && string[length] != '\0'; length++)
38 return length;