Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AnsiSameText is not ANSI?

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'?

like image 266
Server Overflow Avatar asked Dec 20 '22 15:12

Server Overflow


1 Answers

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.

like image 53
David Heffernan Avatar answered Dec 24 '22 01:12

David Heffernan