This unit test runs successfully with Free Pascal 3.0 in Delphi mode:
procedure TFreePascalTests.TestUTF8Decode;
var
Raw: RawByteString;
Actual: string;
begin
Raw := UTF8Encode('关于汉语');
Actual := string( UTF8Decode(Raw) ); // <--- cast from UnicodeString
CheckEquals('关于汉语', Actual);
// check Windows ANSI code page
CheckEquals(1252, GetACP);
// check Free Pascal value (determines how CP_ACP is interpreted)
CheckEquals(65001, DefaultSystemCodePage);
end;
UTF8Decode returns a UnicodeString. Without the hard type cast to string, the compiler warns about an unsafe conversion:
Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString"
(tested with Lazarus 1.6 / FPCUnit GUITestrunner)
As per http://wiki.freepascal.org/Character_and_string_types#String, the string type defaults to AnsiString (if the {$H+} switch is set to use AnsiString instead of ShortString).
It looks like Free Pascal stores the Unicode string in the AnsiString variable. (even without the cast, the test succeeds)
Question: as the test succeeds, can I assume that it is safe to use the cast (to suppress the warning) without risking data loss?