drop extend-if-key-in-array-syntax.patch add yacc debug
[nedit-bw.git] / stringToNum.diff
blob0e41d56150268d219a112c27f28513c3711403af
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.
6 ---
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 @@ -4088,33 +4088,52 @@ static int execError(const char *s1, con
15 return STAT_ERROR;
18 +/*
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).
21 +*/
22 int StringToNum(const char *string, int *number)
24 const char *c = string;
25 + int n = 0, new_n;
26 + int sign = 1;
27 + int haveDigit = False;
29 while (*c == ' ' || *c == '\t') {
30 ++c;
32 - if (*c == '+' || *c == '-') {
33 + if (*c == '+') {
34 ++c;
35 - }
36 - while (isdigit((unsigned char)*c)) {
37 + } else if (*c == '-') {
38 ++c;
39 + sign = -1;
41 - while (*c == ' ' || *c == '\t') {
42 - ++c;
43 + if (isdigit((unsigned char)*c)) {
44 + haveDigit = True; /* now pick up any other digits */
45 + do {
46 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
47 + if (new_n == INT_MIN) {
48 + if (sign != -1) {
49 + return False; /* special case: INT_MIN must be < 0 */
50 + }
51 + } else if (new_n < n) {
52 + return False; /* overflow: digit sequence too long! */
53 + }
54 + n = new_n;
55 + ++c;
56 + } while (isdigit((unsigned char)*c));
57 + while (*c == ' ' || *c == '\t') {
58 + ++c;
59 + }
60 + }
61 + if (number) {
62 + *number = sign * n;
64 if (*c) {
65 /* if everything went as expected, we should be at end, but we're not */
66 return False;
68 - if (number) {
69 - if (sscanf(string, "%d", number) != 1) {
70 - /* This case is here to support old behavior */
71 - *number = 0;
72 - }
73 - }
74 - return True;
75 + return haveDigit;