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 files 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 @@ -4026,37 +4026,56 @@ static int execError(const char *s1, con
14 msg[MAX_ERR_MSG_LEN - 1] = '\0';
15 ErrMsg = stackDumpStr(FrameP, msg, &err, &errlen);
20 +** read an integer from a string, returning True if successful. The string must
21 +** not contain anything other than the number (perhaps surrounded by spaces).
23 int StringToNum(const char *string, int *number)
25 const char *c = string;
28 + int haveDigit = False;
30 while (*c == ' ' || *c == '\t') {
33 - if (*c == '+' || *c == '-') {
37 - while (isdigit((unsigned char)*c)) {
38 + } else if (*c == '-') {
42 - while (*c == ' ' || *c == '\t') {
44 + if (isdigit((unsigned char)*c)) {
45 + haveDigit = True; /* now pick up any other digits */
47 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
48 + if (new_n == INT_MIN) {
50 + return False; /* special case: INT_MIN must be < 0 */
52 + } else if (new_n < n) {
53 + return False; /* overflow: digit sequence too long! */
57 + } while (isdigit((unsigned char)*c));
58 + while (*c == ' ' || *c == '\t') {
66 /* if everything went as expected, we should be at end, but we're not */
70 - if (sscanf(string, "%d", number) != 1) {
71 - /* This case is here to support old behavior */
79 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
80 static char *printdBuffer = NULL;
81 static int printdPos = 0;