-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.c
324 lines (287 loc) · 8.34 KB
/
split.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/********************************************
split.c
copyright 1991-1996,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
/* split.c */
#include "mawk.h"
#include "split.h"
#include "table.h"
#include "bi_vars.h"
#include "bi_funct.h"
#include "types_string.h"
#include "scan.h"
#include "regexp.h"
#include "repl.h"
#include "field.h"
#ifdef MEM_CHECK
#define SP_SIZE 4 /* exercises split_block_list code */
#else
#define SP_SIZE 2048
#endif
typedef struct split_block {
STRING * strings[SP_SIZE];
struct split_block * link;
} Split_Block_Node;
static Split_Block_Node split_block_base;
static Split_Block_Node * split_block_list = &split_block_base;
/* usually the list is of size 1
the list never gets smaller than size 1
this function returns a bigger list to size 1
*/
static void
spb_list_shrink( void )
{
Split_Block_Node * p = split_block_list->link;
split_block_list->link = 0;
while ( p ) {
Split_Block_Node * hold = p;
p = p->link;
free( hold );
}
}
/* this function is passed a pointer to the tail of the list,
adds a new node and returns the new tail
This makes the list one node bigger
*/
static Split_Block_Node *
grow_sp_list( Split_Block_Node * tail )
{
tail->link = (Split_Block_Node *)emalloc( sizeof( Split_Block_Node ) );
tail = tail->link;
tail->link = 0;
return tail;
}
/*
* Split string s of length slen on SPACE without changing s.
* Load the pieces into STRINGS
* return the number of pieces
*/
size_t
space_split( const char * s, size_t slen )
{
size_t cnt = 0;
const char * end = s + slen;
Split_Block_Node * node_p = split_block_list;
unsigned idx = 0;
while ( 1 ) {
/* eat space */
while ( scan_code_get(*(const unsigned char *)s) == SC_SPACE ) {
s++;
}
if ( s == end ) {
return cnt;
}
/* find one field */
{
const char * q = s++; /* q is front of field */
while ( s < end && scan_code_get(*(const unsigned char *)s) != SC_SPACE )
s++;
/* create and store the string field */
node_p->strings[idx] = new_STRING2( q, s - q );
cnt++;
if ( ++idx == SP_SIZE ) {
idx = 0;
node_p = grow_sp_list( node_p );
}
}
}
/* not reached */
}
size_t
re_split( const char * s, size_t slen, void * re )
{
size_t cnt = 0;
const char * end = s + slen;
Split_Block_Node * node_p = split_block_list;
unsigned idx = 0;
int no_front_match = 0;
if ( slen == 0 ) {
return 0;
}
while ( s < end ) {
size_t mlen;
const char * m = re_pos_match( s, end - s, re, &mlen, no_front_match );
no_front_match = 1; /* future matches don't match ^ */
if ( m ) {
/* stuff in front of match is a field, might have length zero */
node_p->strings[idx] = new_STRING2( s, m - s );
cnt++;
if ( ++idx == SP_SIZE ) {
idx = 0;
node_p = grow_sp_list( node_p );
}
s = m + mlen;
}
else {
/* no match so last field is what's left */
node_p->strings[idx] = new_STRING2( s, end - s );
/* done so don't need to increment idx */
return ++cnt;
}
}
/* last match at end of s, so last field is "" */
node_p->strings[idx] = new_STRING0( 0 );
return ++cnt;
}
/* match a string with a regular expression, but
* only matches of positive length count
* input a string str and its length
* return is match point else 0 if no match
* length of match is returned in *lenp
*
* no_front_match -- hook for str being in middle of a bigger string
*/
char *
re_pos_match(
const char * str, size_t str_len, void * re, size_t * lenp, int no_front_match)
{
const char * end = str + str_len;
while ( str < end ) {
char * match = REmatch( str, end - str, re, lenp, no_front_match );
if ( match ) {
if ( *lenp ) {
/* match of positive length so done */
return match;
}
else {
/* match but zero length, move str forward and try again */
/* note this match must have occured at front of str */
str = match + 1;
no_front_match = 1;
}
}
else {
/* no match */
*lenp = 0;
return 0;
}
}
*lenp = 0;
return 0;
}
/* like space split but splits s into single character strings */
size_t
null_split( const char * s, size_t slen )
{
const char * end = s + slen;
Split_Block_Node * node_p = split_block_list;
unsigned idx = 0;
while ( s < end ) {
node_p->strings[idx] = new_STRING2( s++, 1 );
if ( ++idx == SP_SIZE ) {
idx = 0;
node_p = grow_sp_list( node_p );
}
}
return slen;
}
/* The caller knows there are cnt STRING* in the split_block_list
* buffers. This function uses them to make CELLs in cp[]
* The target CELLs are virgin, they don't need to be
* destroyed
*
*/
void
transfer_to_array( CELL cp[], size_t cnt )
{
Split_Block_Node * node_p = split_block_list;
unsigned idx = 0;
while ( cnt > 0 ) {
cp->type = C_MBSTRN;
cp->ptr = (void *)node_p->strings[idx];
cnt--;
cp++;
if ( ++idx == SP_SIZE ) {
idx = 0;
node_p = node_p->link;
}
}
if ( node_p != split_block_list )
spb_list_shrink();
}
/* like above but transfers the saved pieces to $1, $2 ... $cnt
* The target CELLs may be string type so need to be destroyed
* The caller has made sure the target CELLs exist
*
*/
void
transfer_to_fields( size_t cnt )
{
CELL * fp = &field[1]; /* start with $1 */
CELL * fp_end = &field[FBANK_SZ];
Split_Block_Node * node_p = split_block_list;
unsigned idx = 0;
unsigned fb_idx = 0;
while ( cnt > 0 ) {
cell_destroy( fp );
fp->type = C_MBSTRN;
fp->ptr = (void *)node_p->strings[idx];
cnt--;
if ( ++idx == SP_SIZE ) {
idx = 0;
node_p = node_p->link;
}
if ( ++fp == fp_end ) {
fb_idx++;
fp = &fbankv[fb_idx][0];
fp_end = fp + FBANK_SZ;
}
}
if ( node_p != split_block_list ) {
spb_list_shrink();
}
}
/*
* split(s, X, r)
* split s into array X on r
*
* mawk state is EXECUTION sp points at top of eval_stack[]
*
* entry: sp[0] holds r
* sp[-1] pts at X
* sp[-2] holds s
*
exit : sp is 2 less, sp[0] is C_DOUBLE CELL with value equal
to the number of split pieces
*/
CELL *
bi_split( CELL * sp )
{
size_t cnt = 0; /* the number of pieces */
if ( sp->type < C_RE )
cast_for_split( sp );
/* can be C_RE, C_SPACE or C_SNULL */
sp -= 2;
if ( sp->type < C_STRING )
cast1_to_s( sp );
if ( string( sp )->len == 0 ) { /* nothing to split */
cnt = 0;
}
else {
switch ( ( sp + 2 )->type ) {
case C_RE:
cnt = re_split( string( sp )->str, string( sp )->len,
( sp + 2 )->ptr );
break;
case C_SPACE:
cnt = space_split( string( sp )->str, string( sp )->len );
break;
case C_SNULL: /* split on empty string */
cnt = null_split( string( sp )->str, string( sp )->len );
break;
default:
bozo( "bad splitting cell in bi_split" );
}
}
free_STRING( string( sp ) );
sp->type = C_DOUBLE;
sp->dval = (double)cnt;
array_load( ( ARRAY )( sp + 1 )->ptr, cnt );
return sp;
}