On Thu, 8 Mar 2018, Sven Schreiber wrote:
Am 08.03.2018 um 20:04 schrieb Allin Cottrell:
> On Thu, 8 Mar 2018, Sven Schreiber wrote:
>
>> Am 08.03.2018 um 10:23 schrieb oleg_komashko(a)ukr.net:
>>> It looks like 'if' can evaluate non-empty string
>>> alone as 1 but can't evaluate logical expressions
>>> with string input
>>
>> OK, thanks for pointing that out. I tend to think --apart from
>> backward-compatibility reasons-- that either stuff like 'if
"a"' should be
>> banned, or the full logical implications should work as well.
>
> Yep, either we ensure that the result of "if s", for s a string (literal,
> variable or expression), is consistently equivalent to "if strlen(s) != 0",
> or else we ban this usage as too obscure.
>
> I think my preference would be for the latter, unless there's a significant
> compatibility issue.
OK, this is where it might get complicated. Personally I used to do a check
like:
if strlen(strstr("what", "ever"))
but when I discovered that 'if strstr("what", "ever")' worked
the same, I
switched over to this shorter formulation some time ago.
Now I can go through my packages and change it back and it wouldn't be a
major issue. But can we assume (or systematically search) that no one else
has done that?
Assume? No. Systematic search? Not easy.
Since a release is imminent I think it's better to go with fixing up
what we have, at least for now. So in git the following now all work
as expected -- "as expected", that is, if you know that in a boolean
context a string just counts as empty (false) or not (true).
<hansl>
if strstr("abc", "a") && strstr("abc",
"b")
print "true"
else
print "false"
endif
if strstr("abc", "a") && strstr("abc",
"d")
print "true"
else
print "false"
endif
if (1 == 1) && strstr("abc", "a")
print "true"
else
print "false"
endif
# and for good measure...
printf "%s\n", strstr("abc", "a") ? "true" :
"false"
</hansl>
Actually I'm quite happy with the strstr() case, no doubt because of
the analogy with the C idiom "if (strstr(s1, s2))" as equivalent to
"if (strstr(s1, s2) != NULL)" -- though we don't have true NULL
(pointer-zero) in hansl because it's too dangerous, just empty
strings.
It's the 'if "a"' case that makes me a bit uneasy, but maybe I
should just get over it!
Allin