update to arrayReplacesArglist4.diff
[nedit-bw.git] / stringToNum.diff
blob7c03d63c4a171fc4900473b7b8ce73b677e03d4a
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 files 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 @@ -3499,37 +3499,56 @@ static int execError(const char *s1, con
14 msg[MAX_ERR_MSG_LEN - 1] = '\0';
15 ErrMsg = stackDumpStr(FrameP, msg, &err, &errlen);
16 return STAT_ERROR;
19 +/*
20 +** read an integer from a string, returning True if successful. The string must
21 +** not contain anything other than the number (perhaps surrounded by spaces).
22 +*/
23 int StringToNum(const char *string, int *number)
25 const char *c = string;
26 + int n = 0, new_n;
27 + int sign = 1;
28 + int haveDigit = False;
30 while (*c == ' ' || *c == '\t') {
31 ++c;
33 - if (*c == '+' || *c == '-') {
34 + if (*c == '+') {
35 ++c;
36 - }
37 - while (isdigit((unsigned char)*c)) {
38 + } else if (*c == '-') {
39 ++c;
40 + sign = -1;
42 - while (*c == ' ' || *c == '\t') {
43 - ++c;
44 + if (isdigit((unsigned char)*c)) {
45 + haveDigit = True; /* now pick up any other digits */
46 + do {
47 + new_n = 10 * n + *c - '0'; /* evaluate the number as we go */
48 + if (new_n == INT_MIN) {
49 + if (sign != -1) {
50 + return False; /* special case: INT_MIN must be < 0 */
51 + }
52 + } else if (new_n < n) {
53 + return False; /* overflow: digit sequence too long! */
54 + }
55 + n = new_n;
56 + ++c;
57 + } while (isdigit((unsigned char)*c));
58 + while (*c == ' ' || *c == '\t') {
59 + ++c;
60 + }
61 + }
62 + if (number) {
63 + *number = sign * n;
65 if (*c) {
66 /* if everything went as expected, we should be at end, but we're not */
67 return False;
69 - if (number) {
70 - if (sscanf(string, "%d", number) != 1) {
71 - /* This case is here to support old behavior */
72 - *number = 0;
73 - }
74 - }
75 - return True;
76 + return haveDigit;
79 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
80 static char *printdBuffer = NULL;
81 static int printdPos = 0;