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 | 47 +++++++++++++++++++++++++++++++++++------------
9 1 file changed, 35 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 @@ -4089,33 +4089,56 @@ 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 StringToNumEnd(const char *string, const char *end, int *number)
24 const char *c = string;
27 + int haveDigit = False;
29 + /* Always assign to number to support old behavior */
33 while (*c == ' ' || *c == '\t') {
36 - if (*c == '+' || *c == '-') {
40 - while (isdigit((unsigned char)*c)) {
41 + } else if (*c == '-') {
45 - while (*c == ' ' || *c == '\t') {
47 + if (isdigit((unsigned char)*c)) {
48 + haveDigit = True; /* now pick up any other digits */
50 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
51 + if (new_n == INT_MIN) {
53 + return False; /* special case: INT_MIN must be < 0 */
55 + } else if (new_n < n) {
56 + return False; /* overflow: digit sequence too long! */
60 + } while (isdigit((unsigned char)*c));
61 + while (*c == ' ' || *c == '\t') {
68 if (end ? end != c : *c) {
69 /* if everything went as expected, we should be at end, but we're not */
73 - if (sscanf(string, "%d", number) != 1) {
74 - /* This case is here to support old behavior */
82 int StringToNum(const char *string, int *number)