No explicitly positive numbers!

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

No explicitly positive numbers!

Ben Schmidt
Hi, Bram,

Another oddity I've recently discovered is that str2nr won't accept an explicitly
positive number, such as '+3'.

This is unexpected. Would you be willing to adjust the behaviour? I can't think of
any negative/compatibility-breaking side effects this would have, but I also don't
know where the vim_str2nr function is used.

The attached patch does the job.

Cheers,

Ben.



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---


diff -r 64942db7407d src/charset.c
--- a/src/charset.c Sat Jun 21 23:24:54 2008 +1000
+++ b/src/charset.c Tue Jun 24 03:24:33 2008 +1000
@@ -1799,6 +1799,7 @@
     unsigned long   un = 0;
     int    n;
 
+    if (ptr[0] == '+') ++ptr;
     if (ptr[0] == '-')
     {
  negative = TRUE;
Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Andy Armstrong

On 23 Jun 2008, at 18:28, Ben Schmidt wrote:

> diff -r 64942db7407d src/charset.c
> --- a/src/charset.c Sat Jun 21 23:24:54 2008 +1000
> +++ b/src/charset.c Tue Jun 24 03:24:33 2008 +1000
> @@ -1799,6 +1799,7 @@
>     unsigned long   un = 0;
>     int    n;
>
> +    if (ptr[0] == '+') ++ptr;
>     if (ptr[0] == '-')
>     {
> negative = TRUE;

Doesn't that allow '+-3' ?

--
Andy Armstrong, Hexten




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Gautam Iyer-3
On Mon, Jun 23, 2008 at 06:30:58PM +0100, Andy Armstrong wrote:

>> diff -r 64942db7407d src/charset.c
>> --- a/src/charset.c Sat Jun 21 23:24:54 2008 +1000
>> +++ b/src/charset.c Tue Jun 24 03:24:33 2008 +1000
>> @@ -1799,6 +1799,7 @@
>>     unsigned long   un = 0;
>>     int    n;
>>
>> +    if (ptr[0] == '+') ++ptr;
>>     if (ptr[0] == '-')
>>     {
>> negative = TRUE;
>
> Doesn't that allow '+-3' ?
This seems to go unnoticed in some other languages too (e.g. Python).

GI

--
When a cat is dropped, it always lands on its feet, and when toast is
dropped, it always lands with the buttered side facing down. I propose
to strap buttered toast to the back of a cat. The two will hover,
spinning inches above the ground. With a giant buttered cat array, a
high-speed monorail could easily link New York with Chicago.

attachment0 (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Andy Armstrong

On 23 Jun 2008, at 18:34, Gautam Iyer wrote:
>> Doesn't that allow '+-3' ?
>
> This seems to go unnoticed in some other languages too (e.g. Python).


I assume that's because they support unary '+' as an operator. It's  
not part of the numeric constant:

 >>> print +(1+2)
3
 >>> print ++++++(1+2)
3
 >>> print ---1
-1

--
Andy Armstrong, Hexten




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Tony Mechelynck
In reply to this post by Ben Schmidt

On 23/06/08 19:28, Ben Schmidt wrote:

> Hi, Bram,
>
> Another oddity I've recently discovered is that str2nr won't accept an explicitly
> positive number, such as '+3'.
>
> This is unexpected. Would you be willing to adjust the behaviour? I can't think of
> any negative/compatibility-breaking side effects this would have, but I also don't
> know where the vim_str2nr function is used.
>
> The attached patch does the job.
>
> Cheers,
>
> Ben.

In +3, + is a unary operator, which in this case doesn't change the
number 3 to which it is applied.

eval('+3') gives the desired result, 3

Similarly, eval('--3') gives 3, changing the sign of the number -3 by
means of the unary - operator. (-3 is parsed as a single token, as can
be seen from the fact that str2nr('-3') == 3).

Maybe a note would be needed under ":help Number" to mention that +3 is
parsed as two tokens, viz. the unary operator + and the number 3


Best regards,
Tony.
--
"Even the best of friends cannot attend each other's funeral."
                -- Kehlog Albran, "The Profit"

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

iler.ml
In reply to this post by Ben Schmidt
On Mon, Jun 23, 2008 at 8:28 PM, Ben Schmidt <[hidden email]> wrote:
Hi, Bram,

Another oddity I've recently discovered is that str2nr won't accept an explicitly
positive number, such as '+3'.

This is unexpected. Would you be willing to adjust the behaviour? I can't think of
any negative/compatibility-breaking side effects this would have, but I also don't
know where the vim_str2nr function is used.

C functions atoi() and strtol() both do accept + in front of the number, if it matters.

Yakov
 

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Tony Mechelynck
In reply to this post by Andy Armstrong

On 23/06/08 19:37, Andy Armstrong wrote:

> On 23 Jun 2008, at 18:34, Gautam Iyer wrote:
>>> Doesn't that allow '+-3' ?
>> This seems to go unnoticed in some other languages too (e.g. Python).
>
>
> I assume that's because they support unary '+' as an operator. It's
> not part of the numeric constant:
>
>   >>>  print +(1+2)
> 3
>   >>>  print ++++++(1+2)
> 3
>   >>>  print ---1
> -1
>

Vim supports both + and - as unary operators, but apparently it supports
-3 but not +3 as a single numeric token. (Where an expression is
expected, +3 is accepted as the operator + acting on the number 3.)

For instance:
        :echo str2nr('-3')
-3
        :echo str2nr('+3')
0
        :echo str2nr('--3')
0
        :echo eval('-3')
-3
        :echo eval('+3')
3
        :echo eval('--3')
3

The difference is that str2nr() expects the string-form of a Number,
eval() expects the string-form of an expression.

I suppose that now that we know it, we can live with it.


Best regards,
Tony.
--
Baltimore, n.:
        Where the women wear turtleneck sweaters to hide their flea
collars.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Ben Schmidt
In reply to this post by Andy Armstrong

Andy Armstrong wrote:

> On 23 Jun 2008, at 18:28, Ben Schmidt wrote:
>> diff -r 64942db7407d src/charset.c
>> --- a/src/charset.c Sat Jun 21 23:24:54 2008 +1000
>> +++ b/src/charset.c Tue Jun 24 03:24:33 2008 +1000
>> @@ -1799,6 +1799,7 @@
>>     unsigned long   un = 0;
>>     int    n;
>>
>> +    if (ptr[0] == '+') ++ptr;
>>     if (ptr[0] == '-')
>>     {
>> negative = TRUE;
>
> Doesn't that allow '+-3' ?

Yeah. That was dumb, wasn't it? So make it

diff -r 64942db7407d src/charset.c
--- a/src/charset.c Sat Jun 21 23:24:54 2008 +1000
+++ b/src/charset.c Tue Jun 24 03:24:33 2008 +1000
@@ -1799,6 +1799,7 @@
     unsigned long   un = 0;
     int    n;

+    if (ptr[0] == '+') ++ptr;
-   if (ptr[0] == '-')
+   else if (ptr[0] == '-')
     {
        negative = TRUE;

Ben.



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Ben Schmidt
In reply to this post by Tony Mechelynck

Tony Mechelynck wrote:

> On 23/06/08 19:28, Ben Schmidt wrote:
>> Hi, Bram,
>>
>> Another oddity I've recently discovered is that str2nr won't accept an explicitly
>> positive number, such as '+3'.
>>
>> This is unexpected. Would you be willing to adjust the behaviour? I can't think of
>> any negative/compatibility-breaking side effects this would have, but I also don't
>> know where the vim_str2nr function is used.
>>
>> The attached patch does the job.
>>
>> Cheers,
>>
>> Ben.
>
> In +3, + is a unary operator, which in this case doesn't change the
> number 3 to which it is applied.

That's one interpretation.

But it's equally valid to interpret +3 as a number.

And since str2nr converts strings to Numbers, I feel it should accept
such a number.

> Maybe a note would be needed under ":help Number" to mention that +3 is
> parsed as two tokens, viz. the unary operator + and the number 3

If this is to be fixed by documentation...which is a dumb idea in my
opinion, as I can see no reason not to allow +3 to be a number (since +
as an operator does nothing, and + as part of a number does nothing, it
doesn't matter how it's interpreted, and from a human point of view, +3
is definitely a valid number) it should be documented at str2nr() since
that is where it breaks.

Ben.



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Bram Moolenaar
In reply to this post by Ben Schmidt


Ben Schmidt wrote:

> Another oddity I've recently discovered is that str2nr won't accept an
> explicitly positive number, such as '+3'.
>
> This is unexpected. Would you be willing to adjust the behaviour? I
> can't think of any negative/compatibility-breaking side effects this
> would have, but I also don't know where the vim_str2nr function is used.
>
> The attached patch does the job.

vim_str2nr() is used in many places, I can't quickly see the impact of
this change.  What user facing issue are you trying to solve?  If it's
about str2nr() itself then it's better solved in f_str2nr().

*** eval.c~ Mon Jun 23 20:25:08 2008
--- eval.c Mon Jun 23 21:31:49 2008
***************
*** 16165,16170 ****
--- 16165,16172 ----
      }
 
      p = skipwhite(get_tv_string(&argvars[0]));
+     if (*p == '+')
+ p = skipwhite(p + 1);
      vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
      rettv->vval.v_number = n;
  }

--
If your nose runs, and your feet smell, you might be upside down.

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Ben Schmidt

Bram Moolenaar wrote:

>
> Ben Schmidt wrote:
>
>> Another oddity I've recently discovered is that str2nr won't accept an
>> explicitly positive number, such as '+3'.
>>
>> This is unexpected. Would you be willing to adjust the behaviour? I
>> can't think of any negative/compatibility-breaking side effects this
>> would have, but I also don't know where the vim_str2nr function is used.
>>
>> The attached patch does the job.
>
> vim_str2nr() is used in many places, I can't quickly see the impact of
> this change.  What user facing issue are you trying to solve?  If it's
> about str2nr() itself then it's better solved in f_str2nr().

Yes, it is about str2nr() itself. Very happy to have it solved there.

The problem I was facing this time was passing the exponent part of
output from printf, which always has the form +\d\+

Your patch looks fine. It will accept, e.g., +-3 as well, but I don't
see why that's a problem. In a scripting language it's better to be more
permissive than less. It could be avoided by changing + into - before
calling vim_str2nr and then returning the negative of its return value.
But it's probably not worth it.

Ben.

> *** eval.c~ Mon Jun 23 20:25:08 2008
> --- eval.c Mon Jun 23 21:31:49 2008
> ***************
> *** 16165,16170 ****
> --- 16165,16172 ----
>       }
>  
>       p = skipwhite(get_tv_string(&argvars[0]));
> +     if (*p == '+')
> + p = skipwhite(p + 1);
>       vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
>       rettv->vval.v_number = n;
>   }
>

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: No explicitly positive numbers!

Ben Schmidt

Ben Schmidt wrote:

> Bram Moolenaar wrote:
>> Ben Schmidt wrote:
>>
>>> Another oddity I've recently discovered is that str2nr won't accept an
>>> explicitly positive number, such as '+3'.
>>>
>>> This is unexpected. Would you be willing to adjust the behaviour? I
>>> can't think of any negative/compatibility-breaking side effects this
>>> would have, but I also don't know where the vim_str2nr function is used.
>>>
>>> The attached patch does the job.
>> vim_str2nr() is used in many places, I can't quickly see the impact of
>> this change.  What user facing issue are you trying to solve?  If it's
>> about str2nr() itself then it's better solved in f_str2nr().
>
> Yes, it is about str2nr() itself. Very happy to have it solved there.
>
> The problem I was facing this time was passing the exponent part of
> output from printf, which always has the form +\d\+

or [+-]\d\+ even. Duh.

Ben.



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---