diff --git a/src/system/libroot/posix/string/strlen.c b/src/system/libroot/posix/string/strlen.c index 013c278796..a94ded26c6 100644 --- a/src/system/libroot/posix/string/strlen.c +++ b/src/system/libroot/posix/string/strlen.c @@ -6,30 +6,29 @@ #include #include -// 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; + size_t length = 0; + uint32* valuePointer; - //Make sure we use aligned access - while (((uint32) s + i) & 3) { - if (!s[i]) return i; - i++; - } + /* Align access for four byte reads */ + for (; (((addr_t) s + length) & 3) != 0; length++) + if (s[length] == '\0') + return length; - //Check four bytes at once - value = (uint32 *) (s + i); - while (!(hasZeroByte(*value))) - value++; + /* Check four bytes for zero char */ + for (valuePointer = (uint32*) (s + length); !HasZeroByte(*valuePointer); + valuePointer++); - //Find the exact length - i = ((char *) value) - s; - while (s[i]) - i++; + /* Find the exact length */ + for (length = ((char*) valuePointer) - s; s[length] != '\0'; length++); - return i; + return length; } diff --git a/src/system/libroot/posix/string/strnlen.c b/src/system/libroot/posix/string/strnlen.c index 05a29207b8..677fd4f5d8 100644 --- a/src/system/libroot/posix/string/strnlen.c +++ b/src/system/libroot/posix/string/strnlen.c @@ -7,32 +7,31 @@ #include -// 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; + size_t length = 0; + uint32* valuePointer; + uint32* maxScanPosition; - //Make sure we use aligned access - while (((uint32) s + i) & 3) { - if (i == count || !s[i]) return i; - i++; - } + /* Align access for four byte reads */ + for (; (((addr_t) s + length) & 3) != 0; length++) + if (length == count || s[length] == '\0') + return length; + /* Check four bytes for zero char */ + maxScanPosition = (uint32*) (s + count - 4); + for (valuePointer = (uint32*) (s + length); valuePointer <= maxScanPosition + && !HasZeroByte(*valuePointer); valuePointer++); - const uint32 * end = (uint32 *) s + count; - //Check four bytes at once - value = (uint32 *) (s + i); - while (value < end && !(hasZeroByte(*value)) ) - value++; + /* Find the exact length */ + for (length = ((char*) valuePointer) - s; length < count + && s[length] != '\0'; length++); - //Find the exact length - i = ((char *) value) - s; - while (s[i] && i < count ) - i++; - - return i; + return length; }