1 A new StringToNum() function without a call to sscanf()
3 It seems a shame to scan a string twice, as the older version does.
7 source/interpret.c | 43 +++++++++++++++++++++++++++++++------------
8 1 file changed, 31 insertions(+), 12 deletions(-)
10 diff --quilt old/source/interpret.c new/source/interpret.c
11 --- old/source/interpret.c
12 +++ new/source/interpret.c
13 @@ -4053,33 +4053,52 @@ static int execError(const char *s1, con
18 +** read an integer from a string, returning True if successful. The string must
19 +** not contain anything other than the number (perhaps surrounded by spaces).
21 int StringToNum(const char *string, int *number)
23 const char *c = string;
26 + int haveDigit = False;
28 while (*c == ' ' || *c == '\t') {
31 - if (*c == '+' || *c == '-') {
35 - while (isdigit((unsigned char)*c)) {
36 + } else if (*c == '-') {
40 - while (*c == ' ' || *c == '\t') {
42 + if (isdigit((unsigned char)*c)) {
43 + haveDigit = True; /* now pick up any other digits */
45 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
46 + if (new_n == INT_MIN) {
48 + return False; /* special case: INT_MIN must be < 0 */
50 + } else if (new_n < n) {
51 + return False; /* overflow: digit sequence too long! */
55 + } while (isdigit((unsigned char)*c));
56 + while (*c == ' ' || *c == '\t') {
64 /* if everything went as expected, we should be at end, but we're not */
68 - if (sscanf(string, "%d", number) != 1) {
69 - /* This case is here to support old behavior */
77 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */