fix switch statement
[nedit-bw.git] / stringToNum.diff
blob96319424dd73b0ac70b22470ce23db0a030103e3
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 | 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
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 StringToNumEnd(const char *string, const char *end, int *number)
24 const char *c = string;
25 + int n = 0, new_n;
26 + int sign = 1;
27 + int haveDigit = False;
29 + /* Always assign to number to support old behavior */
30 + if (number) {
31 + *number = 0;
32 + }
33 while (*c == ' ' || *c == '\t') {
34 ++c;
36 - if (*c == '+' || *c == '-') {
37 + if (*c == '+') {
38 ++c;
39 - }
40 - while (isdigit((unsigned char)*c)) {
41 + } else if (*c == '-') {
42 ++c;
43 + sign = -1;
45 - while (*c == ' ' || *c == '\t') {
46 - ++c;
47 + if (isdigit((unsigned char)*c)) {
48 + haveDigit = True; /* now pick up any other digits */
49 + do {
50 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
51 + if (new_n == INT_MIN) {
52 + if (sign != -1) {
53 + return False; /* special case: INT_MIN must be < 0 */
54 + }
55 + } else if (new_n < n) {
56 + return False; /* overflow: digit sequence too long! */
57 + }
58 + n = new_n;
59 + ++c;
60 + } while (isdigit((unsigned char)*c));
61 + while (*c == ' ' || *c == '\t') {
62 + ++c;
63 + }
64 + }
65 + if (number) {
66 + *number = sign * n;
68 if (end ? end != c : *c) {
69 /* if everything went as expected, we should be at end, but we're not */
70 return False;
72 - if (number) {
73 - if (sscanf(string, "%d", number) != 1) {
74 - /* This case is here to support old behavior */
75 - *number = 0;
76 - }
77 - }
78 - return True;
79 + return haveDigit;
82 int StringToNum(const char *string, int *number)