When do I start considering the use of UnicodeString rather than AnsiString? lazarus / 62-General ---------------------------------------------------------------------- vfclists · 2024-02-08 16:26 I have been getting warnings about copying the Text property from a TMemo to the ATSynEditor which is about the  conversion of AnsiString to UnicodeString. Giving that UnicodeString is where it all seems to be now but Lazarus and Delphi have a big AnsiString legacy what I am supposed to do now? How much of the AnsiString legacy in Lazarus and Delphi should I be careful in carrying around if I'm going to do a lot of work in Linux where it is all the rage. Point is as most of my coding is in English I don't think it may bother me that much unless I want to use emojis (mysql utf8mb4 et el) but I can't be too careful. Can may of the Lazarus text-based controls be switched to UTF mode or do they have UTF counterparts? ---------------------------------------------------------------------- Thaddy · 2024-02-08 16:39 Lazarus already handles Ansi(CP_UTF8) as standard. If you program Lazarus GUI applications all strings are already Unicode by definition. UnicodeString is a Unicode16 string type which is different from UTF8. ---------------------------------------------------------------------- Nicole · 2024-02-08 17:52 I moved from Delphi as well. In the meanwhile I forgot about those hours and days I spent in changing string-types and dealing with all those warnings. I did this after having Delphi left behind: I changed all parameters to plain "string" and this seemed to have worked that nice, that I forgot about this topic completely. Where it is important: If you do a lot with compare and uppercase and lower case. There are different ways of comparing and editing strings by functions. I solved this one by one. There are lists around, which function does what. Case you need it, please search my posting for attachments with it. Then it is important with handles. Those are sensitive, which format of signs to take as parameters. Please post your troubles one by one. We solve them all. ---------------------------------------------------------------------- JuhaManninen · 2024-02-08 18:49 Quote from: Nicole on February 08, 2024, 06:52:55 pmIf you do a lot with compare and uppercase and lower case. There are different ways of comparing and editing strings by functions. I solved this one by one. There are lists around, which function does what. The Ansi... functions are compatible with Delphi despite the different encodings. Eg. AnsiLowercase, AnsiCompareText. It means that you can have code that supports Unicode yet is 100% compatible with Delphi. See also : https://wiki.freepascal.org/Unicode_Support_in_Lazarus ---------------------------------------------------------------------- vfclists · 2024-02-08 20:29 I get this error message when I compile one of my older projects Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   qryData.SQL.Text := atsynEditor.Text; QuotercaTblBrowser04.pas(1327,39) Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString" qryData is TZeosQuery This code is more interesting because scriptText is a TStringList. Are the lists of a TStringList the AnsiString type and is there a Unicode version of TStringList? QuotercaTblBrowser04.pas(1405,38) Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString" Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  scriptText.Text := atSynEditor.Text;  QuotercaTblBrowser04.pas(1408,41) Warning: Implicit string type conversion from "AnsiString" to "UnicodeString" Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   atSynEditor.Text := scriptText.Text; ---------------------------------------------------------------------- ASerge · 2024-02-08 20:49 Quote from: vfclists on February 08, 2024, 09:29:53 pmrcaTblBrowser04.pas(1327,39) Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString" As @Thaddy said, Lazarus fully supports Unicode, but in ansi string format, via CP_UTF8 encoding, explicitly force this at startup. But the compiler, by default, assumes that the program uses native CP_ACP, so it issues a warning. Delphi supports Unicode only in UTF16 format, so only UnicodeString is used. In Lazarus when strings of different types are encountered, UTF8 <-> UTF16 is automatically converted. ---------------------------------------------------------------------- marcov · 2024-02-08 21:18 Quote from: ASerge on February 08, 2024, 09:49:22 pmQuote from: vfclists on February 08, 2024, 09:29:53 pmrcaTblBrowser04.pas(1327,39) Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString" As @Thaddy said, Lazarus fully supports Unicode, but in ansi string format, via CP_UTF8 encoding, explicitly force this at startup. But that is still a hack and assumes all communication with the OS goes over 2-byte functions (which the FPC 3.0+ RTL does, but be careful with direct unit windows use).  Lazarus also did this for the GUI controls, kind of like Delphi's TNT component suite. An even more complete solution is to set the windows 10-1905 "standard codepage is utf8" flag (in the manifest, see the "application" screen in "project options", which means that also winapi mostly becomes utf8) Of course this does raise the minimal windows version and excludes some versions nowadays unsupported by Microsoft, which can be a problem if you have old XP or win7 machines attached to specific hardware. But if you can live with that it is a very nice solution. The only thing to pay attention to is console output, as that was always OEM encoding rather than ANSI. I don't know how much of this works nowadays in windows 10, with Microsoft nearly completely rewriting the console handling to handle the WSL stuff. It might also depend on the exact console handler (cmd.exe vs cmd.exe /w vs powershell vs windows 11 "Terminal").  While I am dependent on utf8 console programs, it is mostly the file handling that I was after, not the terminal output, so I never dove into that. ---------------------------------------------------------------------- Thaddy · 2024-02-09 07:08 Quote from: vfclists on February 08, 2024, 09:29:53 pmQuotercaTblBrowser04.pas(1327,39) Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "AnsiString" QuotercaTblBrowser04.pas(1408,41) Warning: Implicit string type conversion from "AnsiString" to "UnicodeString"As is now  well explained, a Lazarus GUI app IS unicode, but unicode8. You can uncheck both warnings (NOT errors) In project options messages if you just use string for strings. You can also do that in your sourcecode:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$Mode objfpc}{$H+}{$codepage utf8}{$warn 4104 off}{$warn 4105 off}var   a:string = 'test'; //utf8 string literal  b:unicodestring ='test2';begin  b:=a;  a:=b;end. ---------------------------------------------------------------------- vfclists · 2024-02-09 17:09 Is string limited to 255 characters or is that ShortString? I take it that if string defaults to Unicode8 then there is no need to set a length on such a string. So if you want to set a maximum length of the string what is the syntax used? My programming for my personal needs is in Linux which I assume has fewer issues with Unicode, but I have to develop and run programs in Window and Delphi, and just want to ensure that things don't go horribly wrong there. I remember the lengthy sometimes fractious discussions on Unicode strings way back in the past and I assume they've all been resolved now. Are there some exercises or sample programs that can demonstrate the behaviours related to all the various kinds of strings so they can be properly understood? They don't have to be in FreePascal but that would be preferred. ---------------------------------------------------------------------- Remy Lebeau · 2024-02-09 19:50 Quote from: ASerge on February 08, 2024, 09:49:22 pmDelphi supports Unicode only in UTF16 format, so only UnicodeString is used. Delphi also has UTF8String for UTF-8 strings.  But the RTL/VCL/FMX frameworks mostly use UnicodeString, yes.  But it will implicitly convert between the two. Quote from: ASerge on February 08, 2024, 09:49:22 pmIn Lazarus when strings of different types are encountered, UTF8 <-> UTF16 is automatically converted. Same in Delphi, too. Quote from: vfclists on February 09, 2024, 06:09:37 pmIs string limited to 255 characters or is that ShortString? Yes, only ShortString. Quote from: vfclists on February 09, 2024, 06:09:37 pmI take it that if string defaults to Unicode8 then there is no need to set a length on such a string. So if you want to set a maximum length of the string what is the syntax used? You can't set a maximum length on a (Ansi|Unicode)String.  You can pre-allocate such a string using SetLength(), but you can still add on to it afterwards to grow it further.