/********************************************
copyright 1991, 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 2, 1991.
********************************************/
* Revision 5.1 91/12/05 07:56:31 brennan
#define TEMPBUFF_GOES_HERE
SPLIT_OV
*split_ov_list
;
static int PROTO(re_ov_split
, (char *, PTR
) ) ;
static int PROTO(space_ov_split
, (char *, char *) ) ;
/* split string s of length slen on SPACE without changing s.
load the pieces into STRINGS and ptrs into
return the number of pieces */
int space_split( s
, slen
)
{ char *back
= s
+ slen
;
#define EAT_SPACE() while ( scan_code[*(unsigned char*)s] ==\
#define EAT_NON_SPACE() \
*back = ' ' ; /* sentinel */\
while ( scan_code[*(unsigned char*)s] != SC_SPACE ) s++ ;\
if ( *s
== 0 ) goto done
;
/* mark the front with q */
sval
= split_buff
[i
++] = new_STRING((char *) 0, len
= s
- q
) ;
(void) memcpy(sval
->str
, q
, SIZE_T(len
)) ;
if ( *s
== 0 ) goto done
;
sval
= split_buff
[i
++] = new_STRING((char *) 0, len
= s
- q
) ;
(void) memcpy(sval
->str
, q
, SIZE_T(len
)) ;
if ( *s
== 0 ) goto done
;
sval
= split_buff
[i
++] = new_STRING((char *) 0, len
= s
- q
) ;
(void) memcpy(sval
->str
, q
, SIZE_T(len
)) ;
return i
+ space_ov_split(s
, back
) ;
static int space_ov_split(s
, back
)
register SPLIT_OV
*tail
= &dummy
;
if ( *s
== 0 ) break ; /* done */
tail
= tail
->link
= (SPLIT_OV
*) zmalloc(sizeof(SPLIT_OV
)) ;
tail
->sval
= new_STRING((char *) 0 , len
= s
-q
) ;
(void) memcpy(tail
->sval
->str
, q
, SIZE_T(len
)) ;
tail
->link
= (SPLIT_OV
*) 0 ;
split_ov_list
= dummy
.link
;
char *re_pos_match(s
, re
, lenp
)
PTR re
; unsigned *lenp
;
while ( s
= REmatch(s
, re
, lenp
) )
int lcnt
= MAX_SPLIT
/ 3 ;
if ( !(t
= re_pos_match(s
, re
, &mlen
)) ) goto done
;
sval
= split_buff
[i
++] = new_STRING( (char *)0, len
= t
-s
) ;
(void) memcpy(sval
->str
, s
, SIZE_T(len
)) ;
if ( !(t
= re_pos_match(s
, re
, &mlen
)) ) goto done
;
sval
= split_buff
[i
++] = new_STRING( (char *)0, len
= t
-s
) ;
(void) memcpy(sval
->str
, s
, SIZE_T(len
)) ;
if ( !(t
= re_pos_match(s
, re
, &mlen
)) ) goto done
;
sval
= split_buff
[i
++] = new_STRING( (char *)0, len
= t
-s
) ;
(void) memcpy(sval
->str
, s
, SIZE_T(len
)) ;
return i
+ re_ov_split(s
, re
) ;
split_buff
[i
++] = new_STRING(s
) ;
we've overflowed split_buff[] , put
the rest on the split_ov_list
static int re_ov_split(s
, re
)
register SPLIT_OV
*tail
= &dummy
;
while ( t
= re_pos_match(s
, re
, &mlen
) )
tail
= tail
->link
= (SPLIT_OV
*) zmalloc(sizeof(SPLIT_OV
)) ;
tail
->sval
= new_STRING( (char *)0, len
= t
-s
) ;
(void) memcpy(tail
->sval
->str
, s
, SIZE_T(len
)) ;
tail
= tail
->link
= (SPLIT_OV
*) zmalloc(sizeof(SPLIT_OV
)) ;
tail
->sval
= new_STRING(s
) ;
tail
->link
= (SPLIT_OV
*) 0 ;
split_ov_list
= dummy
.link
;
split s into array X on r
int cnt
; /* the number of pieces */
if ( sp
->type
< C_RE
) cast_for_split(sp
) ;
/* can be C_RE, C_SPACE or C_SNULL */
if ( sp
->type
< C_STRING
) cast1_to_s(sp
) ;
if ( string(sp
)->len
== 0 ) /* nothing to split */
{ free_STRING( string(sp
) ) ;
sp
->type
= C_DOUBLE
; sp
->dval
= 0.0 ;
cnt
= re_split(string(sp
)->str
, (sp
+2)->ptr
) ;
cnt
= space_split(string(sp
)->str
, string(sp
)->len
) ;
/* this case could be done by C_RE, but very slowly.
Since it is the common way to eliminate fields,
we'll treat the special case for speed */
case C_SNULL
: /* split on empty string */
split_buff
[0] = (STRING
*) sp
->ptr
;
default : bozo("bad splitting cell in bi_split") ;
free_STRING( string(sp
) ) ;
sp
->dval
= (double) cnt
;
load_array((ARRAY
)(sp
+1)->ptr
, cnt
) ;