Lazarus

Programming => General => Topic started by: mangakissa on January 20, 2020, 09:58:28 am

Title: Warnings with working XML
Post by: mangakissa on January 20, 2020, 09:58:28 am
Why is Lazarus / FPC still give this warning when working with XML:
Quote
Warning: Implicit string type conversion from "AnsiString" to "WideString"

I thought Lazarus 2.04/ FPC 3.04 Windows is fully UTF-8?
Code: Pascal  [Select][+][-]
  1. procedure CreateValueNode(aNode : TDOMNode; const aFieldName, aFieldValue : string);
  2. var NameNode   : TDOMNode;
  3.     ValueNode  : TDOMNode;
  4. begin
  5.   NameNode := XML.CreateElement(aFieldName);
  6.   ValueNode := XML.CreateTextNode(aFieldValue);
  7.   NameNode.AppendChild(ValueNode);
  8.   aNode.AppendChild(NameNode);
  9. end;
  10.  
Title: Re: Warnings with working XML
Post by: Thaddy on January 20, 2020, 10:54:13 am
Only Lazarus GUI apps. FPC itself is either Ansi or UTF16, although CP_UTF8 is supported for Ansi.
Title: Re: Warnings with working XML
Post by: rvk on January 20, 2020, 11:59:50 am
DOMString is defined as DOMString in FPC 3.0.4.
Code: Pascal  [Select][+][-]
  1.     function CreateTextNode(const data: DOMString): TDOMText;

And DOMString is XMLString which is WideString.
So the whole DOM and fcl-xml is still WideString oriented.

B.T.W. On january 20th 2019 the conversion from WideString -> UnicodeString is introduced in fcp-xml.
So in trunk and newer FPC version you won't get the warnings.
Title: Re: Warnings with working XML
Post by: wp on January 20, 2020, 12:12:26 pm
Which xml units did you use? IIRC, the laz2_* units of the LazUtils package (laz2_dom, laz2_xmlread, laz2_xmlwrite, laz2_xmlutils) should be for utf8 while the fpc units in packages/fcl-xml are for widestrings.
Title: Re: Warnings with working XML
Post by: mangakissa on January 20, 2020, 12:29:24 pm
I'm using DOM, XMLWrite, XMLRead which is standard configured in the lazarus package.
I didn't realised there was also a laz2* supporting UTF8.

Thanks all. I will change it in my application.
Title: Re: Warnings with working XML
Post by: PascalDragon on January 20, 2020, 09:17:54 pm
DOMString is defined as DOMString in FPC 3.0.4.
Code: Pascal  [Select][+][-]
  1.     function CreateTextNode(const data: DOMString): TDOMText;

And DOMString is XMLString which is WideString.
So the whole DOM and fcl-xml is still WideString oriented.

B.T.W. On january 20th 2019 the conversion from WideString -> UnicodeString is introduced in fcp-xml.
So in trunk and newer FPC version you won't get the warnings.
You are in so far right that there won't be a warning Implicit string type conversion from "AnsiString" to "WideString". However there'll now be a warning Warning: Implicit string type conversion from "AnsiString" to "UnicodeString". This is because an assignment does result in a conversion from either ANSI or UTF8 of the AnsiString to UTF16 of the UnicodeString (or WideString in the past). This might not be what the user desires (as it might result in a performance impact, especially if the codepage of the string is not UTF8 as then the conversion will likely be ANSI -> UTF8 -> UTF16), thus this warning exists.

To solve this simply add an explicit typecast to UnicodeString:

Code: Pascal  [Select][+][-]
  1.   NameNode := XML.CreateElement(UnicodeString(aFieldName));
  2.   ValueNode := XML.CreateTextNode(UnicodeString(aFieldValue));
TinyPortal © 2005-2018