Empty string

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Empty string

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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

ik-6
Doesn't FillChar/Word/Byte works for you ?

Ido

On Mon, Mar 3, 2008 at 6:16 PM, Damien Gerard <[hidden email]> 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
>  [hidden email]
>
>  "Intelligence is 10 million rules."
>     -- Douglas Lenat
>
>
>
>
>
>  _______________________________________________
>  fpc-pascal maillist  -  [hidden email]
>  http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



--
http://ik.homelinux.org/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Lucas Vasconcelos
In reply to this post by Damien Gerard
You can try access a UTF8String lika a array.

So, you can replace your test by

if mystringvar[0] <> '' then ...


--
Lucas Vasconcelos
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Damien Gerard

Le Mar 6, 2008 à 5:26 PM, Lucas Vasconcelos a écrit :

> 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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

geneb
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

etrusco
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.


> > 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.


>  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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Damien Gerard
In reply to this post by ik-6

Le Mar 3, 2008 à 5:22 PM, ik a écrit :

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

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

> Ido
>
> On Mon, Mar 3, 2008 at 6:16 PM, Damien Gerard <[hidden email]>  
> 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
>> [hidden email]
>>
>> "Intelligence is 10 million rules."
>>    -- Douglas Lenat
>>
>>
>>
>>
>>
>> _______________________________________________
>> fpc-pascal maillist  -  [hidden email]
>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>>
>
>
>
> --
> http://ik.homelinux.org/
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Damien Gerard
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Jonas Maebe-2
In reply to this post by Damien Gerard

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.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Damien Gerard

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 :)



--
Damien Gerard
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Jonas Maebe-2

On 07 Mar 2008, at 15:09, Damien Gerard wrote:

> 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_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

John Coppens
In reply to this post by Damien Gerard
On Fri, 7 Mar 2008 15:09:36 +0100
Damien Gerard <[hidden email]> 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.

John
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Empty string

Damien Gerard

Le Mar 7, 2008 à 3:35 PM, John Coppens a écrit :

> On Fri, 7 Mar 2008 15:09:36 +0100
> Damien Gerard <[hidden email]> 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
[hidden email]

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





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal