Recent

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

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
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 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
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.
Specialize a type, not a var.

rvk

  • Hero Member
  • *****
  • Posts: 6056
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: 11830
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.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
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 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • 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));

 

TinyPortal © 2005-2018