Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2012-02

copy for PChar

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

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

Reply by email

Sergei Gorelkin
to Mattias Gaertner

19.02.2012 18:38, Mattias Gaertner пишет:

Show quoted text 5 lines
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.

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

Reply by email

Mattias Gaertner
to Sergei Gorelkin: 19.02.2012 18:38, Mattias Gaertner пишет: Not sure why it is allowed in the first…
Show quoted text 7 lines
19.02.2012 18:38, Mattias Gaertner пишет: > 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. > Not sure why it is allowed in the first place.

It's a useful feature.

Show quoted text 9 lines
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.

Indeed. That would be confusing.

Is there already a function

function Copy(StartPos: PChar; Count: PtrUInt; StopAt0: boolean =
true): string;

Mattias

Reply by email

Mattias Gaertner
to Marco van de Voort: In our previous episode, Mattias Gaertner said:…
Show quoted text 7 lines
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

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

Reply by email

Sergei Gorelkin
to Mattias Gaertner: 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 пишет:

Show quoted text 6 lines
Is there already a function function Copy(StartPos: PChar; Count: PtrUInt; StopAt0: boolean = true): string;

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

Reply by email

Marco van de Voort
to Mattias Gaertner: 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:

Show quoted text 9 lines
> > http://www.freepascal.org/docs-html/rtl/strings/strlcopy.html 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.

http://www.freepascal.org/docs-html/rtl/system/setstring.html

Reply by email

Michael Schnell
to Mattias Gaertner: When I use "copy" on a PChar (e.g. copy(p,1,1)), it is very slow, because the compiler…
Mattias Gaertner wrote 6 lines
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?

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

Reply by email