Board index » delphi » Copy(String,5,255) ??

Copy(String,5,255) ??

I often put  COPY(STRING,5,255) to get string starting from 5th to the
end with a 255.
Now with the long string, what should I put??
Should I just drop in a number (any number) that is big enough for my
purpose???

I don't like the idea of  length()-whatever
because it requires another calculation which just degrade the
performance a bit.
A 65535 seems good, but what is generally recommended??

 

Re:Copy(String,5,255) ??


Quote
Anders LEE wrote:
> Should I just drop in a number (any number) that is big enough for my
> purpose???

You could use MAXINT.

Quote
> A 65535 seems good, but what is generally recommended??

As strings can contain up to <wild guess>2 GB</wild guess> 65535 is not
the best choice.

Regards,
Udo
--
Please reply to newsgroup. No PMs unless requested.

Re:Copy(String,5,255) ??


In article <g0olesgg7v23ih48h42efvvop90jc9h...@4ax.com>, Anders LEE

Quote
<and...@aelhk.com> writes:
>I often put  COPY(STRING,5,255) to get string starting from 5th to the
>end with a 255.
>Now with the long string, what should I put??
>Should I just drop in a number (any number) that is big enough for my
>purpose???

>I don't like the idea of  length()-whatever
>because it requires another calculation which just degrade the
>performance a bit.

AFAIK Length(MyString) would be a direct lookup to a memory location of
MyString[0] - 4 for long strings and MyString[0] for short strings. Hence it
involves negligibly more time than a compile time variable, and does not
involve a calculation.

In any case because Copy() copies only the length of the string it must at some
time compare your specified number to the actual string length remaining. If
the compiler were really clever it would not do the compare of specified
against actual remaining, but just copy actual remaining. In such a scenario it
would be quicker to specify Length() than a very large number (like MAXDWORD).

Any _really_ knowledgable people out there <g>

I always think that Length(MyString) is more obviously "all the rest of the
string" than a big number.

Alan Lloyd
alangll...@aol.com

Re:Copy(String,5,255) ??


The long string is simply a pointer to a null terminating string. The
pointer indicates the first starting character. I would simply move
the pointer to the fifth character. This will ensure you read the
string you are after whatever its length.

Regards
John Collins

Quote
> I often put  COPY(STRING,5,255) to get string starting from 5th to
the
> end with a 255.
> Now with the long string, what should I put??
> Should I just drop in a number (any number) that is big enough for
my
> purpose???

> I don't like the idea of  length()-whatever
> because it requires another calculation which just degrade the
> performance a bit.
> A 65535 seems good, but what is generally recommended??

Re:Copy(String,5,255) ??


NO !

Starting from Delphi 2.0, we have had long strings.

Although you can do this:

procedure C_Style_Proc(P:PChar);
begin
    // Do something with the value of P, but at least
    // DO NOT make it longer !!!
end;

...

procedure MyProc;
begin
   S := 'hello'#0'there'; // Yes, pascal long strings CAN include a
   // null byte ! But the C -style proc will only see the "hello"
   // part, because "C" .style strings terminate with a null.
    C_Style_Proc(PChar(S));
   S[6] := ','
    C_Style_Proc(PChar(S)); // NOW it will see "hello,there"
end;

pascal long strings are not just null terminated strings,
although delphi 2.0 and later will guarantee that the first unused
byte after the string will always be #0 (= a null byte)

If you do, what   "Dr J D Collins" suggests below, you will very
likely get access violations, corrupt other data etc...

Reason: the long string variable is actually  a pointer to the 1st
byte of the string, but the previous 12 bytes contain the allocated
length, current length and the usage count (NOT necessary in this
order, so assembler programmers, read the Delphi source, don't just
take my, or anyone else's word, for that matter)

So, "adjusting" a string like this:
inc(integer(S),4); // THIS is very dangerous and will corrupt memory
etc...

So, why not just:

A := copy(B,5,Maxint);

or, if you would use
A := copy(A,5,Maxint);

then it is even more effective to:

system.delete(A,1,4);

// the "system." -prefix will make sure the system unit's DELETE is
what is called, even if you use this inside a form's (etc..) method,
since many classes also have a method called system.

On Wed, 5 Apr 2000 21:54:24 +1000, "Dr J D Collins"

Quote
<jcoll...@acay.com.au> wrote:
>The long string is simply a pointer to a null terminating string. The
>pointer indicates the first starting character. I would simply move
>the pointer to the fifth character. This will ensure you read the
>string you are after whatever its length.

>Regards
>John Collins

>> I often put  COPY(STRING,5,255) to get string starting from 5th to
>the
>> end with a 255.
>> Now with the long string, what should I put??
>> Should I just drop in a number (any number) that is big enough for
>my
>> purpose???

>> I don't like the idea of  length()-whatever
>> because it requires another calculation which just degrade the
>> performance a bit.
>> A 65535 seems good, but what is generally recommended??

Re:Copy(String,5,255) ??


Sorry I meant huge string. This is the type of approach I use for
these strings.

Regards
John Collins

Quote
> NO !

> Starting from Delphi 2.0, we have had long strings.

> Although you can do this:

> procedure C_Style_Proc(P:PChar);
> begin
>     // Do something with the value of P, but at least
>     // DO NOT make it longer !!!
> end;

> ...

> procedure MyProc;
> begin
>    S := 'hello'#0'there'; // Yes, pascal long strings CAN include a
>    // null byte ! But the C -style proc will only see the "hello"
>    // part, because "C" .style strings terminate with a null.
>     C_Style_Proc(PChar(S));
>    S[6] := ','
>     C_Style_Proc(PChar(S)); // NOW it will see "hello,there"
> end;

> pascal long strings are not just null terminated strings,
> although delphi 2.0 and later will guarantee that the first unused
> byte after the string will always be #0 (= a null byte)

> If you do, what   "Dr J D Collins" suggests below, you will very
> likely get access violations, corrupt other data etc...

> Reason: the long string variable is actually  a pointer to the 1st
> byte of the string, but the previous 12 bytes contain the allocated
> length, current length and the usage count (NOT necessary in this
> order, so assembler programmers, read the Delphi source, don't just
> take my, or anyone else's word, for that matter)

> So, "adjusting" a string like this:
> inc(integer(S),4); // THIS is very dangerous and will corrupt memory
> etc...

> So, why not just:

> A := copy(B,5,Maxint);

> or, if you would use
> A := copy(A,5,Maxint);

> then it is even more effective to:

> system.delete(A,1,4);

> // the "system." -prefix will make sure the system unit's DELETE is
> what is called, even if you use this inside a form's (etc..) method,
> since many classes also have a method called system.

> On Wed, 5 Apr 2000 21:54:24 +1000, "Dr J D Collins"
> <jcoll...@acay.com.au> wrote:

> >The long string is simply a pointer to a null terminating string.
The
> >pointer indicates the first starting character. I would simply move
> >the pointer to the fifth character. This will ensure you read the
> >string you are after whatever its length.

> >Regards
> >John Collins

> >> I often put  COPY(STRING,5,255) to get string starting from 5th
to
> >the
> >> end with a 255.
> >> Now with the long string, what should I put??
> >> Should I just drop in a number (any number) that is big enough
for
> >my
> >> purpose???

> >> I don't like the idea of  length()-whatever
> >> because it requires another calculation which just degrade the
> >> performance a bit.
> >> A 65535 seems good, but what is generally recommended??

Other Threads