[Lazarus] Converting all code to use UnicodeString

Kostas Michalopoulos badsectoracula at gmail.com
Tue Sep 26 09:09:22 CEST 2017


I do not see how it is a hack, when you have a function taking a null
terminated string of a specific character type (in this case PWideChar) and
you only have the generic string type you don't know what format the
underlying memory of the string is so you cannot pass it as a pointer to
the function. In this case you need to convert to the explicit type.

These are (potentially) two different types, they just happen to be
strings. You could think of it as if you had a function Foo(VP: PSingle)
and a variable V: Number where Number could be either Single or Double
depending on some macro - but you don't know which one, so to avoid passing
a Double you'd need to assign it to a temporary variable to convert it to
the right type.

There is nothing wrong or hacky with that approach, this is how working
with functions that accept pointers work in general - you need to make sure
that the pointer you pass in is of the correct type.


On Tue, Sep 26, 2017 at 4:37 AM, Marcos Douglas B. Santos via Lazarus <
lazarus at lists.lazarus-ide.org> wrote:

> On Mon, Sep 25, 2017 at 9:52 PM, Juha Manninen via Lazarus
> <lazarus at lists.lazarus-ide.org> wrote:
> > On Tue, Sep 26, 2017 at 3:14 AM, Marcos Douglas B. Santos via Lazarus
> > <lazarus at lists.lazarus-ide.org> wrote:
> >> So, you mean that I cannot declare a constant without specify the
> >> type. The language allow me but it won't work?
> >
> > Yes you can declare a string constant without specifying the type.
>
> But according with this table, I shouldn't do that because so many
> problems could happen.
> http://wiki.freepascal.org/Unicode_Support_in_Lazarus#
> Without_.7B.24codepage_utf8.7D_or_compilerswitch_-FcUTF8
>
> >> 3.1. "When a parameter type is a pointer PWideChar,
> >> you need a temporary UnicodeString variable.
> >> ...
> >> That is a ugly hack. This code doesn't make any sense, if you don't
> >> know about these Unicode issues.
> >> We need do remember that trick when we are coding... not good.
> >
> > It is not so ugly. It is actually an elegant solution. Just one
> > assignment, using the FPC's automatic conversion in a clever way. No
> > explicit conversion functions or anything.
> > The "ugly" pointer typecast is needed always, also in Delphi.
>
> The "ugly" is because we need to remember to do that instead of just
> assign the variable.
> IMHO, both design are wrong. But I understand that the problem is in
> the compiler — or RTL.
>
> >> 4. "Reading / writing text file with Windows codepage"
> >> ...
> >> The text said: "This is not compatible with Delphi ".
> >> Examples on that page are hacks.
> >
> > The solution is to NOT use Windows codepages. They can be seen as a
> > historical remain with severe inherent problems which are solved by
> > Unicode already a long ago.
> > Windows has supported full Unicode since year 2000, and supported
> > UCS-2 before that.
> > Why would anybody still use the historical Windows codepages?
>
> So, no problems here and the page is outdated. OK.
>
> >> Summary:
> >> I know that was a huge work for who made that. Lazarus is more
> >> Unicode, more compatible with Delphi, and the team could move on.
> >> Great.
> >> But you might agree with me that this is far from a good design, right?
> >
> > IMO it is not far from a good design. From FPC's point of view it is a
> > hack but you can write 100% Delphi compatible code by following just
> > few simple rules (and dumping the historical Windows codepages).
>
> Like I said, it's a hack. But, again, it was|is a great job. No doubt.
>
> Best regards,
> Marcos Douglas
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus-ide.org
> https://lists.lazarus-ide.org/listinfo/lazarus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170926/30a79d58/attachment.html>


More information about the Lazarus mailing list