[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Groff] Readable tbl-source
From: |
Ralph Corderoy |
Subject: |
Re: [Groff] Readable tbl-source |
Date: |
Sun, 08 Sep 2002 18:10:48 +0100 |
Hi Werner,
> Indeed, a very nice idea, but no longer necessary: In the CVS, I've
> just added a new global option `nospaces' to make tbl ignore leading
> and trailing spaces in data fields.
Sounds good. I'm not up to speed with C++ but something looks a bit
odd.
1 char *string::extract() const
2 {
3 char *p = ptr;
4 int n = len;
5 int nnuls = 0;
6 int i;
7 for (i = 0; i < n; i++)
8 if (p[i] == '\0')
9 nnuls++;
10 char *q = new char[n + 1 - nnuls];
11 char *r = q;
12 for (i = 0; i < n; i++)
13 if (p[i] != '\0')
14 *r++ = p[i];
15 q[n] = '\0';
16 return q;
17 }
Does `new char[n]' give memory pre-filled with '\0'? I guess not
because #15 puts the terminating '\0' in place. But if on entry ptr ==
"a\0c\0" and len == 3 then #7 loops through "a\0c" and by #10 nnuls ==
1. This means `n + 1 - nnuls' == 3 which is correct to hold "ac\0".
But won't #15 be writing to q[3] which is past the end of q? Shouldn't
it be `*r = '\0';'?
Assuming I'm right that then...
19 void string::remove_spaces()
20 {
21 int l = len - 1;
22 while (l >= 0 && ptr[l] == ' ')
23 l--;
24 char *p = ptr;
25 if (l > 0)
26 while (*p == ' ') {
27 p++;
28 l--;
29 }
30 if (len - 1 != l) {
31 if (l >= 0) {
32 len = l + 1;
33 char *tmp = new char[len];
34 memcpy(tmp, p, len);
35 a_delete ptr;
36 ptr = tmp;
37 }
38 else {
39 len = 0;
40 if (ptr) {
41 a_delete ptr;
42 ptr = 0;
43 }
44 }
45 }
46 }
given ptr == "abc \0" and len == 4 by #33 len == 3 and #34 will copy
"abc" without a '\0' character on the end.
The two routines don't seem to match in terms of what ptr holds, a \0
terminated string or not.
As I said, I could be way off beam on this.
Cheers,
Ralph.