1747Bug in meAtoi -- leading zeros
- Dec 19, 2005From what I can see there is a bug in the atoi routine used.
I first noticed this when I was trying to remove leading zeroes from
part of the ascii-time variable:
set-variable #l1 "09"
insert-string " -> "
set-variable #l1 &add #l1 0 ; "09" -> "9"
The output of this is
09 -> 0
when it should really be
9 -> 9
If I change the set-variable to "9", it comes out
9 -> 9
I tracked down the function, it ends up being in eval.c
case UFADD: return meItoa(meAtoi(arg1) + meAtoi(arg2));
in eextrn.h it is defined
#define meAtoi(s) strtol((char *)(s),(char **)NULL,0)
and the 0 here is the base, and is a special value meaning, interpret
strings beginning with 0x as hex, and strings beginning with 0 as octal.
Which is why my "09" comes out as zero: 9 is undefined in octal.
Sure enough, if I change my input string to "07", I get
07 -> 7
So, my workaround is going to be to prepend a "1" to make it a decimal
1xx number and then subtract off the 100:
set-variable #l1 &sub &cat "1" #l1 100 ; "09" -> "9"
What I really want to do is fix the base to be, e.g., 10, but would that
break other things? I don't know.
- Next post in topic >>