hrev43559 adds 1 changeset to branch 'master' old head: 753a02c15661a650762495257bf3abf1167998a7 new head: 11048d36190ef8eefdb3eec13dc1f1f91dddd11f ---------------------------------------------------------------------------- 11048d3: Reworked strlen and strnlen to follow style guide. Fix problems pointed out by Marcus. [ Fredrik Holmqvist <fredrik.holmqvist@xxxxxxxxx> ] ---------------------------------------------------------------------------- Revision: hrev43559 Commit: 11048d36190ef8eefdb3eec13dc1f1f91dddd11f URL: http://cgit.haiku-os.org/haiku/commit/?id=11048d3 Author: Fredrik Holmqvist <fredrik.holmqvist@xxxxxxxxx> Date: Sun Dec 25 16:00:31 2011 UTC ---------------------------------------------------------------------------- 2 files changed, 42 insertions(+), 44 deletions(-) src/system/libroot/posix/string/strlen.c | 45 ++++++++++++------------- src/system/libroot/posix/string/strnlen.c | 41 +++++++++++----------- ---------------------------------------------------------------------------- diff --git a/src/system/libroot/posix/string/strlen.c b/src/system/libroot/posix/string/strlen.c index 013c278..a94ded2 100644 --- a/src/system/libroot/posix/string/strlen.c +++ b/src/system/libroot/posix/string/strlen.c @@ -6,30 +6,29 @@ #include <string.h> #include <SupportDefs.h> -// From Bit twiddling hacks: http://graphics.stanford.edu/~seander/bithacks.html -#define hasZeroByte(value) (value - 0x01010101) & ~value & 0x80808080 + +/* From Bit twiddling hacks: + http://graphics.stanford.edu/~seander/bithacks.html */ +#define HasZeroByte(value) ((value - 0x01010101) & ~value & 0x80808080) + size_t -strlen(char const *s) +strlen(const char* s) { - size_t i = 0; - uint32 *value; - - //Make sure we use aligned access - while (((uint32) s + i) & 3) { - if (!s[i]) return i; - i++; - } - - //Check four bytes at once - value = (uint32 *) (s + i); - while (!(hasZeroByte(*value))) - value++; - - //Find the exact length - i = ((char *) value) - s; - while (s[i]) - i++; - - return i; + size_t length = 0; + uint32* valuePointer; + + /* Align access for four byte reads */ + for (; (((addr_t) s + length) & 3) != 0; length++) + if (s[length] == '\0') + return length; + + /* Check four bytes for zero char */ + for (valuePointer = (uint32*) (s + length); !HasZeroByte(*valuePointer); + valuePointer++); + + /* Find the exact length */ + for (length = ((char*) valuePointer) - s; s[length] != '\0'; length++); + + return length; } diff --git a/src/system/libroot/posix/string/strnlen.c b/src/system/libroot/posix/string/strnlen.c index 05a2920..677fd4f 100644 --- a/src/system/libroot/posix/string/strnlen.c +++ b/src/system/libroot/posix/string/strnlen.c @@ -7,32 +7,31 @@ #include <SupportDefs.h> -// From Bit twiddling hacks: http://graphics.stanford.edu/~seander/bithacks.html -#define hasZeroByte(value) (value - 0x01010101) & ~value & 0x80808080 +/* From Bit twiddling hacks: + http://graphics.stanford.edu/~seander/bithacks.html */ +#define HasZeroByte(value) ((value - 0x01010101) & ~value & 0x80808080) + size_t -strnlen(char const *s, size_t count) +strnlen(const char* s, size_t count) { - size_t i = 0; - uint32 *value; - - //Make sure we use aligned access - while (((uint32) s + i) & 3) { - if (i == count || !s[i]) return i; - i++; - } + size_t length = 0; + uint32* valuePointer; + uint32* maxScanPosition; + /* Align access for four byte reads */ + for (; (((addr_t) s + length) & 3) != 0; length++) + if (length == count || s[length] == '\0') + return length; - const uint32 * end = (uint32 *) s + count; - //Check four bytes at once - value = (uint32 *) (s + i); - while (value < end && !(hasZeroByte(*value)) ) - value++; + /* Check four bytes for zero char */ + maxScanPosition = (uint32*) (s + count - 4); + for (valuePointer = (uint32*) (s + length); valuePointer <= maxScanPosition + && !HasZeroByte(*valuePointer); valuePointer++); - //Find the exact length - i = ((char *) value) - s; - while (s[i] && i < count ) - i++; + /* Find the exact length */ + for (length = ((char*) valuePointer) - s; length < count + && s[length] != '\0'; length++); - return i; + return length; }