Checking if a string is null

Regan Heath regan at netmail.co.nz
Wed Jul 25 06:41:20 PDT 2007


Bruno Medeiros wrote:
> Frits van Bommel wrote:
>> Regan Heath wrote:
>>> This all boils down to the empty vs null string debate where some 
>>> people want to be able to distinguish between them and some see no 
>>> point.
>>>
>>> I'm in the 'distinguishable' camp.  I can see the merit.  At the very 
>>> least it should be consistent!
>>
>> They *are* distinguishable. That's why above code returns different 
>> results for the 'is' comparison...
>>
> 
> The .ptr of empty arrays may be different than the .ptr of null arrays, 
> but they are conceptually the same, and thus not safely distinguishable.
> Example:
>     writefln("" is null); // false
>     writefln("".dup is null); // true
> 
> "".ptr is not null, but "".dup.ptr is null. Such duplication is correct, 
> because empty arrays are conceptually the same as null arrays, and 
> trying to use .ptr do distinguish them is unsafe, 
> implementation-depedendent behavior (aka a program error).

Ick.  IMO "".dup should allocate 1 byte of memory, set it to '\0' and 
create a reference to it with length of 0.

What do you mean by "empty arrays are conceptually the same as null 
arrays"?

To me null arrays (non-existant) and "" arrays (empty) are conceptually 
different.  null indicates the array does not exist (no set at all), "" 
indicates it does but contains no items (an empty set).

Regan


More information about the Digitalmars-d-learn mailing list