#1 2023-04-27 21:17:42

tbo
Member
Registered: 2015-04-20
Posts: 337

Record helper for RawUtf8

Could a record helper for RawUtf8 be useful for current Delphi versions?

type
  TRawUtf8Helper = record helper for RawUtf8
  public
    procedure From(const pmcValue: String); inline;
    function ToString: String; inline;
    function ToInteger: Integer; overload; inline;
    function ToInteger(pmDefault: Integer): Integer; overload; inline;
    function ToInteger(pmMin, pmMax: Integer; pmDefault: Integer = 0): Integer; overload; inline;

procedure TRawUtf8Helper.From(const pmcValue: String);
begin
  Self := StringToUtf8(pmcValue);
end;

function TRawUtf8Helper.ToInteger: Integer;
begin
  Result := Utf8ToInteger(Self);
end;

function TRawUtf8Helper.ToInteger(pmDefault: Integer): Integer;
begin
  Result := Utf8ToInteger(Self, pmDefault);
end;

function TRawUtf8Helper.ToInteger(pmMin, pmMax, pmDefault: Integer): Integer;
begin
  Result := Utf8ToInteger(Self, pmMin, pmMax, pmDefault);
end;

function TRawUtf8Helper.ToString: String;
begin
  Result := Utf8ToString(Self);
end;

Then you could write like this:

var
  s: String;
  u: RawUtf8;
begin
  s := '12';
  u.From(s);
  ShowMessage(u.ToString);
  var i: Integer := u.ToInteger(8, 12);

With best regards
Thomas

Offline

#2 2023-04-28 10:03:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,316
Website

Re: Record helper for RawUtf8

It may help some people, but last time I checked, on Delphi record helpers don't propagate if you create your own sub-type:

type
  TFirstName = type(RawUtf8);

Refine the types is a practice I recommend... and it voids the effect of such helpers...

This is the main reason why I never used record helpers - and also that you can only define a single record helper per type on Delphi.

This is an old topic:
https://blog.synopse.info/?post/2015/05 … %2C-do-you
and
https://synopse.info/forum/viewtopic.php?id=2556

Offline

#3 2023-04-28 13:50:39

tbo
Member
Registered: 2015-04-20
Posts: 337

Re: Record helper for RawUtf8

ab wrote:

It may help some people, ...

That was the reason I asked. I prefer to answer questions in a forum about programming problems with concrete source code. Most with reference to mORMot. The new functions, which are unknown, often discourage, although the solution, thanks to mORMot, is better. If it was more similar to actual Delphi source code, it could be easier to teach. Rarely you get such a concrete feedback how good the result has become by using mORMot.

With best regards
Thomas

Offline

#4 2023-04-28 17:53:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,316
Website

Re: Record helper for RawUtf8

Thanks for the feedback link.
Impressive! big_smile

And from our work on the last two days on PostgreSQL asynchronous queries via callbacks, I am confident mORMot could be in the Top #10 of the TFB frameworks.
https://synopse.info/forum/viewtopic.ph … 643#p39643

You are right: the main trick is to let people know about how powerful the FPC and mORMot combination could be, and not too difficult to use, once you have understood how it works.
This is why your samples and articles are so meaningful. Thanks again.

Offline

#5 2023-04-30 10:17:29

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 398

Re: Record helper for RawUtf8

I am using the following for my work:
https://gist.github.com/dkounal/137630c … bfe9efe32e

Offline

#6 2023-04-30 16:34:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,316
Website

Re: Record helper for RawUtf8

@dcount
Thanks for sharing. Even if this code expects string = UnicodeString which is not cross-compiler - so it is Delphi only. wink
Some of your helpers are pretty weird, like the one using a temporary string then copy() on it with passed parameters. I don't see how it may work in practice because the indexes expects UTF-16 but you only have a UTF-8 string at hand.
Also the use of PUtf8Char() over a RawUtf8 is pretty inefficient, because it will make a temporary string allocation.

Offline

#7 2023-04-30 16:38:57

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 398

Re: Record helper for RawUtf8

I was really afraid to present it..... but it help to learn!
smile
Thanks @ab

Offline

#8 2023-04-30 16:53:10

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 398

Re: Record helper for RawUtf8

ab wrote:

@dcount
Thanks for sharing. Even if this code expects string = UnicodeString which is not cross-compiler - so it is Delphi only. wink

I am missing something here..... I have the same code being compiled in Delphi and FPC. is something really bad?

ab wrote:

Some of your helpers are pretty weird, like the one using a temporary string then copy() on it with passed parameters. I don't see how it may work in practice because the indexes expects UTF-16 but you only have a UTF-8 string at hand.

I am using greek text that with utf8 you can not know where to cut it. So, for cases that I know the place to be cut in delphi string but I have a rawutf8 I using this

ab wrote:

Also the use of PUtf8Char() over a RawUtf8 is pretty inefficient, because it will make a temporary string allocation.

I changed the PUtf8Char to pointer().

Offline

Board footer

Powered by FluxBB