Beispiel #1
0
VALUE
rb_str_eat( int argc, VALUE *argv, VALUE str)
{
    VALUE val;
    int n;
    int l;
    int r;

#ifdef HAVE_HEADER_RUBY_H
    n = l = RSTRING_LEN( str);
#else
    n = l = rb_str_strlen( str);
#endif
    if (rb_scan_args( argc, argv, "01", &val) == 1) {
        if (!NIL_P( val)) {
            int v = NUM2INT( val);
            if (v >= 0) {
                if (n >= v) n = v;
            } else {
                n = -n;
                if (n <= v) n = v;
            }
        }
    }
    rb_str_modify( str);
#ifdef HAVE_HEADER_RUBY_H
    if (n > 0) {
        r = l - n;
        val = rb_str_new5( str, RSTRING_PTR( str), n);
        memmove( RSTRING_PTR( str), RSTRING_PTR( str) + n, r);
    } else {
        r = l + n;
        val = rb_str_new5( str, RSTRING_PTR( str) + r, -n);
    }
    RSTRING_LEN( str) = r;
    OBJ_INFECT( val, str);
#else
    if (n > 0) {
        r = 0;
    } else if (n < 0) {
        r = l + n;
        n = -n;
    } else
        return Qnil;
    val = rb_str_substr( str, r, n);
    if (!NIL_P(val))
        rb_str_update( str, r, n, rb_str_new( NULL, 0));
#endif
    return val;
}
Beispiel #2
0
VALUE string_spec_rb_str_new5(VALUE self, VALUE str, VALUE ptr, VALUE len) {
  return rb_str_new5(str, RSTRING_PTR(ptr), FIX2INT(len));
}