Reworked strlen and strnlen to follow style guide.
Fix problems pointed out by Marcus.
This commit is contained in:
@@ -6,30 +6,29 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <SupportDefs.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
|
size_t
|
||||||
strlen(char const *s)
|
strlen(const char* s)
|
||||||
{
|
{
|
||||||
size_t i = 0;
|
size_t length = 0;
|
||||||
uint32 *value;
|
uint32* valuePointer;
|
||||||
|
|
||||||
//Make sure we use aligned access
|
/* Align access for four byte reads */
|
||||||
while (((uint32) s + i) & 3) {
|
for (; (((addr_t) s + length) & 3) != 0; length++)
|
||||||
if (!s[i]) return i;
|
if (s[length] == '\0')
|
||||||
i++;
|
return length;
|
||||||
}
|
|
||||||
|
|
||||||
//Check four bytes at once
|
/* Check four bytes for zero char */
|
||||||
value = (uint32 *) (s + i);
|
for (valuePointer = (uint32*) (s + length); !HasZeroByte(*valuePointer);
|
||||||
while (!(hasZeroByte(*value)))
|
valuePointer++);
|
||||||
value++;
|
|
||||||
|
|
||||||
//Find the exact length
|
/* Find the exact length */
|
||||||
i = ((char *) value) - s;
|
for (length = ((char*) valuePointer) - s; s[length] != '\0'; length++);
|
||||||
while (s[i])
|
|
||||||
i++;
|
|
||||||
|
|
||||||
return i;
|
return length;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,32 +7,31 @@
|
|||||||
#include <SupportDefs.h>
|
#include <SupportDefs.h>
|
||||||
|
|
||||||
|
|
||||||
// From Bit twiddling hacks: http://graphics.stanford.edu/~seander/bithacks.html
|
/* From Bit twiddling hacks:
|
||||||
#define hasZeroByte(value) (value - 0x01010101) & ~value & 0x80808080
|
http://graphics.stanford.edu/~seander/bithacks.html */
|
||||||
|
#define HasZeroByte(value) ((value - 0x01010101) & ~value & 0x80808080)
|
||||||
|
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
strnlen(char const *s, size_t count)
|
strnlen(const char* s, size_t count)
|
||||||
{
|
{
|
||||||
size_t i = 0;
|
size_t length = 0;
|
||||||
uint32 *value;
|
uint32* valuePointer;
|
||||||
|
uint32* maxScanPosition;
|
||||||
|
|
||||||
//Make sure we use aligned access
|
/* Align access for four byte reads */
|
||||||
while (((uint32) s + i) & 3) {
|
for (; (((addr_t) s + length) & 3) != 0; length++)
|
||||||
if (i == count || !s[i]) return i;
|
if (length == count || s[length] == '\0')
|
||||||
i++;
|
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;
|
/* Find the exact length */
|
||||||
//Check four bytes at once
|
for (length = ((char*) valuePointer) - s; length < count
|
||||||
value = (uint32 *) (s + i);
|
&& s[length] != '\0'; length++);
|
||||||
while (value < end && !(hasZeroByte(*value)) )
|
|
||||||
value++;
|
|
||||||
|
|
||||||
//Find the exact length
|
return length;
|
||||||
i = ((char *) value) - s;
|
|
||||||
while (s[i] && i < count )
|
|
||||||
i++;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user