Recent

Author Topic: String helper  (Read 7544 times)

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
String helper
« on: October 21, 2016, 04:44:30 pm »
Hi, I have fpc trunk and it contains AnsiString helper but only works with ansi characters. There will be in future a String helper that works with utf-8 strings?

Code: Pascal  [Select][+][-]
  1. var
  2.   s: string;
  3. begin
  4.   s := 'cañón';
  5.   ShowMessage(s.ToUpper); // Shows CAñóN, must be CAÑÓN

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: String helper
« Reply #1 on: October 21, 2016, 05:07:52 pm »
Not an answer to your question, but what's wrong with UpperCase(s) (i.o.w. why do we need string.ToUpper)?

Bart

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: String helper
« Reply #2 on: October 21, 2016, 05:13:47 pm »
Not an answer to your question, but what's wrong with UpperCase(s) (i.o.w. why do we need string.ToUpper)?

Bart

Nothing wrong.

But my question goes like, if they are adding Helpers to AnsiString and to a lot of types, will they add support to UTF8 Strings too?

There are a lot of new helpers, and thanks to pascal we can use both worlds, functions or the dot followed by a method.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: String helper
« Reply #3 on: October 21, 2016, 06:15:39 pm »
LazUtf8 maps AnsiUpperCase to Utf8Uppercase IIRC?
If your strings codepage is UTF8 and the result codepage is UTF8 it should work (if .ToUpper uses AnsiUppeCase internally).

Bart

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: String helper
« Reply #4 on: October 21, 2016, 06:29:40 pm »
Adding LazUTF8 does nothing.

To upper uses this:

Code: Pascal  [Select][+][-]
  1. class function TStringHelper.UpperCase(const S: string): string;
  2. begin
  3.   Result:=sysutils.Uppercase(S);
  4. end;

That is here:

Code: Pascal  [Select][+][-]
  1. {   UpperCase returns a copy of S where all lowercase characters ( from a to z )
  2.     have been converted to uppercase   }
  3. Function UpperCase(Const S : AnsiString) : AnsiString;
  4.   begin
  5.     Result:=InternalChangeCase(S,['a'..'z'],-32);
  6.   end;

So it is ansi only.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: String helper
« Reply #5 on: October 21, 2016, 07:03:15 pm »
UnicodeUpperCase

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: String helper
« Reply #6 on: October 21, 2016, 07:04:18 pm »
That sucks then.
No idea why it does not use AnsiUpperCase.

Bart

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: String helper
« Reply #7 on: October 21, 2016, 07:10:13 pm »
UnicodeUpperCase

Yes it works, also works the UTF8UpperCase and AnsiUpperCase:

Code: Pascal  [Select][+][-]
  1. var
  2.   s: string;
  3. begin
  4.   s := 'cañón';
  5.   ShowMessage(UnicodeUpperCase(s));
  6.   ShowMessage(UTF8UpperCase(s));
  7.   ShowMessage(AnsiUpperCase(s));

The first work with warnings:
unit1.pas(36,33) 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"

But that is not the question. I'm asking about the helpers.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: String helper
« Reply #8 on: October 21, 2016, 07:15:32 pm »
AFAIK there can only be one .ToUpper helper for ansistrings.
Unfortunately it seems to use SysUtils.UpperCase.

The fpc devels must have had their reasons for that.
(At least it does not need the widestring manager, which pulls in libc on *nix.)

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: String helper
« Reply #9 on: October 21, 2016, 07:29:22 pm »
The fpc devels must have had their reasons for that.

backwards compatibility ?

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: String helper
« Reply #10 on: October 21, 2016, 07:30:29 pm »
So it is ansi only.

Technically, it is ASCII only.  If it were ANSI then it would be working.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: String helper
« Reply #11 on: October 21, 2016, 07:45:59 pm »
So it is ansi only.

Technically, it is ASCII only.  If it were ANSI then it would be working.

You're right.

The fpc devels must have had their reasons for that.

backwards compatibility ?

Is something like this implemented in Delphi, I can't say, I don't have delphi.

Edit:
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.TStringHelper.ToUpper
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.TStringHelper.ToUpperInvariant

According to that website they are using unicode utf-16, we use utf-8, that's not the point in any case. The point is that they are using unicode and we ASCIII :'(
« Last Edit: October 21, 2016, 08:03:45 pm by lainz »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: String helper
« Reply #12 on: October 22, 2016, 12:01:37 am »
The fpc devels must have had their reasons for that.

backwards compatibility ?

Backwards compatibility with what exactly?
.ToUpper is a relatively new invention, why then support just ASCII?

Bart

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: String helper
« Reply #13 on: October 22, 2016, 11:36:42 am »
Yes, the String helper should definitely use Ansi...() functions like AnsiUpperCase.
Otherwise it will not be very useful. People have to make their own String helpers and the library code is left unused.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: String helper
« Reply #14 on: October 24, 2016, 07:46:08 pm »
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.TStringHelper.ToUpper
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.TStringHelper.ToUpperInvariant

TStringHelper.ToUpper() uses the ASCII-only version of SysUtils.UpperCase() (I do not know why, as there is a locale-aware overload of UpperCase() available that uses AnsiUpperCase()).

TStringHelper.ToUpperInvariant() uses TCharacter.ToUpper(), which internally uses LCMapString() on Windows systems and towupper_l() on Posix systems.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018