CVS rebase
[nedit-bw.git] / stringToNum.diff
blobe0308130ef916a1c4f7311459421aea657d563b4
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.
5 ---
7 source/interpret.c | 43 +++++++++++++++++++++++++++++++------------
8 1 file 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 @@ -4053,33 +4053,52 @@ static int execError(const char *s1, con
14 return STAT_ERROR;
17 +/*
18 +** read an integer from a string, returning True if successful. The string must
19 +** not contain anything other than the number (perhaps surrounded by spaces).
20 +*/
21 int StringToNum(const char *string, int *number)
23 const char *c = string;
24 + int n = 0, new_n;
25 + int sign = 1;
26 + int haveDigit = False;
28 while (*c == ' ' || *c == '\t') {
29 ++c;
31 - if (*c == '+' || *c == '-') {
32 + if (*c == '+') {
33 ++c;
34 - }
35 - while (isdigit((unsigned char)*c)) {
36 + } else if (*c == '-') {
37 ++c;
38 + sign = -1;
40 - while (*c == ' ' || *c == '\t') {
41 - ++c;
42 + if (isdigit((unsigned char)*c)) {
43 + haveDigit = True; /* now pick up any other digits */
44 + do {
45 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
46 + if (new_n == INT_MIN) {
47 + if (sign != -1) {
48 + return False; /* special case: INT_MIN must be < 0 */
49 + }
50 + } else if (new_n < n) {
51 + return False; /* overflow: digit sequence too long! */
52 + }
53 + n = new_n;
54 + ++c;
55 + } while (isdigit((unsigned char)*c));
56 + while (*c == ' ' || *c == '\t') {
57 + ++c;
58 + }
59 + }
60 + if (number) {
61 + *number = sign * n;
63 if (*c) {
64 /* if everything went as expected, we should be at end, but we're not */
65 return False;
67 - if (number) {
68 - if (sscanf(string, "%d", number) != 1) {
69 - /* This case is here to support old behavior */
70 - *number = 0;
71 - }
72 - }
73 - return True;
74 + return haveDigit;
77 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */