Game Development Community

stricmp

by Thomas Bang · in Torque 3D Professional · 01/24/2011 (2:32 am) · 6 replies

$result = stricmp("dugi","fugi");
echo($result);

Why do i get -2 as result? It should be -1.

#1
01/24/2011 (6:06 am)
No, -2 is right, because there is a 2 letters difference from 'd' to 'f'.

The 'strcmp' string comparison family returns 0 for equality, and the first difference spotted if the strings are different.

Here is the source code for strcmp function:
int
strcmp (p1, p2)
     const char *p1;
     const char *p2;
{
  register const unsigned char *s1 = (const unsigned char *) p1;
  register const unsigned char *s2 = (const unsigned char *) p2;
  unsigned reg_char c1, c2;

  do
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == '\0')
        return c1 - c2;
    }
  while (c1 == c2);

  return c1 - c2;
}

It's not the stricmp function, but they are very close.

Nicolas Buquet
www.buquet-net.com/cv/
#2
01/24/2011 (3:27 pm)

Yep, exactly. For reference: VC _stricmp
#3
01/25/2011 (3:10 am)
@Nicolas & Rene: Thank you very much.
#4
01/25/2011 (8:32 am)
Just to point out:
The Torque3D Script manual says:
int stricmp  ( string  str1,  
  string  str2   
 )    

Compares two strings using case-insensitive comparison. 

Parameters:
 str1  The first string.  
 str2  The second string.  

Returns:
0 if both strings are equal, -1 if the first character different in str1 has a smaller character code value than the character at the same position in str2, and 1 otherwise.
Example:
if( stricmp( "FOObar", "foobar" ) == 0 )
   echo( "this is always true" );
See also:
strcmp 
strinatcmp

And that doesn't say anything about returning -2 to -25.
#5
01/25/2011 (9:18 pm)

Ha, that would be my effup. Thanks for posting Steve. I'll fix the docs.

Looking at it, I actually made this mistake on several of the string functions.
#6
01/25/2011 (9:43 pm)
Rene,
I always use == 0 or != 0 for my string comparisons, so if Thomas hadn't of spotted it I certainly wouldn't!