diff --git a/libc/str/wcscmp.c b/libc/str/wcscmp.c index 33abf540a..2ac448237 100644 --- a/libc/str/wcscmp.c +++ b/libc/str/wcscmp.c @@ -30,5 +30,5 @@ int wcscmp(const wchar_t *a, const wchar_t *b) { size_t i = 0; if (a == b) return 0; while (a[i] == b[i] && b[i]) ++i; - return (unsigned)a[i] - (unsigned)b[i]; + return (a[i] > b[i]) - (a[i] < b[i]); } diff --git a/libc/str/wcsncmp.c b/libc/str/wcsncmp.c index e12eda01e..fba450993 100644 --- a/libc/str/wcsncmp.c +++ b/libc/str/wcsncmp.c @@ -30,5 +30,5 @@ int wcsncmp(const wchar_t *a, const wchar_t *b, size_t n) { size_t i = 0; if (!n-- || a == b) return 0; while (i < n && a[i] == b[i] && b[i]) ++i; - return (unsigned)a[i] - (unsigned)b[i]; + return (a[i] > b[i]) - (a[i] < b[i]); } diff --git a/test/libc/str/strcmp_test.c b/test/libc/str/strcmp_test.c index f7ceb4ff1..ada05d0f8 100644 --- a/test/libc/str/strcmp_test.c +++ b/test/libc/str/strcmp_test.c @@ -460,11 +460,11 @@ TEST(wcscmp, testTwosComplementBane) { EXPECT_EQ(wcscmp(memcpy(B1, "\x00\x00\x00\x80", 4), memcpy(B2, "\x00\x00\x00\x80", 4)), 0); - EXPECT_EQ(-1, wcscmp(memcpy(B1, "\xff\xff\xff\x7f", 4), - memcpy(B2, "\x00\x00\x00\x80", 4))); - EXPECT_EQ(wcscmp(memcpy(B1, "\x00\x00\x00\x80", 4), + EXPECT_LT(0, wcscmp(memcpy(B1, "\xff\xff\xff\x7f", 4), + memcpy(B2, "\x00\x00\x00\x80", 4))); + EXPECT_LT(wcscmp(memcpy(B1, "\x00\x00\x00\x80", 4), memcpy(B2, "\xff\xff\xff\x7f", 4)), - 1); + 0); free(B2); free(B1); } @@ -475,12 +475,12 @@ TEST(wcsncmp, testTwosComplementBane) { EXPECT_EQ(wcsncmp(memcpy(B1, "\x00\x00\x00\x80", 4), memcpy(B2, "\x00\x00\x00\x80", 4), 1), 0); - EXPECT_EQ(wcsncmp(memcpy(B1, "\xff\xff\xff\x7f", 4), + EXPECT_GT(wcsncmp(memcpy(B1, "\xff\xff\xff\x7f", 4), memcpy(B2, "\x00\x00\x00\x80", 4), 1), - -1); - EXPECT_EQ(wcsncmp(memcpy(B1, "\x00\x00\x00\x80", 4), + 0); + EXPECT_LT(wcsncmp(memcpy(B1, "\x00\x00\x00\x80", 4), memcpy(B2, "\xff\xff\xff\x7f", 4), 1), - 1); + 0); free(B2); free(B1); }