Checking if a string is null
Regan Heath
regan at netmail.co.nz
Wed Jul 25 10:27:13 PDT 2007
>> The only thing that should compare equal to null is null. Likewise an
>> empty array should only compare equal to another empty array.
> >
> > My reasoning for this is consistency, see at end.
>
> Since null arrays have length 0, they *are* empty arrays :P.
I can't tell in which way you're joking so I'm just going to come out
with...
The length of something be it an array, a car, a <insert thing> is
totally independant of whether it exists (though a non-existant item
cannot have a length).
It either exists or it does not. If it exists, it has a length which
may or may not be zero.
Something which exists cannot be equal to something which doesn't.
Period.
>> Aside: If the location and length are identical you can short-circuit
>> the compare, returning true and ignoring the content, this could save
>> a bit of time on comparisons of large arrays.
>
> At least with that last paragraph I can agree ;)
:)
> Your change would change the second line (even if it actually allocated
> a new empty string like you probably want instead of returning null).
> How would that be consistent in any way?
Oops, my bad. My suggested code change is totally incorrect. That'll
teach me for posting while working on something else at the same time.
> (Same goes for other ways to create different-ptr empty strings)
>
> What you might have meant on that extra line might be more like:
> ---
> if (u.length == 0) return ((u.ptr is null) == (v.ptr is null));
> ---
> which will return true if both .ptr values are null or both are non-null.
Yes, and yes, I want "".dup to allocate a new 1 byte point at it and set
length to 0.
Regan
More information about the Digitalmars-d-learn
mailing list