I have to process text that contains non-ASCII characters. I understand that the String type in Free Pascal is keyed to UTF-8 by default. I don’t require Delphi compatibility, so mode Delphi isn’t essential for me.
Consider this bit of code:
program UniCode;
{$H+}{$codepage utf8}
uses Classes, SysUtils, LazUTF8;
function IsNonAscii(const S : String; const aPos: integer): Boolean;
const NonAsciiChar = 'é';
//comment the line above and uncomment the line below to get rid of the warning:
//const NonAsciiChar : String = 'é';
var c : String;
begin
c := UTF8Copy(s,apos,1);
Result := c = NonAsciiChar;
end;
begin
end.
When I compile this code, the compiler issues the following warning: UniCode.lpr(12,13) Warning: Implicit string type conversion from “AnsiString” to “WideString”. However, if I switch to the typed constant, the warning disappears.
I don’t get it: even though I’m specifying $codepage utf8, why is the compiler defaulting string constants to WideString instead of String? Is there a way to switch that?
When I compile this, I get