[...]
Knuth's strlen then, looks like this.H and L are used only within myStrlen; I'd declare them inside the
#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL
function. Yes, I know macro definitions aren't scoped that way, but
it's useful for documentation.
size_t myStrlen(char *s)[snip]
{
}You're using a %d format for a size_t value.
#ifdef TEST
int main(int argc,char *argv[])
{
char *str = "The lazy fox jumped over the slow dog";
if (argc 1) {
str = argv[1];
}
printf(
"Strlen of '%s' is %d (%d)\n",
str,strlen(str),myStrlen(str));
And I'd add a "return 0;" here, even though it isn't strictly
necessary in C99.
}As I mentioned elsethread, the algorithm (which I snipped here) is not
#endif
portable due to alignment issues, and I believe there's no way to make
it 100% portable. But there are *mostly* portable tricks that can
detect pointer alignment on *most* systems (and testing can detect
systems where the tricks don't work). And, of course, an
implementation of strlen() doesn't have to be written in 100% portable
C; it can use whatever system-specific tricks it likes.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"