Checking if a string is null

Regan Heath regan at
Thu Jul 26 01:18:05 PDT 2007

Oskar Linde wrote:
>>>> This should mean "" == "" but not "" == null, likewise null == null 
>>>> but not null == "".
> This would mean that "two arrays are equal if all elements are equal" 
> would no longer hold. 

Not true, the two arrays you mention below would still compare 'true' as 
their contents are still equal.

Ignore the suggested code changes, my one was patently incorrect and the 
first step is to make it clear what behaviour is desired, something I 
have obviously not done.

 > (Consider two zero length slices at arbitrary
> memory location, neither of them null).

The content of these arrays is equal and would compare so.

The case(s) I want to stop comparing as equal are:

null == ""
"" == null

The cases which should continue to compare equal are:

null == null
"" == ""  (your example above)

No more, no less.


p.s. I know I said ignore the suggested code changes but it would have 
to go something like:

if (lhs.length == 0) {
   if (lhs.ptr && rhs.ptr) return true;  //"" == ""
   if (lhs.ptr || rhs.ptr) return false  //"" == null && null == ""
   return true;                          //null == null

More information about the Digitalmars-d-learn mailing list