diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-08-12 16:09:32 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-08-12 16:09:32 -0400 |
commit | 556843458a3b38f1efe536fdb7ff3b0f65538e0f (patch) | |
tree | 3a50873293a29b2a4e2132e10dbfab61c3b18bda /src/string/strcmp.c | |
parent | 24ca03515a8c1c8e640231650254372b31a8a809 (diff) |
fix to be solf-contained and more reliable
Diffstat (limited to 'src/string/strcmp.c')
-rw-r--r-- | src/string/strcmp.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/string/strcmp.c b/src/string/strcmp.c index fdceeb67..f8863fba 100644 --- a/src/string/strcmp.c +++ b/src/string/strcmp.c @@ -4,21 +4,32 @@ /** compare strings **/ int strcmp(const char *s1, const char *s2) { - size_t n1 = 0; - size_t n2 = 0; - ASSERT_NONNULL(s1); ASSERT_NONNULL(s2); - n1 = strlen(s1); - n2 = strlen(s2); + while (*s1 && *s2) { + if (*s1 != *s2) { + return *s1 - *s2; + } + s1++; + s2++; + } /* RETURN(NEGATIVE, ARGUMENT(s1) is less than ARGUMENT(s2)); RETURN(ZERO, ARGUMENT(s1) is equal to ARGUMENT(s2)); RETURN(POSITIVE, ARGUMENT(s1) is greater than ARGUMENT(s2)); */ - return strncmp(s1, s2, n1 < n2 ? n1 : n2); + + if (*s1) { + return -1; + } + + if (*s2) { + return 1; + } + + return 0; } /*** |