1 From: Tony Balinski <ajbj@free.fr>
2 Subject: A new StringToNum() function without a call to sscanf()
4 It seems a shame to scan a string twice, as the older version does.
8 source/interpret.c | 43 +++++++++++++++++++++++++++++++------------
9 1 file changed, 31 insertions(+), 12 deletions(-)
11 diff --quilt old/source/interpret.c new/source/interpret.c
12 --- old/source/interpret.c
13 +++ new/source/interpret.c
14 @@ -4243,33 +4243,52 @@ static int execError(const char *s1, con
19 +** read an integer from a string, returning True if successful. The string must
20 +** not contain anything other than the number (perhaps surrounded by spaces).
22 int StringToNum(const char *string, int *number)
24 const char *c = string;
27 + int haveDigit = False;
29 while (*c == ' ' || *c == '\t') {
32 - if (*c == '+' || *c == '-') {
36 - while (isdigit((unsigned char)*c)) {
37 + } else if (*c == '-') {
41 - while (*c == ' ' || *c == '\t') {
43 + if (isdigit((unsigned char)*c)) {
44 + haveDigit = True; /* now pick up any other digits */
46 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
47 + if (new_n == INT_MIN) {
49 + return False; /* special case: INT_MIN must be < 0 */
51 + } else if (new_n < n) {
52 + return False; /* overflow: digit sequence too long! */
56 + } while (isdigit((unsigned char)*c));
57 + while (*c == ' ' || *c == '\t') {
65 /* if everything went as expected, we should be at end, but we're not */
69 - if (sscanf(string, "%d", number) != 1) {
70 - /* This case is here to support old behavior */