Hi,
When I use "copy" on a PChar (e.g. copy(p,1,1)), it is very slow, because the compiler converts the PChar to a string.
Is there already a function for PChar?
Can an overloaded version of copy be added?
Mattias

Mailing lists / fpc-devel / 2012-02
8 messages · 4 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
When I use "copy" on a PChar (e.g. copy(p,1,1)), it is very slow, because the compiler converts the PChar to a string.
Is there already a function for PChar?
Can an overloaded version of copy be added?
Mattias
19.02.2012 18:38, Mattias Gaertner пишет:
Not sure why it is allowed in the first place. If out of Delphi compatibility, maybe we should leave it enabled for {$mode delphi} only (should be possible because copy is partially handled by compiler).
Is there already a function for PChar? Can an overloaded version of copy be added?
It is a question how to interpret indices then (PChar is zero-based while strings are 1-based). And actually it will mean 4 overloads: PChar, PWideChar, array of char, array of widechar.
Regards,
Sergei
When I use "copy" on a PChar (e.g. copy(p,1,1)), it is very slow, because the compiler…
In our previous episode, Mattias Gaertner said:
When I use "copy" on a PChar (e.g. copy(p,1,1)), it is very slow, because the compiler converts the PChar to a string. Is there already a function for PChar?
http://www.freepascal.org/docs-html/rtl/strings/strlcopy.html
19.02.2012 18:38, Mattias Gaertner пишет: Not sure why it is allowed in the first…
It's a useful feature.
Indeed. That would be confusing.
Is there already a function
function Copy(StartPos: PChar; Count: PtrUInt; StopAt0: boolean =
true): string;Mattias
In our previous episode, Mattias Gaertner said:…
Thanks. But apparently I was not exact enough.
"Copy" has the nice feature, that the bounds are checked. For PChar it does that too, but it always runs through the whole PChar to #0.
And I need an Ansistring as result.
Mattias
On Sun, 19 Feb 2012 20:01:11 +0400 It's a useful feature. Indeed. That would be confusing.
19.02.2012 20:34, Mattias Gaertner пишет:
I don't know about an already existing such function (well, actually SetString used to work that way in the past, but no longer), but here's one (assuming StartPosnil):
var
len: sizeint;
begin
if StopAt0 then
begin
len:=IndexByte(StartPos^,Count,0);
if len=-1 then
len:=Count;
end
else
len:=Count;
SetString(result,StartPos,len);
end;Regards,
Sergei
On Sun, 19 Feb 2012 17:31:48 +0100 (CET) Thanks. But apparently I was not exact enough.…
In our previous episode, Mattias Gaertner said:
http://www.freepascal.org/docs-html/rtl/system/setstring.html
When I use "copy" on a PChar (e.g. copy(p,1,1)), it is very slow, because the compiler…
How should it work ?
A Phar as a left value sets the pointer to some (hopefully sensible) memory location. for a string resulting from "copy" such memory would need to be allocated.
IMHO implicitly doing a malloc() and propagate the need to free the memory allocation to the user is very ambiguous.
-Michael