String comparaison

Hi,

In UnrealString.h , FString::Equals(…), i noticed that the code miss the opportunity of an early exit by comparing string lengths.


if (Len() != Other.Len())
return false;

This would be faster and would avoid the cache destruction by the use of standard Strcmp(…) function.

Is there some information i don’t have ?

Thanks

I noticed that function leads through all of the platform specific string functions which is all sorts of wrapped up in macros, which are difficult to track down the actual conditions and code that is returning the result of:

FCString::Strcmp

or

FCString::Stricmp

My guess is that they are instead relying on the platform code to do its job, though I agree with you that a check at the start could work.

Maybe you could put in a pull request and see if the engine team would want to add it?

I had an extra thought about the problem.

A string content can be changed with the operator [].

mystring[3] = 0; // if string length is greater than 3 of course

it’s a bad idea because mystring.Len() would return a bad answer but it is still possible.

So the length comparaison for an early exit is not correct.

I checked my implementation of STL, and the same behavior occurs. You can change the content of a string and make its length an invalid value.

So I suppose we should not change anything in UE4.

Hey!
You can compare length manually if you want with: FString::Len | Unreal Engine Documentation

Length compare not always 100% accurate.
Strings are character arrays, one character stored as one byte always as i know, so comparig length no matter if it is character array length or byte length can give false result.

For example: Six and Sex are 3 character and 3 byte but not equal :slight_smile:

Strcmp also compare char by char if i remember good :wink:

Good call.

Thanks for the explanation.