Does D have an equivalent to C#'s String.IsNullOrWhiteSpace?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Oct 13 08:22:29 UTC 2017


On Friday, October 13, 2017 02:12:21 Jonathan M Davis via Digitalmars-d-
learn wrote:
> On Friday, October 13, 2017 07:36:28 bauss via Digitalmars-d-learn wrote:
> > On Thursday, 12 October 2017 at 18:17:54 UTC, Adam D. Ruppe wrote:
> > > On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:
> > >> Does D have an equivalent to C#'s String.IsNullOrWhiteSpace()
> > >> in the standard library?
> > >
> > > import std.string;
> > >
> > > if(str.strip().length == 0) {
> > >
> > >   // is null, empty, or all whitespace
> > >
> > > }
> >
> > Or this:
> > if(!str.strip()) {
> >
> >    // is null, empty, or all whitespace
> >
> > }
>
> Nope. That's wrong. You should basically never test a string (or any type
> of dynamic array) with an if statement, loop condition, or assertion. It
> doesn't check whether the array is empty. It checks whether it's null.
> So, for instance, this code
>
> -------------------------
> import std.stdio;
> import std.string;
>
> void main()
> {
>     string str1 = "hello";
>
>     writeln(isNullOrEmptyOrWS("hello"));
>     writeln(isNullOrEmptyOrWS("     "));
>     writeln(isNullOrEmptyOrWS(""));
>     writeln(isNullOrEmptyOrWS(null));
>
> }
>
> bool isNullOrEmptyOrWS(string str)
> {
>     if(!str.strip())
>         return true;
>     return false;
> }
> -------------------------
>
> prints out
>
> false
> false
> false
> true
>
> whereas what you'd want is
>
> false
> true
> true
> true
>
> Putting anything in an if condition is an implicit, explict cast to bool
> (it's lowered to an explicit cast by the compiler, so its semantics are
> that of an explicit cast, but it's implicit in that the compiler does it
> for you).
>
> if(cond)
>
> is lowered to
>
> if(cast(bool)cond)
>
> and
>
> if(!cond)
>
> is lowered to
>
> if(!cast(bool)cond)
>
> and casting any dynamic array to a bool is equivalent to arr !is null. So,

Oh drat. I did this too quickly and wrote some of this backwards.

> if(!str.strip())
>
> becomes
>
> if(!cast(bool)(str.strip()))
>
> which becomes
>
> if(!(str.strip() is null))

Actually, it becomes

if(!(str.strip() !is null))

because

cast(bool)arr

is results in true if the array is _not_ null.

> which is the same as
>
> if(str.strip() !is null)

So, this is really

if(str.strip() is null)

> whereas what you really want is
>
> if(str.strip().empty)
>
> or
>
> if(str.strip().length != 0)

Hopefully, I got my point across in spite of screwing up some of the details
in the lowerings. Just don't test dynamic arrays for whether they're true or
not, because it's rarely what you want - and when it is what you want,
anyone reading your code won't have any way of knowing whether you're
purposefully checking for non-null or whether you just misunderstood and
thought that you were checking for non-empty (and the odds are generally
much higher that the person who wrote the code misunderstood). So, even if
you know what it does, I'd argue that it's bad practice to do it. If you
want to check for null, then do it explicitly, and if you want to check for
empty, then do it explicitly. Then there's no ambiguity.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list