line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
#include |
4
|
|
|
|
|
|
|
#include |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#include "header.h" |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#define unless(C) if(!(C)) |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#define CREATE_SIZE 1 |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
|
|
|
extern symbol * create_s(void) { |
13
|
|
|
|
|
|
|
symbol * p; |
14
|
2
|
|
|
|
|
|
void * mem = malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol)); |
15
|
2
|
50
|
|
|
|
|
if (mem == NULL) return NULL; |
16
|
2
|
|
|
|
|
|
p = (symbol *) (HEAD + (char *) mem); |
17
|
2
|
|
|
|
|
|
CAPACITY(p) = CREATE_SIZE; |
18
|
2
|
|
|
|
|
|
SET_SIZE(p, CREATE_SIZE); |
19
|
2
|
|
|
|
|
|
return p; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
|
extern void lose_s(symbol * p) { |
23
|
2
|
50
|
|
|
|
|
if (p == NULL) return; |
24
|
2
|
|
|
|
|
|
free((char *) p - HEAD); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
/* |
28
|
|
|
|
|
|
|
new_p = skip_utf8(p, c, lb, l, n); skips n characters forwards from p + c |
29
|
|
|
|
|
|
|
if n +ve, or n characters backwards from p + c - 1 if n -ve. new_p is the new |
30
|
|
|
|
|
|
|
position, or 0 on failure. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
-- used to implement hop and next in the utf8 case. |
33
|
|
|
|
|
|
|
*/ |
34
|
|
|
|
|
|
|
|
35
|
104
|
|
|
|
|
|
extern int skip_utf8(const symbol * p, int c, int lb, int l, int n) { |
36
|
|
|
|
|
|
|
int b; |
37
|
104
|
50
|
|
|
|
|
if (n >= 0) { |
38
|
192
|
100
|
|
|
|
|
for (; n > 0; n--) { |
39
|
104
|
100
|
|
|
|
|
if (c >= l) return -1; |
40
|
88
|
|
|
|
|
|
b = p[c++]; |
41
|
88
|
50
|
|
|
|
|
if (b >= 0xC0) { /* 1100 0000 */ |
42
|
0
|
0
|
|
|
|
|
while (c < l) { |
43
|
0
|
|
|
|
|
|
b = p[c]; |
44
|
0
|
0
|
|
|
|
|
if (b >= 0xC0 || b < 0x80) break; |
45
|
|
|
|
|
|
|
/* break unless b is 10------ */ |
46
|
0
|
|
|
|
|
|
c++; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} else { |
51
|
0
|
0
|
|
|
|
|
for (; n < 0; n++) { |
52
|
0
|
0
|
|
|
|
|
if (c <= lb) return -1; |
53
|
0
|
|
|
|
|
|
b = p[--c]; |
54
|
0
|
0
|
|
|
|
|
if (b >= 0x80) { /* 1000 0000 */ |
55
|
0
|
0
|
|
|
|
|
while (c > lb) { |
56
|
0
|
|
|
|
|
|
b = p[c]; |
57
|
0
|
0
|
|
|
|
|
if (b >= 0xC0) break; /* 1100 0000 */ |
58
|
0
|
|
|
|
|
|
c--; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
return c; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
/* Code for character groupings: utf8 cases */ |
67
|
|
|
|
|
|
|
|
68
|
128
|
|
|
|
|
|
static int get_utf8(const symbol * p, int c, int l, int * slot) { |
69
|
|
|
|
|
|
|
int b0, b1; |
70
|
128
|
100
|
|
|
|
|
if (c >= l) return 0; |
71
|
120
|
|
|
|
|
|
b0 = p[c++]; |
72
|
120
|
100
|
|
|
|
|
if (b0 < 0xC0 || c == l) { /* 1100 0000 */ |
73
|
112
|
|
|
|
|
|
* slot = b0; return 1; |
74
|
|
|
|
|
|
|
} |
75
|
8
|
|
|
|
|
|
b1 = p[c++]; |
76
|
8
|
50
|
|
|
|
|
if (b0 < 0xE0 || c == l) { /* 1110 0000 */ |
77
|
8
|
|
|
|
|
|
* slot = (b0 & 0x1F) << 6 | (b1 & 0x3F); return 2; |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
* slot = (b0 & 0xF) << 12 | (b1 & 0x3F) << 6 | (p[c] & 0x3F); return 3; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
static int get_b_utf8(const symbol * p, int c, int lb, int * slot) { |
83
|
|
|
|
|
|
|
int b0, b1; |
84
|
0
|
0
|
|
|
|
|
if (c <= lb) return 0; |
85
|
0
|
|
|
|
|
|
b0 = p[--c]; |
86
|
0
|
0
|
|
|
|
|
if (b0 < 0x80 || c == lb) { /* 1000 0000 */ |
87
|
0
|
|
|
|
|
|
* slot = b0; return 1; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
b1 = p[--c]; |
90
|
0
|
0
|
|
|
|
|
if (b1 >= 0xC0 || c == lb) { /* 1100 0000 */ |
91
|
0
|
|
|
|
|
|
* slot = (b1 & 0x1F) << 6 | (b0 & 0x3F); return 2; |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
* slot = (p[c] & 0xF) << 12 | (b1 & 0x3F) << 6 | (b0 & 0x3F); return 3; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
64
|
|
|
|
|
|
extern int in_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
97
|
|
|
|
|
|
|
do { |
98
|
|
|
|
|
|
|
int ch; |
99
|
48
|
|
|
|
|
|
int w = get_utf8(z->p, z->c, z->l, & ch); |
100
|
72
|
100
|
|
|
|
|
unless (w) return -1; |
101
|
40
|
50
|
|
|
|
|
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
102
|
|
|
|
|
|
|
return w; |
103
|
16
|
|
|
|
|
|
z->c += w; |
104
|
16
|
50
|
|
|
|
|
} while (repeat); |
105
|
|
|
|
|
|
|
return 0; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
extern int in_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
109
|
|
|
|
|
|
|
do { |
110
|
|
|
|
|
|
|
int ch; |
111
|
0
|
|
|
|
|
|
int w = get_b_utf8(z->p, z->c, z->lb, & ch); |
112
|
0
|
0
|
|
|
|
|
unless (w) return -1; |
113
|
0
|
0
|
|
|
|
|
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return w; |
115
|
0
|
|
|
|
|
|
z->c -= w; |
116
|
0
|
0
|
|
|
|
|
} while (repeat); |
117
|
|
|
|
|
|
|
return 0; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
64
|
|
|
|
|
|
extern int out_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
121
|
|
|
|
|
|
|
do { |
122
|
|
|
|
|
|
|
int ch; |
123
|
80
|
|
|
|
|
|
int w = get_utf8(z->p, z->c, z->l, & ch); |
124
|
112
|
50
|
|
|
|
|
unless (w) return -1; |
125
|
80
|
50
|
|
|
|
|
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
126
|
|
|
|
|
|
|
return w; |
127
|
48
|
|
|
|
|
|
z->c += w; |
128
|
48
|
50
|
|
|
|
|
} while (repeat); |
129
|
|
|
|
|
|
|
return 0; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
extern int out_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
133
|
|
|
|
|
|
|
do { |
134
|
|
|
|
|
|
|
int ch; |
135
|
0
|
|
|
|
|
|
int w = get_b_utf8(z->p, z->c, z->lb, & ch); |
136
|
0
|
0
|
|
|
|
|
unless (w) return -1; |
137
|
0
|
0
|
|
|
|
|
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
138
|
|
|
|
|
|
|
return w; |
139
|
0
|
|
|
|
|
|
z->c -= w; |
140
|
0
|
0
|
|
|
|
|
} while (repeat); |
141
|
|
|
|
|
|
|
return 0; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
/* Code for character groupings: non-utf8 cases */ |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
extern int in_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
147
|
|
|
|
|
|
|
do { |
148
|
|
|
|
|
|
|
int ch; |
149
|
0
|
0
|
|
|
|
|
if (z->c >= z->l) return -1; |
150
|
0
|
|
|
|
|
|
ch = z->p[z->c]; |
151
|
0
|
0
|
|
|
|
|
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
152
|
|
|
|
|
|
|
return 1; |
153
|
0
|
|
|
|
|
|
z->c++; |
154
|
0
|
0
|
|
|
|
|
} while (repeat); |
155
|
|
|
|
|
|
|
return 0; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
extern int in_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
159
|
|
|
|
|
|
|
do { |
160
|
|
|
|
|
|
|
int ch; |
161
|
0
|
0
|
|
|
|
|
if (z->c <= z->lb) return -1; |
162
|
0
|
|
|
|
|
|
ch = z->p[z->c - 1]; |
163
|
0
|
0
|
|
|
|
|
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
164
|
|
|
|
|
|
|
return 1; |
165
|
0
|
|
|
|
|
|
z->c--; |
166
|
0
|
0
|
|
|
|
|
} while (repeat); |
167
|
|
|
|
|
|
|
return 0; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
extern int out_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
171
|
|
|
|
|
|
|
do { |
172
|
|
|
|
|
|
|
int ch; |
173
|
0
|
0
|
|
|
|
|
if (z->c >= z->l) return -1; |
174
|
0
|
|
|
|
|
|
ch = z->p[z->c]; |
175
|
0
|
0
|
|
|
|
|
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
176
|
|
|
|
|
|
|
return 1; |
177
|
0
|
|
|
|
|
|
z->c++; |
178
|
0
|
0
|
|
|
|
|
} while (repeat); |
179
|
|
|
|
|
|
|
return 0; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
extern int out_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) { |
183
|
|
|
|
|
|
|
do { |
184
|
|
|
|
|
|
|
int ch; |
185
|
0
|
0
|
|
|
|
|
if (z->c <= z->lb) return -1; |
186
|
0
|
|
|
|
|
|
ch = z->p[z->c - 1]; |
187
|
0
|
0
|
|
|
|
|
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
188
|
|
|
|
|
|
|
return 1; |
189
|
0
|
|
|
|
|
|
z->c--; |
190
|
0
|
0
|
|
|
|
|
} while (repeat); |
191
|
|
|
|
|
|
|
return 0; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
extern int eq_s(struct SN_env * z, int s_size, const symbol * s) { |
195
|
0
|
0
|
|
|
|
|
if (z->l - z->c < s_size || memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) return 0; |
|
|
0
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
z->c += s_size; return 1; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
extern int eq_s_b(struct SN_env * z, int s_size, const symbol * s) { |
200
|
0
|
0
|
|
|
|
|
if (z->c - z->lb < s_size || memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) return 0; |
|
|
0
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
z->c -= s_size; return 1; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
extern int eq_v(struct SN_env * z, const symbol * p) { |
205
|
0
|
|
|
|
|
|
return eq_s(z, SIZE(p), p); |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
extern int eq_v_b(struct SN_env * z, const symbol * p) { |
209
|
0
|
|
|
|
|
|
return eq_s_b(z, SIZE(p), p); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
8
|
|
|
|
|
|
extern int find_among(struct SN_env * z, const struct among * v, int v_size) { |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
int i = 0; |
215
|
|
|
|
|
|
|
int j = v_size; |
216
|
|
|
|
|
|
|
|
217
|
8
|
|
|
|
|
|
int c = z->c; int l = z->l; |
218
|
32
|
|
|
|
|
|
symbol * q = z->p + c; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
const struct among * w; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
int common_i = 0; |
223
|
|
|
|
|
|
|
int common_j = 0; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
int first_key_inspected = 0; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
while(1) { |
228
|
32
|
|
|
|
|
|
int k = i + ((j - i) >> 1); |
229
|
|
|
|
|
|
|
int diff = 0; |
230
|
32
|
|
|
|
|
|
int common = common_i < common_j ? common_i : common_j; /* smaller */ |
231
|
32
|
|
|
|
|
|
w = v + k; |
232
|
|
|
|
|
|
|
{ |
233
|
72
|
100
|
|
|
|
|
int i2; for (i2 = common; i2 < w->s_size; i2++) { |
234
|
64
|
50
|
|
|
|
|
if (c + common == l) { diff = -1; break; } |
235
|
64
|
|
|
|
|
|
diff = q[common] - w->s[i2]; |
236
|
64
|
100
|
|
|
|
|
if (diff != 0) break; |
237
|
40
|
|
|
|
|
|
common++; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
32
|
50
|
|
|
|
|
if (diff < 0) { j = k; common_j = common; } |
241
|
|
|
|
|
|
|
else { i = k; common_i = common; } |
242
|
32
|
100
|
|
|
|
|
if (j - i <= 1) { |
243
|
8
|
50
|
|
|
|
|
if (i > 0) break; /* v->s has been inspected */ |
244
|
0
|
0
|
|
|
|
|
if (j == i) break; /* only one item in v */ |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
/* - but now we need to go round once more to get |
247
|
|
|
|
|
|
|
v->s inspected. This looks messy, but is actually |
248
|
|
|
|
|
|
|
the optimal approach. */ |
249
|
|
|
|
|
|
|
|
250
|
8
|
0
|
|
|
|
|
if (first_key_inspected) break; |
251
|
|
|
|
|
|
|
first_key_inspected = 1; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
while(1) { |
255
|
8
|
|
|
|
|
|
w = v + i; |
256
|
8
|
50
|
|
|
|
|
if (common_i >= w->s_size) { |
257
|
8
|
|
|
|
|
|
z->c = c + w->s_size; |
258
|
8
|
50
|
|
|
|
|
if (w->function == 0) return w->result; |
259
|
|
|
|
|
|
|
{ |
260
|
0
|
|
|
|
|
|
int res = w->function(z); |
261
|
0
|
|
|
|
|
|
z->c = c + w->s_size; |
262
|
0
|
0
|
|
|
|
|
if (res) return w->result; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
} |
265
|
0
|
|
|
|
|
|
i = w->substring_i; |
266
|
0
|
0
|
|
|
|
|
if (i < 0) return 0; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
/* find_among_b is for backwards processing. Same comments apply */ |
271
|
|
|
|
|
|
|
|
272
|
64
|
|
|
|
|
|
extern int find_among_b(struct SN_env * z, const struct among * v, int v_size) { |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
int i = 0; |
275
|
|
|
|
|
|
|
int j = v_size; |
276
|
|
|
|
|
|
|
|
277
|
64
|
|
|
|
|
|
int c = z->c; int lb = z->lb; |
278
|
416
|
|
|
|
|
|
symbol * q = z->p + c - 1; |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
const struct among * w; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
int common_i = 0; |
283
|
|
|
|
|
|
|
int common_j = 0; |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
int first_key_inspected = 0; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
while(1) { |
288
|
416
|
|
|
|
|
|
int k = i + ((j - i) >> 1); |
289
|
|
|
|
|
|
|
int diff = 0; |
290
|
416
|
|
|
|
|
|
int common = common_i < common_j ? common_i : common_j; |
291
|
416
|
|
|
|
|
|
w = v + k; |
292
|
|
|
|
|
|
|
{ |
293
|
648
|
100
|
|
|
|
|
int i2; for (i2 = w->s_size - 1 - common; i2 >= 0; i2--) { |
294
|
640
|
100
|
|
|
|
|
if (c - common == lb) { diff = -1; break; } |
295
|
632
|
|
|
|
|
|
diff = q[- common] - w->s[i2]; |
296
|
632
|
100
|
|
|
|
|
if (diff != 0) break; |
297
|
232
|
|
|
|
|
|
common++; |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
} |
300
|
416
|
100
|
|
|
|
|
if (diff < 0) { j = k; common_j = common; } |
301
|
|
|
|
|
|
|
else { i = k; common_i = common; } |
302
|
416
|
100
|
|
|
|
|
if (j - i <= 1) { |
303
|
72
|
100
|
|
|
|
|
if (i > 0) break; |
304
|
16
|
50
|
|
|
|
|
if (j == i) break; |
305
|
72
|
100
|
|
|
|
|
if (first_key_inspected) break; |
306
|
|
|
|
|
|
|
first_key_inspected = 1; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
while(1) { |
310
|
96
|
|
|
|
|
|
w = v + i; |
311
|
96
|
100
|
|
|
|
|
if (common_i >= w->s_size) { |
312
|
16
|
|
|
|
|
|
z->c = c - w->s_size; |
313
|
16
|
50
|
|
|
|
|
if (w->function == 0) return w->result; |
314
|
|
|
|
|
|
|
{ |
315
|
0
|
|
|
|
|
|
int res = w->function(z); |
316
|
0
|
|
|
|
|
|
z->c = c - w->s_size; |
317
|
0
|
0
|
|
|
|
|
if (res) return w->result; |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
} |
320
|
80
|
|
|
|
|
|
i = w->substring_i; |
321
|
80
|
100
|
|
|
|
|
if (i < 0) return 0; |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
/* Increase the size of the buffer pointed to by p to at least n symbols. |
327
|
|
|
|
|
|
|
* If insufficient memory, returns NULL and frees the old buffer. |
328
|
|
|
|
|
|
|
*/ |
329
|
1
|
|
|
|
|
|
static symbol * increase_size(symbol * p, int n) { |
330
|
|
|
|
|
|
|
symbol * q; |
331
|
1
|
|
|
|
|
|
int new_size = n + 20; |
332
|
1
|
|
|
|
|
|
void * mem = realloc((char *) p - HEAD, |
333
|
1
|
|
|
|
|
|
HEAD + (new_size + 1) * sizeof(symbol)); |
334
|
1
|
50
|
|
|
|
|
if (mem == NULL) { |
335
|
0
|
|
|
|
|
|
lose_s(p); |
336
|
0
|
|
|
|
|
|
return NULL; |
337
|
|
|
|
|
|
|
} |
338
|
1
|
|
|
|
|
|
q = (symbol *) (HEAD + (char *)mem); |
339
|
1
|
|
|
|
|
|
CAPACITY(q) = new_size; |
340
|
1
|
|
|
|
|
|
return q; |
341
|
|
|
|
|
|
|
} |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
/* to replace symbols between c_bra and c_ket in z->p by the |
344
|
|
|
|
|
|
|
s_size symbols at s. |
345
|
|
|
|
|
|
|
Returns 0 on success, -1 on error. |
346
|
|
|
|
|
|
|
Also, frees z->p (and sets it to NULL) on error. |
347
|
|
|
|
|
|
|
*/ |
348
|
40
|
|
|
|
|
|
extern int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s, int * adjptr) |
349
|
|
|
|
|
|
|
{ |
350
|
|
|
|
|
|
|
int adjustment; |
351
|
|
|
|
|
|
|
int len; |
352
|
40
|
50
|
|
|
|
|
if (z->p == NULL) { |
353
|
0
|
|
|
|
|
|
z->p = create_s(); |
354
|
0
|
0
|
|
|
|
|
if (z->p == NULL) return -1; |
355
|
|
|
|
|
|
|
} |
356
|
40
|
|
|
|
|
|
adjustment = s_size - (c_ket - c_bra); |
357
|
40
|
|
|
|
|
|
len = SIZE(z->p); |
358
|
40
|
50
|
|
|
|
|
if (adjustment != 0) { |
359
|
40
|
100
|
|
|
|
|
if (adjustment + len > CAPACITY(z->p)) { |
360
|
1
|
|
|
|
|
|
z->p = increase_size(z->p, adjustment + len); |
361
|
1
|
50
|
|
|
|
|
if (z->p == NULL) return -1; |
362
|
|
|
|
|
|
|
} |
363
|
80
|
|
|
|
|
|
memmove(z->p + c_ket + adjustment, |
364
|
40
|
|
|
|
|
|
z->p + c_ket, |
365
|
40
|
|
|
|
|
|
(len - c_ket) * sizeof(symbol)); |
366
|
40
|
|
|
|
|
|
SET_SIZE(z->p, adjustment + len); |
367
|
40
|
|
|
|
|
|
z->l += adjustment; |
368
|
40
|
100
|
|
|
|
|
if (z->c >= c_ket) |
369
|
9
|
|
|
|
|
|
z->c += adjustment; |
370
|
|
|
|
|
|
|
else |
371
|
31
|
50
|
|
|
|
|
if (z->c > c_bra) |
372
|
0
|
|
|
|
|
|
z->c = c_bra; |
373
|
|
|
|
|
|
|
} |
374
|
40
|
100
|
|
|
|
|
unless (s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol)); |
375
|
40
|
50
|
|
|
|
|
if (adjptr != NULL) |
376
|
0
|
|
|
|
|
|
*adjptr = adjustment; |
377
|
|
|
|
|
|
|
return 0; |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
static int slice_check(struct SN_env * z) { |
381
|
|
|
|
|
|
|
|
382
|
24
|
0
|
|
|
|
|
if (z->bra < 0 || |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
383
|
24
|
0
|
|
|
|
|
z->bra > z->ket || |
|
|
50
|
|
|
|
|
|
384
|
24
|
0
|
|
|
|
|
z->ket > z->l || |
|
|
50
|
|
|
|
|
|
385
|
24
|
0
|
|
|
|
|
z->p == NULL || |
|
|
50
|
|
|
|
|
|
386
|
24
|
|
|
|
|
|
z->l > SIZE(z->p)) /* this line could be removed */ |
387
|
|
|
|
|
|
|
{ |
388
|
|
|
|
|
|
|
#if 0 |
389
|
|
|
|
|
|
|
fprintf(stderr, "faulty slice operation:\n"); |
390
|
|
|
|
|
|
|
debug(z, -1, 0); |
391
|
|
|
|
|
|
|
#endif |
392
|
|
|
|
|
|
|
return -1; |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
return 0; |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
24
|
|
|
|
|
|
extern int slice_from_s(struct SN_env * z, int s_size, const symbol * s) { |
398
|
24
|
50
|
|
|
|
|
if (slice_check(z)) return -1; |
399
|
24
|
|
|
|
|
|
return replace_s(z, z->bra, z->ket, s_size, s, NULL); |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
0
|
|
|
|
|
|
extern int slice_from_v(struct SN_env * z, const symbol * p) { |
403
|
0
|
|
|
|
|
|
return slice_from_s(z, SIZE(p), p); |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
16
|
|
|
|
|
|
extern int slice_del(struct SN_env * z) { |
407
|
16
|
|
|
|
|
|
return slice_from_s(z, 0, 0); |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
|
410
|
0
|
|
|
|
|
|
extern int insert_s(struct SN_env * z, int bra, int ket, int s_size, const symbol * s) { |
411
|
|
|
|
|
|
|
int adjustment; |
412
|
0
|
0
|
|
|
|
|
if (replace_s(z, bra, ket, s_size, s, &adjustment)) |
413
|
|
|
|
|
|
|
return -1; |
414
|
0
|
0
|
|
|
|
|
if (bra <= z->bra) z->bra += adjustment; |
415
|
0
|
0
|
|
|
|
|
if (bra <= z->ket) z->ket += adjustment; |
416
|
|
|
|
|
|
|
return 0; |
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
|
419
|
0
|
|
|
|
|
|
extern int insert_v(struct SN_env * z, int bra, int ket, const symbol * p) { |
420
|
|
|
|
|
|
|
int adjustment; |
421
|
0
|
0
|
|
|
|
|
if (replace_s(z, bra, ket, SIZE(p), p, &adjustment)) |
422
|
|
|
|
|
|
|
return -1; |
423
|
0
|
0
|
|
|
|
|
if (bra <= z->bra) z->bra += adjustment; |
424
|
0
|
0
|
|
|
|
|
if (bra <= z->ket) z->ket += adjustment; |
425
|
|
|
|
|
|
|
return 0; |
426
|
|
|
|
|
|
|
} |
427
|
|
|
|
|
|
|
|
428
|
0
|
|
|
|
|
|
extern symbol * slice_to(struct SN_env * z, symbol * p) { |
429
|
0
|
0
|
|
|
|
|
if (slice_check(z)) { |
430
|
0
|
|
|
|
|
|
lose_s(p); |
431
|
0
|
|
|
|
|
|
return NULL; |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
{ |
434
|
0
|
|
|
|
|
|
int len = z->ket - z->bra; |
435
|
0
|
0
|
|
|
|
|
if (CAPACITY(p) < len) { |
436
|
0
|
|
|
|
|
|
p = increase_size(p, len); |
437
|
0
|
0
|
|
|
|
|
if (p == NULL) |
438
|
|
|
|
|
|
|
return NULL; |
439
|
|
|
|
|
|
|
} |
440
|
0
|
|
|
|
|
|
memmove(p, z->p + z->bra, len * sizeof(symbol)); |
441
|
0
|
|
|
|
|
|
SET_SIZE(p, len); |
442
|
|
|
|
|
|
|
} |
443
|
0
|
|
|
|
|
|
return p; |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
0
|
|
|
|
|
|
extern symbol * assign_to(struct SN_env * z, symbol * p) { |
447
|
0
|
|
|
|
|
|
int len = z->l; |
448
|
0
|
0
|
|
|
|
|
if (CAPACITY(p) < len) { |
449
|
0
|
|
|
|
|
|
p = increase_size(p, len); |
450
|
0
|
0
|
|
|
|
|
if (p == NULL) |
451
|
|
|
|
|
|
|
return NULL; |
452
|
|
|
|
|
|
|
} |
453
|
0
|
|
|
|
|
|
memmove(p, z->p, len * sizeof(symbol)); |
454
|
0
|
|
|
|
|
|
SET_SIZE(p, len); |
455
|
0
|
|
|
|
|
|
return p; |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
#if 0 |
459
|
|
|
|
|
|
|
extern void debug(struct SN_env * z, int number, int line_count) { |
460
|
|
|
|
|
|
|
int i; |
461
|
|
|
|
|
|
|
int limit = SIZE(z->p); |
462
|
|
|
|
|
|
|
/*if (number >= 0) printf("%3d (line %4d): '", number, line_count);*/ |
463
|
|
|
|
|
|
|
if (number >= 0) printf("%3d (line %4d): [%d]'", number, line_count,limit); |
464
|
|
|
|
|
|
|
for (i = 0; i <= limit; i++) { |
465
|
|
|
|
|
|
|
if (z->lb == i) printf("{"); |
466
|
|
|
|
|
|
|
if (z->bra == i) printf("["); |
467
|
|
|
|
|
|
|
if (z->c == i) printf("|"); |
468
|
|
|
|
|
|
|
if (z->ket == i) printf("]"); |
469
|
|
|
|
|
|
|
if (z->l == i) printf("}"); |
470
|
|
|
|
|
|
|
if (i < limit) |
471
|
|
|
|
|
|
|
{ int ch = z->p[i]; |
472
|
|
|
|
|
|
|
if (ch == 0) ch = '#'; |
473
|
|
|
|
|
|
|
printf("%c", ch); |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
printf("'\n"); |
477
|
|
|
|
|
|
|
} |
478
|
|
|
|
|
|
|
#endif |