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

2017-10-13 Thread Jonathan M Davis via Digitalmars-d-learn
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



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

2017-10-13 Thread Jonathan M Davis via Digitalmars-d-learn
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,

if(!str.strip())

becomes

if(!cast(bool)(str.strip()))

which becomes

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

which is the same as

if(str.strip() !is null)

whereas what you really want is

if(str.strip().empty)

or

if(str.strip().length != 0)

- Jonathan M Davis



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

2017-10-13 Thread Daniel Kozak via Digitalmars-d-learn
Yes I beleive it is neat functionality. I just dont like those names :)

On Fri, Oct 13, 2017 at 8:36 AM, Jacob Carlborg via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> On 2017-10-12 21:42, Daniel Kozak wrote:
>
>> Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), but
>> they have IsNullOrWhiteSpace OK little akward but still OK until you
>> realized it is more like IsNullOrEmptyOrWhiteSpace :D
>>
>
> It's pretty neat functionality. Ruby has it (or rather Rails), it's called
> "blank?".
>
> --
> /Jacob Carlborg
>


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

2017-10-13 Thread bauss via Digitalmars-d-learn

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
}



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

2017-10-13 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-10-12 21:42, Daniel Kozak wrote:
Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), 
but they have IsNullOrWhiteSpace OK little akward but still OK until you 
realized it is more like IsNullOrEmptyOrWhiteSpace :D


It's pretty neat functionality. Ruby has it (or rather Rails), it's 
called "blank?".


--
/Jacob Carlborg


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

2017-10-12 Thread Daniel Kozak via Digitalmars-d-learn
Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), but
they have IsNullOrWhiteSpace OK little akward but still OK until you
realized it is more like IsNullOrEmptyOrWhiteSpace :D

On Thu, Oct 12, 2017 at 8:11 PM, Nieto via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() in the
> standard library?
> just asking if there's a native one, so I don't need reinvente the wheel
>
> https://msdn.microsoft.com/en-us/library/system.string.isnul
> lorwhitespace%28v=vs.110%29.aspx?f=255=-2147217396
>


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

2017-10-12 Thread Jesse Phillips via Digitalmars-d-learn

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
}


Also

   if(str.strip().empty)

std.array.empty() works essentially the same as IsNullOrEmpty()


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

2017-10-12 Thread Adam D. Ruppe via Digitalmars-d-learn

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
}