Sign in Join

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

Mailing lists / fpc-pascal / 2008-03

Empty string

12 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Damien Gerard

In any programs, a lot of tests on empty strings are made. The following made the work :
if Length(s) 0 then ...
or
if s '' then ...

In a lot of cases, the real length is no needed, just to know if the string is empty or not.
I am mainly using UTF8String so strings with leading zero. Just with the first char we can know if the string is empty or not.

Is there a function of this kind that I don't know already exist ? Would it be possible to implement such a function (may be a stupid idea for an unknown reason) ?
Can this function be directly done in FPC ?

Regards.

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
ik
to Damien Gerard

Doesn't FillChar/Word/Byte works for you ?

Ido

Damien Gerard wrote 35 lines
In any programs, a lot of tests on empty strings are made. The following made the work : if Length(s) 0 then ... or if s '' then ... In a lot of cases, the real length is no needed, just to know if the string is empty or not. I am mainly using UTF8String so strings with leading zero. Just with the first char we can know if the string is empty or not. Is there a function of this kind that I don't know already exist ? Would it be possible to implement such a function (may be a stupid idea for an unknown reason) ? Can this function be directly done in FPC ? Regards. -- Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Damien Gerard
to Lucas Vasconcelos
Lucas Vasconcelos wrote 5 lines
You can try access a UTF8String lika a array. So, you can replace your test by if mystringvar[0] '' then ...

I thought string should be accessed from 1 not 0 for compatibility. And it is what I am doing.

for i := 1 to length(s) do ...

so I am not sure that using s[0] is safe

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Gene Buckle
to Damien Gerard
Show quoted text 7 lines
I thought string should be accessed from 1 not 0 for compatibility. And it is what I am doing. for i := 1 to length(s) do ... so I am not sure that using s[0] is safe

If memory serves, the [0] element was originally used to hold a byte that was equivalent to the string length. This was in the Turbo Pascal days though. I don't recall if that behavior was carried over into Delphi or the Delphi mode of FPC.

g.

-- Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.

Flávio Etrusco
to Gene Buckle

To the OP: are you using the AnsiString as a buffer (that contains "several" strings)? Otherwise, using "MyString '' " would do because #0 is only valid as terminator in UTF-8 too.

BTW, IIRC Delphi would generate faster code for the above construction instead of "Length(MyString) 0" because of locking.

Show quoted text 7 lines
> I thought string should be accessed from 1 not 0 for compatibility. > And it is what I am doing. > > for i := 1 to length(s) do ... > > so I am not sure that using s[0] is safe >

s[0] won't compile. Not in Delphi, at least.

Show quoted text 5 lines
If memory serves, the [0] element was originally used to hold a byte that was equivalent to the string length. This was in the Turbo Pascal days though. I don't recall if that behavior was carried over into Delphi or the Delphi mode of FPC.

That's correct, but for ShortStrings only.

Best regards,
Flávio

Damien Gerard
to ik: Doesn't FillChar/Word/Byte works for you ? Ido http://ik.homelinux.org/
ik wrote
Doesn't FillChar/Word/Byte works for you ?

I don't think any of them made my purpose.

Show quoted text 47 lines
Ido On Mon, Mar 3, 2008 at 6:16 PM, Damien Gerard wrote: > > In any programs, a lot of tests on empty strings are made. > The following made the work : > if Length(s) 0 then ... > or > if s '' then ... > > In a lot of cases, the real length is no needed, just to know if the > string is empty or not. > I am mainly using UTF8String so strings with leading zero. > Just with the first char we can know if the string is empty or not. > > Is there a function of this kind that I don't know already exist ? > Would it be possible to implement such a function (may be a stupid > idea for an unknown reason) ? > Can this function be directly done in FPC ? > > Regards. > > > -- > Damien Gerard > > > "Intelligence is 10 million rules." > -- Douglas Lenat > > > > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > -- http://ik.homelinux.org/ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Damien Gerard
to Jonas Maebe
Jonas Maebe wrote 14 lines
On 03 Mar 2008, at 17:16, Damien Gerard wrote: > In any programs, a lot of tests on empty strings are made. > The following made the work : > if Length(s) 0 then ... > or > if s '' then ... > > In a lot of cases, the real length is no needed, just to know if > the string is empty or not. That is what "if s '' then" checks for. This is already generated as optimally as possible.

In a previous thread, I have ask the difference between (Length(s) =
0) and (s = '')

Copy paste a answer :

Show quoted text 6 lines
> > if s = '' then ... > of this one > if Length(s) 0 then ... Both generate the same code.

Consequently, it can not be the same code. Otherwise I understood nothing :)

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat
Jonas Maebe
to Damien Gerard
Damien Gerard wrote 31 lines
Le Mar 7, 2008 à 2:54 PM, Jonas Maebe a écrit : > > On 03 Mar 2008, at 17:16, Damien Gerard wrote: > >> In any programs, a lot of tests on empty strings are made. >> The following made the work : >> if Length(s) 0 then ... >> or >> if s '' then ... >> >> In a lot of cases, the real length is no needed, just to know if >> the string is empty or not. > > That is what "if s '' then" checks for. This is already > generated as optimally as possible. In a previous thread, I have ask the difference between (Length(s) = 0) and (s = '') Copy paste a answer : >> >> if s = '' then ... >> of this one >> if Length(s) 0 then ... > > Both generate the same code. Consequently, it can not be the same code. Otherwise I understood nothing :)

I don't understand your question. Above, you say that these statements work:

>> if Length(s) 0 then ... >> or >> if s '' then ...

(or does "made the work" mean something else?). If that indeed works, then everything is fine, because
a) both indeed generate the same code
b) both check whether the string is empty or not (without actually looking at the length)

Jonas

Damien Gerard
to John Coppens
John Coppens wrote 13 lines
On Fri, 7 Mar 2008 15:09:36 +0100 Damien Gerard wrote: > Consequently, it can not be the same code. Otherwise I understood > nothing :) '' is an empty string, _and_ its length is zero (of course). So the compiler is so intelligent as to replace length(s) = 0, or s = '' by the same code. Normally s = '' would be a very slow way to check for an empty string, so the compiler optimizes this automatically.

Thanks !

Sorry for my question not so clear :)

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat