This repository has been archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbsplit.cpp
150 lines (138 loc) · 3.23 KB
/
bsplit.cpp
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
//
// bsplit.cc
// Split front-end
////////////////////////////////////////////////////////////////////////////////
// 1999.11.24 update by Tatsuo Baba
//
// You may distribute under the terms of either the GNU General Public
// License or the Artistic License, as specified in the perl_license.txt file.
////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define KANJI
#include "global.h"
#include "sv.h"
#include "intreg.h"
int split(regexp* rx,char *target,char *targetendp,
int limit,char *msg)
{
char *orig,*m;
char *s = target;
int len = targetendp - target;
if (len < 1)
return -1;
char *strend = s + len;
int maxiters = (strend - s) + 10;
int iters = 0;
orig = m = s;
rx->splitctr = 0; // split counter
// rx->prelen = 0 means split each characters
// and limit is 1 returns all string
if (!rx->prelen || limit == 1) {
int blen = 2*len + 3;
if (limit == 1)
blen = 5;
char **buf = (char**)new char[blen * sizeof(char*)];
int copycnt = 0;
if (buf == NULL) {
strcpy(msg,"out of space buf");
return -1;
}
int kanjiflag = rx->pmflags & PMf_KANJI;
while (s < strend) {
if (--limit == 0) {
buf[copycnt++] = s;
buf[copycnt++] = strend;
break;
}
if (kanjiflag && iskanji(*s)) {
buf[copycnt++] = s;
s += 2;
buf[copycnt++] = s;
} else {
buf[copycnt++] = s++;
buf[copycnt++] = s;
}
}
if (copycnt) {
rx->splitctr = copycnt / 2; // split counter
buf[copycnt] = NULL; // set stopper
buf[copycnt+1] = NULL; // set stopper
rx->splitp = buf;
}
else
delete [] buf ;
return rx->splitctr;
}
// now ready
int blen = 256; // initial size
char **buf = (char**)new char[blen * sizeof(char*)];
int copycnt = 0;
if (buf == NULL) {
strcpy(msg,"out of space buf");
return -1;
}
if (!bregexec(rx, s, strend, orig, 0,1,msg)) { // no split ?
buf[0] = target;
buf[1] = targetendp;
rx->splitctr = 1; // split counter
buf[2] = NULL; // set stopper
buf[3] = NULL; // set stopper
rx->splitp = buf;
return 1;
}
// now ready to go
limit--;
do {
if (iters++ > maxiters) {
delete [] buf;
strcpy(msg,"Split loop");
return -1;
}
m = rx->startp[0];
len = m - s;
if (blen <= copycnt + 3) {
char *tp = new char[(blen + 256)*sizeof(char*)];
if (tp == NULL) {
strcpy(msg,"out of space buf");
delete [] buf;
return -1;
}
memcpy(tp,buf,copycnt*sizeof(char*));
delete [] buf;
buf = (char**)tp; blen += 256;
}
buf[copycnt++] = s;
buf[copycnt++] = s+ len;
s = rx->endp[0];
if (--limit == 0)
break;
} while (bregexec(rx, s, strend, orig, s == m, 1,msg));
len = rx->subend - s;
if (blen <= copycnt + 3) {
char *tp = new char[(blen + 3)*sizeof(char*)];
if (tp == NULL) {
strcpy(msg,"out of space buf");
delete [] buf;
return -1;
}
memcpy(tp,buf,copycnt*sizeof(char*));
delete [] buf;
buf = (char**)tp;
}
if (len) {
buf[copycnt++] = s;
buf[copycnt++] = s+ len;
}
if (copycnt) {
rx->splitctr = copycnt / 2; // split counter
buf[copycnt] = NULL; // set stopper
buf[copycnt+1] = NULL; // set stopper
rx->splitp = buf;
}
else
delete [] buf ;
return rx->splitctr;
}