Witold Filipczyk <[EMAIL PROTECTED]> wrote Tue, Aug 09, 2005:
> In attachment "smarter" strlcmp. It doesn't use strlen and uses one condition
> less in loop than previous version. When string is shorter than length
> ('\0' in the middle of string) results differ. Previous version is IMHO buggy.
Yes, if you compare to the str*cmp() family. This function is designed
to only check for equality so you cannot assume anything, other than the
string differ, when getting a non-zero return value.
> I've too fast computer to judge which version is faster.
So does it matter?
> Results may vary for different data.
>
> Could you check it?
After a small review it looks wrong.
[ Another thing, commenting out the old code makes it less readable IMO,
because there is more code to review. Please, just remove the old code,
it will still be visible. ]
> --- src/util/string.c 2005-08-09 16:47:01.450227816 +0200
> +++ src/util/string.c.2 2005-08-09 16:42:56.180514480 +0200
> @@ -203,17 +203,22 @@
> \
> /* TODO: Don't precompute strlen()s but rather make the loop smarter.
> * --pasky */ \
> - if (n1 == -1) n1 = strlen(s1); \
> - if (n2 == -1) n2 = strlen(s2); \
> + /* if (n1 == -1) n1 = strlen(s1); */ \
> + /* if (n2 == -1) n2 = strlen(s2); */ \
> \
> - string_assert(errfile, errline, n1 >= 0 && n2 >= 0, c); \
> + /* string_assert(errfile, errline, n1 >= 0 && n2 >= 0, c); */ \
> + if (n1 == -1) n1 = n2; \
> + if (n2 == -1) n2 = n1; \
> + if (n1 > n2) n1 = n2; \
Here, both n1 and n2 can be -1, which means the loop below can be
endless.
> \
> - d = n1 - n2; \
> - if (d) return d; \
> + /* d = n1 - n2; */ \
> + /* if (d) return d; */ \
> \
> - for (p = 0; p < n1 && s1[p] && s2[p]; p++) { \
> + /* for (p = 0; p < n1 && s1[p] && s2[p]; p++) { */ \
> + for (p = 0; p != n1; p++) { \
> d = t1 - t2; \
> if (d) return d; \
> + if (!s1[p]) return 0; \
> } \
> return 0; \
> }
--
Jonas Fonseca
_______________________________________________
elinks-dev mailing list
[email protected]
http://linuxfromscratch.org/mailman/listinfo/elinks-dev