Talk:Strcmp
From SA-MP Wiki
Not sure if anyone will read this, but eh.
The so called "bug" where an empty string compared to a different string would return false (0) is not a bug at all.
strcmp indeed compares the string, but it doesn't simply compare and return 0/1. It returns the number of characters which are different. When you're comparing an empty string to anything, the result will always be 0, because technically no characters are different between the strings due to the fact that there are none in one of the test strings.
From my testing, this is what I found:
- When you compare a blank string to anything, return value is 0 (false)
- When you compare two equal strings, case-sensitive, return value is 0 (false)
- When you compare two case-insensitive-ly equal strings with case insensitive off (false), return value is -1 (true)
- When you compare two case-insensitive-ly equal strings with case insensitive on (true), return value is 0 (false)
- If string 1 is in string 2, then string 2 has extra characters afterward, return value will be -x (true), where x is the number of extra characters. It will be a positive return value if string 2 is in string 1, and string 1 has extra characters after (true).
- If string 1 is in string 2, then string 2 has extra characters before, return value will be -1 (true). Opposite (string 2 in string 1, string 1 has extra chars) will return 1 (true)
- Two same-length completely different strings will return 1 (true)
- When you compare two different strings with the same length, (not already covered by anything stated above) return value will be 1 (true)
- When you compare two different strings with different lengths, (not already covered by anything stated above) return value will be -1 (true)
Could be all possible return values, or there could be others.
I decided not to post this all directly in the page, perhaps somebody else would like to, in a more informative or user-friendly manner.
- ev0lution 08:50, 27 March 2010 (UTC)
Return Values
According to this PDF file, the return value is either -1, 1 or 0, depending on where string2 occurs in string1.
--Vince 21:08, 15 November 2010 (CET)