Recent

Author Topic: Warnings with working XML  (Read 291 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 970
Warnings with working XML
« 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.  
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

Thaddy

  • Hero Member
  • *****
  • Posts: 9637
Re: Warnings with working XML
« Reply #1 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.
I am more like donkey than shrek

rvk

  • Hero Member
  • *****
  • Posts: 3918
Re: Warnings with working XML
« Reply #2 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.

wp

  • Hero Member
  • *****
  • Posts: 6723
Re: Warnings with working XML
« Reply #3 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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

mangakissa

  • Hero Member
  • *****
  • Posts: 970
Re: Warnings with working XML
« Reply #4 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.
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

PascalDragon

  • Hero Member
  • *****
  • Posts: 963
  • Compiler Developer
Re: Warnings with working XML
« Reply #5 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));