You are not logged in.
Pages: 1
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
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
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
Thanks for the feedback link.
Impressive!
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
I am using the following for my work:
https://gist.github.com/dkounal/137630c … bfe9efe32e
Offline
@dcount
Thanks for sharing. Even if this code expects string = UnicodeString which is not cross-compiler - so it is Delphi only.
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
I was really afraid to present it..... but it help to learn!
Thanks @ab
Offline
@dcount
Thanks for sharing. Even if this code expects string = UnicodeString which is not cross-compiler - so it is Delphi only.
I am missing something here..... I have the same code being compiled in Delphi and FPC. is something really bad?
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
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
Pages: 1