Method: U::String#split

Defined in:
ext/u/rb_u_string_split.c

#split(pattern = $;, limit = 0) ⇒ Array<U::String>

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.

Parameters:

  • pattern (Regexp, #to_str) (defaults to: $;)
  • limit (#to_int) (defaults to: 0)

Returns:



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
# File 'ext/u/rb_u_string_split.c', line 200

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);
}