From: David Kastrup <hidden> Date: 2016-06-15 22:43:36
"David Symonds" [off-list ref] writes:
On 24/09/2007, David Kastrup [off-list ref] wrote:
quoted
Mike Hommey [off-list ref] writes:
quoted
On Sun, Sep 23, 2007 at 10:42:08PM +0200, David Kastrup wrote:
quoted
-while case $# in 0) break ;; esac
+while test $# != 0
Wouldn't -ne be better ?
Why?
Because -ne does a numeric comparison, != does a string comparison,
and it's a numeric comparison happening, semantically speaking.
I don't see the point in converting $# and 0 into numbers before
comparing them. "!=" is quite more readable, and the old code also
compared the strings.
--
David Kastrup
From: Pierre Habouzit <hidden> Date: 2016-06-15 22:43:36
On Mon, Sep 24, 2007 at 07:57:31AM +0000, David Kastrup wrote:
"David Symonds" [off-list ref] writes:
quoted
On 24/09/2007, David Kastrup [off-list ref] wrote:
quoted
Mike Hommey [off-list ref] writes:
quoted
On Sun, Sep 23, 2007 at 10:42:08PM +0200, David Kastrup wrote:
quoted
-while case $# in 0) break ;; esac
+while test $# != 0
Wouldn't -ne be better ?
Why?
Because -ne does a numeric comparison, != does a string comparison,
and it's a numeric comparison happening, semantically speaking.
I don't see the point in converting $# and 0 into numbers before
comparing them. "!=" is quite more readable, and the old code also
compared the strings.
Fwiw $# already is a number. Hence test $# -ne 0 is definitely a
better test.
$# != 0 would yield sth like (strcmp(sprintf("%d", argc), "0"))
$# -ne 0 would yield sth like (argc != atoi("0")).
Not that it matters much, but the latter looks better to me.
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org
From: Pierre Habouzit <hidden> Date: 2016-06-15 22:43:36
On Mon, Sep 24, 2007 at 08:01:34AM +0000, Pierre Habouzit wrote:
On Mon, Sep 24, 2007 at 07:57:31AM +0000, David Kastrup wrote:
quoted
"David Symonds" [off-list ref] writes:
quoted
On 24/09/2007, David Kastrup [off-list ref] wrote:
quoted
Mike Hommey [off-list ref] writes:
quoted
On Sun, Sep 23, 2007 at 10:42:08PM +0200, David Kastrup wrote:
quoted
-while case $# in 0) break ;; esac
+while test $# != 0
Wouldn't -ne be better ?
Why?
Because -ne does a numeric comparison, != does a string comparison,
and it's a numeric comparison happening, semantically speaking.
I don't see the point in converting $# and 0 into numbers before
comparing them. "!=" is quite more readable, and the old code also
compared the strings.
Fwiw $# already is a number. Hence test $# -ne 0 is definitely a
better test.
$# != 0 would yield sth like (strcmp(sprintf("%d", argc), "0"))
$# -ne 0 would yield sth like (argc != atoi("0")).
Of course this holds only for shell where test/[ is a builtin, which
is the at least the case for zsh, bash, and dash (but not posh).
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org
From: David Kastrup <hidden> Date: 2016-06-15 22:43:36
Pierre Habouzit [off-list ref] writes:
On Mon, Sep 24, 2007 at 07:57:31AM +0000, David Kastrup wrote:
quoted
"David Symonds" [off-list ref] writes:
quoted
On 24/09/2007, David Kastrup [off-list ref] wrote:
quoted
Mike Hommey [off-list ref] writes:
quoted
On Sun, Sep 23, 2007 at 10:42:08PM +0200, David Kastrup wrote:
quoted
-while case $# in 0) break ;; esac
+while test $# != 0
Wouldn't -ne be better ?
Why?
Because -ne does a numeric comparison, != does a string comparison,
and it's a numeric comparison happening, semantically speaking.
I don't see the point in converting $# and 0 into numbers before
comparing them. "!=" is quite more readable, and the old code also
compared the strings.
Fwiw $# already is a number.
It isn't.
Hence test $# -ne 0 is definitely a better test.
/* TEST/[ builtin. */
int
test_builtin (list)
WORD_LIST *list;
{
char **argv;
int argc, result;
/* We let Matthew Bradburn and Kevin Braunsdorf's code do the
actual test command. So turn the list of args into an array
of strings, since that is what their code wants. */
if (list == 0)
{
if (this_command_name[0] == '[' && !this_command_name[1])
{
builtin_error ("missing `]'");
return (EX_BADUSAGE);
}
return (EXECUTION_FAILURE);
}
argv = make_builtin_argv (list, &argc);
result = test_command (argc, argv);
free ((char *)argv);
return (result);
}
$# != 0 would yield sth like (strcmp(sprintf("%d", argc), "0"))
$# -ne 0 would yield sth like (argc != atoi("0")).
Not that it matters much, but the latter looks better to me.
Not to me. The code does not support your argument, and all $x
expansions certainly are strings, according to manual and usage. I
will rework the patch this evening in order to get a commit message
more placable to Junio, and I will at his request remove all of the
(redundant) quoting. But removing quoting from $# does not turn it
into a number: it remains the same string '0'. If someone else feels
he should replace all "=" and "!=" tests for "numeric" comparisons
with the unreadable numeric tests, he can go ahead proposing a
separate patch that should not just cover $#.
You can have bash declare numeric variables, but even they are strings
(they just auto-evaluate on assignment):
declare -i nonsense
nonsense="3+$(echo 4)"
echo "$nonsense"
gives 7, even though everything has been "strings" here.
--
David Kastrup
From: Johannes Schindelin <hidden> Date: 2016-06-15 22:43:36
Hi,
On Mon, 24 Sep 2007, Pierre Habouzit wrote:
On Mon, Sep 24, 2007 at 08:01:34AM +0000, Pierre Habouzit wrote:
quoted
On Mon, Sep 24, 2007 at 07:57:31AM +0000, David Kastrup wrote:
quoted
"David Symonds" [off-list ref] writes:
quoted
On 24/09/2007, David Kastrup [off-list ref] wrote:
quoted
Mike Hommey [off-list ref] writes:
quoted
On Sun, Sep 23, 2007 at 10:42:08PM +0200, David Kastrup wrote:
quoted
-while case $# in 0) break ;; esac
+while test $# != 0
Wouldn't -ne be better ?
Why?
Because -ne does a numeric comparison, != does a string comparison,
and it's a numeric comparison happening, semantically speaking.
I don't see the point in converting $# and 0 into numbers before
comparing them. "!=" is quite more readable, and the old code also
compared the strings.
Fwiw $# already is a number. Hence test $# -ne 0 is definitely a
better test.
$# != 0 would yield sth like (strcmp(sprintf("%d", argc), "0"))
$# -ne 0 would yield sth like (argc != atoi("0")).
Of course this holds only for shell where test/[ is a builtin, which
is the at least the case for zsh, bash, and dash (but not posh).
The reason we used "case" is that this has always been a builtin (has to
be, because it changes workflow).
Therefore I am somewhat uneasy that the patch went in so easily,
especially given a message that flies in the face of our endeavours to
make git less dependent on any given shell (as long as it is not broken to
begin with).
Ciao,
Dscho
From: David Kastrup <hidden> Date: 2016-06-15 22:43:36
Johannes Schindelin [off-list ref] writes:
The reason we used "case" is that this has always been a builtin
(has to be, because it changes workflow).
Therefore I am somewhat uneasy that the patch went in so easily,
It didn't yet.
especially given a message that flies in the face of our endeavours
to make git less dependent on any given shell (as long as it is not
broken to begin with).
"test" is not actually a shell dependency since it is available as an
external when not available as builtin. And if you really want to
prefer "case" over "test" because the latter is not a built-in in a
small number of shells, then it should be done consistently everywhere
and not just in code I touch.
--
David Kastrup