libCom: Fix epicsString.h comparison functions

The string comparison functions epicsStrCaseCmp() and
epicsStrnCaseCmp() were returning incorrect results
when the strings did not match.

These functions now match their BSD equivalents, and
have working tests to confirm their operation.
This commit is contained in:
Andrew Johnson committed 2014-03-11 17:12:41 -05:00
1 parent 4e1a5eefff
commit f5b9db9583
2 files changed
+15 -15

No files matched your search

+8 -8
View File
@@ -171,11 +171,11 @@ size_t epicsStrnEscapedFromRawSize(const char *inbuf, size_t inlen)
int epicsStrCaseCmp(const char *s1, const char *s2)
{
while (1) {
int ch1 = toupper(*s1);
int ch2 = toupper(*s2);
int ch1 = toupper((int) *s1);
int ch2 = toupper((int) *s2);
if (ch1 == 0) return (ch2 != 0);
if (ch2 == 0) return -1;
if (ch2 == 0) return (ch1 != 0);
if (ch1 == 0) return -1;
if (ch1 < ch2) return -1;
if (ch1 > ch2) return 1;
s1++;
@@ -188,11 +188,11 @@ int epicsStrnCaseCmp(const char *s1, const char *s2, size_t len)
size_t i = 0;
while (i++ < len) {
int ch1 = toupper(*s1);
int ch2 = toupper(*s2);
int ch1 = toupper((int) *s1);
int ch2 = toupper((int) *s2);
if (ch1 == 0) return (ch2 != 0);
if (ch2 == 0) return -1;
if (ch2 == 0) return (ch1 != 0);
if (ch1 == 0) return -1;
if (ch1 < ch2) return -1;
if (ch1 > ch2) return 1;
s1++;
+7 -7
View File
@@ -56,8 +56,8 @@ MAIN(epicsStringTest)
testOk1(epicsStrnCaseCmp(empty, "", 0) == 0);
testOk1(epicsStrnCaseCmp(empty, "", 1) == 0);
testOk1(epicsStrnCaseCmp(space, empty, 1) < 0);
testOk1(epicsStrnCaseCmp(empty, space, 1) > 0);
testOk1(epicsStrnCaseCmp(space, empty, 1) > 0);
testOk1(epicsStrnCaseCmp(empty, space, 1) < 0);
testOk1(epicsStrnCaseCmp(a, A, 1) == 0);
testOk1(epicsStrnCaseCmp(a, A, 2) == 0);
testOk1(epicsStrnCaseCmp(abcd, ABCD, 2) == 0);
@@ -65,17 +65,17 @@ MAIN(epicsStringTest)
testOk1(epicsStrnCaseCmp(abcd, ABCD, 1000) == 0);
testOk1(epicsStrnCaseCmp(abcd, ABCDE, 2) == 0);
testOk1(epicsStrnCaseCmp(abcd, ABCDE, 4) == 0);
testOk1(epicsStrnCaseCmp(abcd, ABCDE, 1000)> 0);
testOk1(epicsStrnCaseCmp(abcd, ABCDE, 1000) < 0);
testOk1(epicsStrnCaseCmp(abcde, ABCD, 2) == 0);
testOk1(epicsStrnCaseCmp(abcde, ABCD, 4) == 0);
testOk1(epicsStrnCaseCmp(abcde, ABCD, 1000) < 0);
testOk1(epicsStrnCaseCmp(abcde, ABCD, 1000) > 0);
testOk1(epicsStrCaseCmp(empty, "") == 0);
testOk1(epicsStrCaseCmp(a, A) == 0);
testOk1(epicsStrCaseCmp(abcd, ABCD) == 0);
testOk1(epicsStrCaseCmp(abcd, ABCDE) != 0);
testOk1(epicsStrCaseCmp(abcde, ABCD) != 0);
testOk1(epicsStrCaseCmp(abcde, "ABCDF") != 0);
testOk1(epicsStrCaseCmp(abcd, ABCDE) < 0);
testOk1(epicsStrCaseCmp(abcde, ABCD) > 0);
testOk1(epicsStrCaseCmp(abcde, "ABCDF") < 0);
s = epicsStrDup(abcd);
testOk(strcmp(s, abcd) == 0 && s != abcd, "epicsStrDup");