[fpc-pascal] PChars and AnsiString Code Pages

Jonas Maebe jonas.maebe at elis.ugent.be
Wed May 18 15:27:34 CEST 2016


Tony Whyman wrote on Wed, 18 May 2016:

> I've been looking at some old code which still seems to work in fpc  
> 3.0.0 - but is it safe? The code looks like:
>
> {$mode delphi}
>
> var Buf: PChar;
>       s: string;
> begin
>   Buf := LoadString; {returns a pointer to an arbitrarily long null  
> terminated string}
>
>   s := string(Buf); {seems to work - but should it?}
> end;

Why shouldn't it?

> The above has been around since Delphi days and has never caused  
> trouble, but I am certainly wondering if this coercion is safe  
> anymore given the introduction of AnsiString code pages.

Why would that make any difference?

In particular, see  
http://wiki.freepascal.org/FPC_Unicode_support#PAnsiChar.2FAnsiChar  
and the definition of "plain ansistring" in  
http://wiki.freepascal.org/FPC_Unicode_support#Declared_code_page

To repeat what I said in about every message in the previous  
disinformation thread on this topic: everything will work exactly the  
same in FPC 2.6.4 and FPC 3.0 if you use the same types (with the same  
effective modes/modeswitch) in both FPC versions, except for  
utf8string (which is an alias for ansistring in FPC 2.6.4, and an  
ansistring(CP_UTF8) in FPC 3.0).


Jonas



More information about the fpc-pascal mailing list