> I would say: GPC *does* optimize this. Just type `gpc -O -S foo.pas'
> and look at `foo.s' ...
Hmmm... well, I could not see the effect. I wrote the following test program:
program x;
var s:string[10000];
begin
s:='';
s:=s+'a'
end.
I compiled it with "-O3 -S". The resulting code contained two calls to
_memcpy. Another test program that appended characters in a loop obviously
ran in O(n^2). Am I missing something?
--
Frank Heckenbach, Erlangen, Germany
heckenb(a)mi.uni-erlangen.de
Turbo Pascal: http://www.mi.uni-erlangen.de/~heckenb/programs.htm
Internet links: http://www.mi.uni-erlangen.de/~heckenb/links.htm
According to skye:
>
> Is Exit not included in GPC? i.e.
> [...]
> I tried using return; and that works.
GPC 2.0 only has `return'; newer alpha versions also have `exit'.
> I posted this question to the comp.lang.pascal NG and had someone from
> Borland tell me that Exit should do the trick (I didn't say I wasn't using
> TP tho).
:-)
> Is exit a Borland thing?
It's an UCSD extension. Borland Pascal is based a lot on UCSD Pascal
(Units, Strings, Exit, ...).
Greetings,
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski(a)uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
maintainer GNU Pascal [970420] - http://home.pages.de/~gnu-pascal/ [970125]
According to Frank Heckenbach:
> >
> > No. They are exceptions in yet another way because they have the
> > implicit read-only "length" field.
>
> That's what I meant. If length was made accessible in a normal way, it
> would not be an exception.
Being an array schema, the presence of the implicit "length" field would
make the string an exception. Being a record schema, the implicit
dereference of the "string" field when indexing the string makes it
an exception. No way out. (However see my other mail.)
Greetings,
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski(a)uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
maintainer GNU Pascal [970420] - http://home.pages.de/~gnu-pascal/ [970125]
According to The African Chief:
> According to Frank Heckenbach:
> >255 chars max length are sometimes not enough.
> But you can use normal GPC strings for that.
Just my point: For applications where you need looooong strings, GPC's
<=2GB strings with 8 bytes of overhead are perfect. In those cases
where you must save space, 255 characters are fine - and they are
compatible to UCSD and Borland Pascal.
Should it be `ShortString' or `ShortStr'? In analogy to `LongInt'
instead of `LongInteger' I would vote for `ShortStr'; OTOH, Prospero's
Extended Pascal compiler (PEP) has `ShortString' for this ...
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski(a)uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
maintainer GNU Pascal [970420] - http://home.pages.de/~gnu-pascal/ [970125]
On Tue, 29 Apr 1997 12:53:53 +0200 Frank Heckenbach <heckenb(a)mi.uni-erlangen.de>
wrote:
[...]
>> It would not be too difficult to make "MyStr.length" and "MyStr.String"
>> accessible "record fields" of strings. Would it be desireable?
>
>Yes! (At least length; the String part is accessible anyway with s[n].)
>
>> I am
>> not sure how this fits into the standard ...
>
>I'm not, either. If it conflicts, it should be disabled with
>"--extended-pascal", I suppose.
It should be disabled, with "-borlland-pascal" as well.
>> Another thing *is* planned: A new data type `ShortString' (or
>> `ShortStr'?) which will be the UCSD/Borland version of Strings. In many
>> contexts, they are better than Extended Pascal string schemata because
>> they have only 1 byte overhead instead of 8. (But this will be a lot
>> of work. Anybody interested to help here???)
>
>What about another type with a 2 byte length field (2 instead of 8 bytes
>overhead)?
Will this be BP compatible?
>255 chars max length are sometimes not enough.
But you can use normal GPC strings for that.
>BTW: I heard
>Delphi has some kind of long strings. Anyone knows what they are?
Only available with 32-bit versions of Delphi. Sort of dynamically
allocated strings. Delphi does all the background work itself
(allocating, freeing, sizing, resizing, etc). I think they can be up
to 2GB in size. Need to be used with care, because all sorts of
things can go wrong if you treat them like normal strings.
"Almost" compatible with pChars. You can do something
like "PChar (MyLongString)" to pass a long string as a parameter
to a function that requires a pChar parameter. When used as
parameters or function results in a DLL, all programs that use
that DLL must use Delphi's memory manager DLL as well.
Personally, I wouldn't touch them with a barge pole. I would
rather use a pChar, if that is what I want, or a normal string, if
that is what I want - and I prefer to allocate and free memory
for data structures myself.
Best regards, The Chief
Dr Abimbola A. Olowofoyeku (The African Chief, and the Great Elephant)
Author of: Chief's Installer Pro v3.12 for Win16 and Win32.
Homepage: http://ourworld.compuserve.com/homepages/African_Chief/
E-mail: laa12(a)cc.keele.ac.uk
> I would say: GPC *does* optimize this. Just type `gpc -O -S foo.pas'
> and look at `foo.s' ...
Fine! I'll take a look at it.
> It would not be too difficult to make "MyStr.length" and "MyStr.String"
> accessible "record fields" of strings. Would it be desireable?
Yes! (At least length; the String part is accessible anyway with s[n].)
> I am
> not sure how this fits into the standard ...
I'm not, either. If it conflicts, it should be disabled with
"--extended-pascal", I suppose.
> Another thing *is* planned: A new data type `ShortString' (or
> `ShortStr'?) which will be the UCSD/Borland version of Strings. In many
> contexts, they are better than Extended Pascal string schemata because
> they have only 1 byte overhead instead of 8. (But this will be a lot
> of work. Anybody interested to help here???)
What about another type with a 2 byte length field (2 instead of 8 bytes
overhead)? 255 chars max length are sometimes not enough. BTW: I heard
Delphi has some kind of long strings. Anyone knows what they are?
> > (Are you planning
> > to make strings regular schema types then -- regular except for the extended
> > syntax that allows s[n]?)
>
> No. They are exceptions in yet another way because they have the
> implicit read-only "length" field.
That's what I meant. If length was made accessible in a normal way, it
would not be an exception.
--
Frank Heckenbach, Erlangen, Germany
heckenb[No_Spam! Remove this to reply.](a)mi.uni-erlangen.de
Turbo Pascal: http://www.mi.uni-erlangen.de/~heckenb/programs.htm
Internet links: http://www.mi.uni-erlangen.de/~heckenb/links.htm
According to Frank Heckenbach:
>
> Well, the problem is, I said "efficiently".
> I don't assume, gpc would optimize statements like
> s:=s+'a'
> where s is a (possibly quite long) string, so I'd have O(n) where actually
> O(1) is possible.
I would say: GPC *does* optimize this. Just type `gpc -O -S foo.pas'
and look at `foo.s' ...
> E.g. the examples from the documentation about converting
> CStrings to Pascal strings append char by char in a loop -- O(n^2) instead
> of O(n). I don't think that should be left as it is.
You mean the routines in the FAQ? I think they are O(n) because
"S:= S + Src^" is inlined and optimized. But of course, feel free
to improve those routines and send the result to Jan-Jaap ... ;-)
> I'd have to do it like this if there's no other way...
> But shouldn't it be possible to access the fields of the "string record" in
> a direct way? Perhaps in the course of the schema project?
It would not be too difficult to make "MyStr.length" and "MyStr.String"
accessible "record fields" of strings. Would it be desireable? I am
not sure how this fits into the standard ...
Another thing *is* planned: A new data type `ShortString' (or
`ShortStr'?) which will be the UCSD/Borland version of Strings. In many
contexts, they are better than Extended Pascal string schemata because
they have only 1 byte overhead instead of 8. (But this will be a lot
of work. Anybody interested to help here???)
> (Are you planning
> to make strings regular schema types then -- regular except for the extended
> syntax that allows s[n]?)
No. They are exceptions in yet another way because they have the
implicit read-only "length" field.
Greetings,
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski(a)uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
maintainer GNU Pascal [970420] - http://home.pages.de/~gnu-pascal/ [970125]
> > Is there a way to change a string's length directly?
>
> AFAIK, there isn't. Use the `SubStr' function (like BP's `copy' function)
> to shorten a string, and the `+' operator to make it longer.
Well, the problem is, I said "efficiently".
I don't assume, gpc would optimize statements like
s:=s+'a'
where s is a (possibly quite long) string, so I'd have O(n) where actually
O(1) is possible. E.g. the examples from the documentation about converting
CStrings to Pascal strings append char by char in a loop -- O(n^2) instead
of O(n). I don't think that should be left as it is.
> And of course you can take a pointer to the string, cast it to a pointer
> to a record the fields of which you can manipulate like you wish ...
I'd have to do it like this if there's no other way...
But shouldn't it be possible to access the fields of the "string record" in
a direct way? Perhaps in the course of the schema project? (Are you planning
to make strings regular schema types then -- regular except for the extended
syntax that allows s[n]?)
--
Frank Heckenbach, Erlangen, Germany
heckenb[No_Spam! Remove this to reply.](a)mi.uni-erlangen.de
Turbo Pascal: http://www.mi.uni-erlangen.de/~heckenb/programs.htm
Internet links: http://www.mi.uni-erlangen.de/~heckenb/links.htm
Peter Gerwinski wrote:
> I encountered some problems with Procedures accepting `CString'
> parameters. When passing a string constant to such a parameter, GPC
> does *not* automatically add a "chr ( 0 )" terminator.
>
> Should it do? And if so, in which cases? We always can explicitly write
> "foo ( 'bar'#0 );" instead of just "foo ( 'bar' );", so I tend to let
> things like they are ...
I think it should automatically add a #0 terminator. After all, a "CString"
without terminator is somewhat undefined.
In which cases? I think always when a string constant is given where a
"CString" value is expected, whether for a parameter, or a variable
initializer. E.g. the following should work as expected:
var numbers:array[1..3] of cstring value (1:'one';2:'two';3:'three');
or in BP syntax:
const numbers:array[1..3] of pchar=('one','two','three');
Many of my programs rely on this.
--
Frank Heckenbach, Erlangen, Germany
heckenb[No_Spam! Remove this to reply.](a)mi.uni-erlangen.de
Turbo Pascal: http://www.mi.uni-erlangen.de/~heckenb/programs.htm
Internet links: http://www.mi.uni-erlangen.de/~heckenb/links.htm
According to Christian Wendt:
>
> I wondered how to implement the ability to change colours and write text
> to Standard output - but I think it's nearly impossible to do this within
> a Unit - the C - Textcolor doesn't work with the fprintf function that is
> used in the RTS...
Nevertheless I did this. :-) Please have a look at the BPcompat package,
available in the `contrib' subdirectory of the GPC distribution.
> I wondered wether it would be legal (in sense of
> portability and compability...) to change the RTS : Textcolor works if you
> check wether the file passed to writeln is Stdout - when it is you use
> cprintf (or putch instead of fwrite).
You are right that this is a problem we must think about. In this case
I think it increases portability and compatibility to do the changes
because otherwise it is impossible to get colors under DOS whereas you
can have them with Linux or any other system which provides colors as
terminal escape sequences, e.g. according to ANSI.
> The question is - would it be legal to do such - you could put in a
> variable (smth like "GPC_ConsoleOutput") to switch in on/off ?
In the BPcompat package or in newer Alpha releases of GPC, this variable
exists and is called `p_directvideo', available as `DirectVideo' if you
do `uses CRT'. If it is `true', `writeln' is performed through `cprintf',
otherwise through `printf'.
Greetings,
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski(a)uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
maintainer GNU Pascal [970420] - http://home.pages.de/~gnu-pascal/ [970125]