From 327b026e4a103f5bbfe29b91b72a193ec7821604 Mon Sep 17 00:00:00 2001 From: "David G. Holm" Date: Sat, 15 May 1999 01:11:08 +0000 Subject: [PATCH] See ChangeLog entry 19990514-20:10 EDT David G. Holm --- harbour/ChangeLog | 6 +++++ harbour/source/rtl/strcmp.c | 18 ++++++++++---- harbour/tests/working/strings3.prg | 40 ++++++++++++++---------------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index d6772f65ac..77554a268e 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -1,3 +1,9 @@ +19990514-20:10 EDT David G. Holm + * source/rtl/strcmp.c + - Added SET EXACT ON rules for null strings and trailing spaces. + * tests/working/strings3.prg + - Added tests for null strings and trailing spaces. + 19990515-01:15 CET Eddie Runia * source/compiler/harbour.y #include again diff --git a/harbour/source/rtl/strcmp.c b/harbour/source/rtl/strcmp.c index 1576d59b0f..9897fcf535 100644 --- a/harbour/source/rtl/strcmp.c +++ b/harbour/source/rtl/strcmp.c @@ -12,10 +12,17 @@ int hb_itemStrCmp( PITEM pFirst, PITEM pSecond, BOOL bForceExact ) char *szSecond = pSecond->value.szText; long lLenFirst = pFirst->wLength; /* TODO: change ITEM.wLength from WORD to long */ long lLenSecond = pSecond->wLength; /* TODO: change ITEM.wLength from WORD to long */ - long lMinLen = lLenFirst < lLenSecond ? lLenFirst : lLenSecond; + long lMinLen; long lCounter; int iRet = 0; /* Current status */ + if (hb_set.HB_SET_EXACT && !bForceExact) + { /* SET EXACT ON and not using == */ + /* Don't include trailing spaces */ + while( lLenFirst > 0 && szFirst[ lLenFirst - 1 ] == ' ') lLenFirst--; + while( lLenSecond > 0 && szSecond[ lLenSecond - 1 ] == ' ') lLenSecond--; + } + lMinLen = lLenFirst < lLenSecond ? lLenFirst : lLenSecond; if( lMinLen ) /* One of the strings is empty */ { for( lCounter = 0; lCounter < lMinLen && !iRet; lCounter++ ) @@ -28,19 +35,20 @@ int hb_itemStrCmp( PITEM pFirst, PITEM pSecond, BOOL bForceExact ) szSecond++; } } -/* printf ("\nhb_itemStrCmp: iRet = %d, lCounter = %ld, lLenFirst = %ld, lLenSecond = %ld", iRet, lCounter, lLenFirst, lLenSecond); */ if( hb_set.HB_SET_EXACT || bForceExact || lLenSecond > lCounter ) { /* Force an exact comparison */ if( !iRet && lLenFirst != lLenSecond ) /* If length is different ! */ - iRet = (lLenFirst < lLenSecond) ? -1 : 1; + iRet = (lLenFirst < lLenSecond) ? -1 : 1; } -/* printf ("\n; hb_set.HB_SET_EXACT = %d, bForceExact = %d, iRet = %d.\n", hb_set.HB_SET_EXACT, bForceExact, iRet); */ } else { if( lLenFirst != lLenSecond ) /* Both empty ? */ - iRet = (lLenFirst < lLenSecond) ? -1 : 1; + if( hb_set.HB_SET_EXACT || bForceExact ) + iRet = (lLenFirst < lLenSecond) ? -1 : 1; + else + iRet = (lLenSecond == 0) ? 0 : -1; else iRet = 0; /* Both empty => Equal ! */ } diff --git a/harbour/tests/working/strings3.prg b/harbour/tests/working/strings3.prg index 2133504402..8b4519f98a 100644 --- a/harbour/tests/working/strings3.prg +++ b/harbour/tests/working/strings3.prg @@ -9,7 +9,7 @@ local cTest, nI, nJ, crlf := CHR(13)+CHR(10) OUTSTD (cStr) OUTSTD (UPPER (cStr)) OUTSTD (LOWER (cStr)) - OUTSTD (CHR (13) + CHR (10)) + OUTSTD (crlf) OUTSTD (ASC (SUBSTR (cStr, 8))) OUTSTD (ASC (SUBSTR (cStr, 9))) OUTSTD (ASC (SUBSTR (cStr, 10))) @@ -76,45 +76,40 @@ local cTest, nI, nJ, crlf := CHR(13)+CHR(10) OUTSTD (crlf) // Test the string comparison operators in the HVM. - OUTSTD ("EXACT ") - IF SET (_SET_EXACT) - OUTSTD ("ON") - ELSE - OUTSTD ("OFF") - ENDIF - StrTest ("ABC", "ABC") - StrTest ("ABC", "ABCD") - StrTest ("ABCD", "ABC") - StrTest ("ABC", "DEF") - StrTest ("ABC", "DEFG") - StrTest ("ABCD", "DEF") + // Note: SET (_SET_EXACT) defaults to .F. + TestStr () OUTSTD (crlf) OUTSTD (crlf) - OUTSTD ("EXACT ") - SET (_SET_EXACT, .T.) + TestStr () + OUTSTD (crlf) +return nil + +function TestStr () + OUTSTD ("EXACT ") IF SET (_SET_EXACT) OUTSTD ("ON") ELSE OUTSTD ("OFF") ENDIF + StrTest ("ABC", "") + StrTest ("ABC", " ") StrTest ("ABC", "ABC") StrTest ("ABC", "ABCD") - StrTest ("ABCD", "ABC") + StrTest ("ABC", "ABC ") StrTest ("ABC", "DEF") StrTest ("ABC", "DEFG") StrTest ("ABCD", "DEF") - OUTSTD (crlf) - OUTSTD (crlf) return nil function StrTest (Str1, Str2) OUTSTD (CHR(13)+CHR(10)+CHR(10)) + OUTSTD ("'") OUTSTD (Str1) - OUTSTD (", ") + OUTSTD ("', '") OUTSTD (Str2) - OUTSTD (" == ") + OUTSTD ("' == ") OUTSTD (Str1 == Str2) OUTSTD (" = ") OUTSTD (Str1 = Str2) @@ -130,10 +125,11 @@ function StrTest (Str1, Str2) OUTSTD (Str1 >= Str2) OUTSTD (CHR(13)+CHR(10)) + OUTSTD ("'") OUTSTD (Str2) - OUTSTD (", ") + OUTSTD ("', '") OUTSTD (Str1) - OUTSTD (" == ") + OUTSTD ("' == ") OUTSTD (Str2 == Str1) OUTSTD (" = ") OUTSTD (Str2 = Str1)