/*
* call-seq:
* str.rpartition(sep) => [head, sep, tail]
*
* Searches <i>sep</i> in the string from the end of the string, and
* returns the part before it, the <i>sep</i>, and the part after it.
* If <i>sep</i> is not found, returns two empty strings and
* <i>str</i>.
*
* "hello".rpartition("l") #=> ["hel", "l", "o"]
* "hello".rpartition("x") #=> ["", "", "hello"]
*/
static VALUE
rb_str_rpartition(str, sep)
VALUE str;
VALUE sep;
{
long pos = RSTRING(str)->len;
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, pos, 1);
if (pos < 0) {
return rb_ary_new3(3, rb_str_new(0,0),rb_str_new(0,0), str);
}
sep = rb_reg_nth_match(0, rb_backref_get());
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));
}