/*
* call-seq:
* str.partition(sep) => [head, sep, tail]
*
* Searches the string for <i>sep</i> and returns the part before it,
* the <i>sep</i>, and the part after it. If <i>sep</i> is not
* found, returns <i>str</i> and two empty strings. If no argument
* is given, Enumerable#partition is called.
*
* "hello".partition("l") #=> ["he", "l", "lo"]
* "hello".partition("x") #=> ["hello", "", ""]
*/
static VALUE
rb_str_partition(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE sep;
long pos;
if (argc == 0) return rb_call_super(argc, argv);
rb_scan_args(argc, argv, "1", &sep);
if (TYPE(sep) != T_REGEXP) {
VALUE tmp;
tmp = rb_check_string_type(sep);
if (NIL_P(tmp)) {
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sep));
}
sep = get_arg_pat(tmp);
}
pos = rb_reg_search(sep, str, 0, 0);
if (pos < 0) {
failed:
return rb_ary_new3(3, str, rb_str_new(0,0),rb_str_new(0,0));
}
sep = rb_str_subpat(str, sep, 0);
if (pos == 0 && RSTRING(sep)->len == 0) goto failed;
return rb_ary_new3(3, rb_str_substr(str, 0, pos),
sep,
rb_str_substr(str, pos+RSTRING(sep)->len,
RSTRING(str)->len-pos-RSTRING(sep)->len));
}