-
Notifications
You must be signed in to change notification settings - Fork 0
/
rb_u_string_split.c
245 lines (207 loc) · 9.03 KB
/
rb_u_string_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
#include "rb_includes.h"
#include "rb_u_re.h"
static VALUE
rb_u_string_split_trim(VALUE result, bool limit_given, int limit)
{
if (limit_given || limit != 0)
return result;
long length;
while ((length = RARRAY_LEN(result)) > 0 &&
USTRING_LENGTH(RVAL2USTRING(RARRAY_PTR(result)[length - 1])) == 0)
rb_ary_pop(result);
return result;
}
static VALUE
rb_u_string_split_rest(VALUE self, long offset, bool limit_given, int limit, VALUE result)
{
const struct rb_u_string *string = RVAL2USTRING(self);
long length = USTRING_LENGTH(string);
if (length > 0 && (limit_given || length > offset || limit < 0))
rb_ary_push(result,
length == offset ?
rb_u_string_new_empty(self) :
rb_u_string_new_subsequence(self,
offset,
length - offset));
return rb_u_string_split_trim(result, limit_given, limit);
}
static VALUE
rb_u_string_split_awk(VALUE self, bool limit_given, int limit)
{
VALUE result = rb_ary_new();
const struct rb_u_string *string = RVAL2USTRING(self);
const char *begin = USTRING_STR(string);
const char *p = begin;
const char *end = USTRING_END(string);
int i = 1;
while (p < end) {
const char *q;
while (p < end && u_char_isspace(u_decode(&q, p, end)))
p = q;
if (p == end || (limit_given && i >= limit))
break;
i++;
q = p;
const char *r;
while (q < end && !u_char_isspace(u_decode(&r, q, end)))
q = r;
rb_ary_push(result,
rb_u_string_new_subsequence(self,
p - begin,
q - p));
p = q;
}
return rb_u_string_split_rest(self, p - begin, limit_given, limit, result);
}
static VALUE
rb_u_string_split_string(VALUE self, VALUE rbseparator, bool limit_given, int limit)
{
const struct rb_u_string *string = RVAL2USTRING(self);
const struct rb_u_string *separator = RVAL2USTRING_ANY(rbseparator);
const char *begin = USTRING_STR(string);
const char *p = begin;
const char *end = USTRING_END(string);
const char *s_p = USTRING_STR(separator);
long s_len = USTRING_LENGTH(separator);
rb_u_validate(p, USTRING_LENGTH(string));
rb_u_validate(s_p, s_len);
VALUE result = rb_ary_new();
/* TODO: Better variable name. */
long offset;
for (int i = 1; (!limit_given || i < limit) && p < end; i++) {
if ((offset = rb_u_memsearch(s_p, s_len, p, end - p)) < 0)
break;
rb_ary_push(result, rb_u_string_new_subsequence(self, p - begin, offset));
p += offset + s_len;
}
return rb_u_string_split_rest(self, p - begin, limit_given, limit, result);
}
static void
rb_u_string_split_pattern_push_registers(VALUE self,
struct re_registers *registers,
VALUE result)
{
for (int i = 1; i < registers->num_regs; i++) {
if (registers->beg[i] == -1)
continue;
rb_ary_push(result,
registers->beg[i] == registers->end[i] ?
rb_u_string_new_empty(self) :
rb_u_string_new_subsequence(self,
registers->beg[i],
registers->end[i] - registers->beg[i]));
}
}
static VALUE
rb_u_string_split_pattern(VALUE self, VALUE pattern, bool limit_given, int limit)
{
VALUE str = rb_str_to_str(self);
const char *begin = RSTRING_PTR(str);
const char *p = begin;
const char *end = RSTRING_END(str);
VALUE result = rb_ary_new();
bool last_was_empty = false;
long start = 0;
/* TODO: Better variable name. */
long offset;
int i = 1;
while ((offset = rb_reg_search(pattern, str, start, 0)) >= 0) {
struct re_registers *registers = RMATCH_REGS(rb_backref_get());
if (start == offset && registers->beg[0] == registers->end[0]) {
if (begin == NULL) {
rb_ary_push(result, rb_u_string_new_empty(self));
break;
} else if (last_was_empty) {
const char *q;
u_decode(&q, p, end);
rb_ary_push(result,
rb_u_string_new_subsequence(self,
p - begin,
q - p));
} else {
if (begin + start == end)
start++;
else {
const char *q;
u_decode(&q, p, end);
start += q - p;
}
last_was_empty = true;
continue;
}
} else {
rb_ary_push(result,
rb_u_string_new_subsequence(self,
p - begin,
offset - (p - begin)));
start = registers->end[0];
}
last_was_empty = false;
p = begin + start;
rb_u_string_split_pattern_push_registers(self, registers, result);
i++;
if (limit_given && i == limit)
break;
}
return rb_u_string_split_rest(self, p - begin, limit_given, limit, result);
}
/* @overload split(pattern = $;, limit = 0)
*
* Returns the receiver split into LIMIT substrings separated by PATTERN,
* each inheriting any taint and untrust.
*
* If PATTERN = `$;` = nil or PATTERN = `' '`, splits according to AWK rules,
* that is, any {#space?} prefix is skipped, then substrings are separated by
* non-empty {#space?} substrings.
*
* If LIMIT < 0, then no limit is imposed and trailing {#empty?} substrings
* aren’t removed.
*
* If LIMIT = 0, then no limit is imposed and trailing {#empty?} substrings
* are removed.
*
* If LIMIT = 1, then, if {#length} = 0, the result will be empty, otherwise
* it will consist of the receiver only.
*
* If LIMIT > 1, then the receiver is split into at most LIMIT substrings.
*
* @param [Regexp, #to_str] pattern
* @param [#to_int] limit
* @return [Array<U::String>] */
VALUE
rb_u_string_split_m(int argc, VALUE *argv, VALUE self)
{
VALUE rbpattern, rblimit;
int limit = 0;
bool limit_given;
if (rb_scan_args(argc, argv, "02", &rbpattern, &rblimit) == 2)
limit = NUM2INT(rblimit);
const struct rb_u_string *string = RVAL2USTRING(self);
if (limit == 1) {
if (USTRING_LENGTH(string) == 0)
return rb_ary_new2(0);
return rb_ary_new3(1, self);
}
limit_given = !NIL_P(rblimit) && limit >= 0;
if (NIL_P(rbpattern) && NIL_P(rb_fs))
return rb_u_string_split_awk(self, limit_given, limit);
else if (NIL_P(rbpattern))
rbpattern = rb_fs;
if (TYPE(rbpattern) != T_STRING && !RTEST(rb_obj_is_kind_of(rbpattern, rb_cUString)))
return rb_u_string_split_pattern(self,
rb_u_pattern_argument(rbpattern, true),
limit_given,
limit);
const struct rb_u_string *pattern = RVAL2USTRING_ANY(rbpattern);
const char *p = USTRING_STR(pattern);
long length = USTRING_LENGTH(pattern);
if (length == 0)
return rb_u_string_split_pattern(self,
rb_reg_regcomp(rb_str_to_str(rbpattern)),
limit_given,
limit);
else if (length == 1 && *p == ' ')
return rb_u_string_split_awk(self, limit_given, limit);
else
return rb_u_string_split_string(self, rbpattern, limit_given, limit);
}