[Lazarus] Converting all code to use UnicodeString

Marcos Douglas B. Santos md at delfire.net
Mon Sep 25 20:02:43 CEST 2017


Hi,

Yes, yes... another email about Unicode, because it has not been
completed yet. Sorry.
I would like to know how I can develop CLI and GUI (Lazarus) programs
using just UnicodeString, making them all compatible with Delphi
compiler.

My environment is:
Lazarus 1.9.0 r54784 FPC 3.0.1 i386-win32-win32/win64

I thought that I could use this:

{$mode delphi}
{$modeswitch unicodestrings}

Then, I made some tests:

1- Writing a CLI with basic chars:

===code-begin===
program Project1;
{$mode delphi}
{$modeswitch unicodestrings}
uses
  SysUtils, Classes;
const
  TXT = '1'#13#10'2'#13#10'3';
var
  Ss: TStrings;
begin
  Ss := TStringList.Create;
  try
    Ss.Text := TXT;
    Writeln('text ', ss.Text);
    Writeln('count ', ss.Count);
  finally
    Ss.Free;
  end;
  ReadLn;
end.
===code-end===

===output-begin===
text 1
2
3

count 3
===output-end===

Everything worked. No warnings. Good.


Then I changed the const like this:
===code-begin===
const
  TXT: string = '1'#13#10'2'#13#10'3';
===code-end===

Everything worked. But now I have a warning:
project1.lpr(13,19) Warning: Implicit string type conversion with
potential data loss from "UnicodeString" to "AnsiString"

Why?
Is not String supposed to be UnicodeString?
Is TStrings ANSI and because that I got this warning?


Then I changed the const like this:
===code-begin===
const
  TXT = '1'#13#10'2'#13#10'3'#13#10'áéíóú';
===code-end===


And:
===output-begin===
text 1
2
3
áéíóú

count 4
===output-end===

Is it not possible to write accented chars to display at the console?




2- Writing a GUI with basic chars:

I just copy the same code with some modifications:
===code-begin===
unit Unit1;

{$mode delphi}
{$modeswitch unicodestrings}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

const
  TXT = '1'#13#10'2'#13#10'3'#13#10;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  Ss: TStrings;
begin
  Ss := TStringList.Create;
  try
    Ss.Text := TXT;
    ShowMessage('text ' + ss.Text);
    ShowMessage('count ' + ss.Count.ToString);
  finally
    Ss.Free;
  end;
end;

end.
===code-end===


Everything worked. But now I have a few warnings:

Compile Project, Target: C:\temp\project1.exe: Success, Warnings: 4
unit1.pas(36,29) Warning: Implicit string type conversion from
"AnsiString" to "UnicodeString"
unit1.pas(36,34) Warning: Implicit string type conversion with
potential data loss from "UnicodeString" to "AnsiString"
unit1.pas(37,30) Warning: Implicit string type conversion from
"AnsiString" to "UnicodeString"
unit1.pas(37,45) Warning: Implicit string type conversion with
potential data loss from "UnicodeString" to "AnsiString"


Then, I changed the const like this (again):
===code-begin===
const
  TXT: string = '1'#13#10'2'#13#10'3';
===code-end===

Everything worked. But now I have same warnings before.



Then, I changed the const like this (yeah, again):
===code-begin===
const
  TXT = '1'#13#10'2'#13#10'3'#13#10'áéíóú';
===code-end===

Everything worked. But now I have same warnings before.


Finally ,I changed the const like this:
===code-begin===
const
  TXT: string = '1'#13#10'2'#13#10'3'#13#10'áéíóú';
===code-end===

And now, the first message is:
[Window Title]
project1

[Content]
text 1
2
3
áéíóú



Summary:

1. I did simple programs with simple constants and I got different
results or warnings.

2. I truly believe that smarter people as FPC team, Lazarus team, and
all smart collaborators can code CLI, GUI and all compatible with
Delphi... but for me is still very hard to understand.


What I need and my thoughts for help you to help me:

1. I would like to use just "string" everywhere.
2. I DON'T care about performance like gain 2ms... 10ms... 1s... I
really don't care, if I can code a simple and elegant code.
3. Don't think external files. If a have a UTF8 encoded file, I know
that and I can use string conversion... but it is EXTERNAL, not part
of the code. So, in my mind this is OK and normal.

Again, my env is:
Lazarus 1.9.0 r54784 FPC 3.0.1 i386-win32-win32/win64


May I code using just "string"?

Thank you.

Best regards,
Marcos Douglas


More information about the Lazarus mailing list