Question

One would believe, looking at the name, that AnsiSameText defined in SysUtils (Delphi XE) will receive ANSI strings as parameters but the function is defined like this:

function AnsiSameText(const S1, S2: string): Boolean

What am I missing here?
There is an ANSI function in AnsiStrings unit, but still why is this one (in Sysutils) called 'ansi'?

Was it helpful?

Solution

In older versions of Delphi, pre-Unicode, there were two sets of string comparison functions:

  • SameText, CompareText, etc. These performed comparisons that ignore locale.
  • AnsiSameText, AnsiCompareText, etc. These performed comparisons that took locale into account.

When Unicode was introduced, these functions, which operate on string, now operate on UTF-16 data. For the sake of backwards compatibility, they retain the same names, and behave in the same way. That is SameText does not account for locale, but AnsiSameText does.

So, whilst the names are misleading, the Ansi prefix simply indicates that the function is locale aware. For what it is worth, in my view the Ansi prefix is poor even in pre-Unicode Delphi.

The reason that locale is important is that different locales have different rules for letter ordering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top