VALUE rb_str_ends_with_p( VALUE str, VALUE oth) { long i; char *s, *o; VALUE ost; #ifdef HAVE_HEADER_RUBY_H #else if (!rb_str_comparable( str, oth)) return Qnil; #endif ost = rb_string_value( &oth); i = RSTRING_LEN( ost); if (i > RSTRING_LEN( str)) return Qnil; s = RSTRING_END( str); o = RSTRING_END( ost); for (; i; i--) if (*--s != *--o) return Qnil; #ifdef HAVE_HEADER_RUBY_H return INT2FIX( RSTRING_LEN( str) - RSTRING_LEN( ost)); #else return INT2FIX( rb_str_strlen( str) - rb_str_strlen( ost)); #endif }
VALUE rb_str_starts_with_p( VALUE str, VALUE oth) { long i; char *s, *o; VALUE ost; #ifdef HAVE_HEADER_RUBY_H #else if (!rb_str_comparable( str, oth)) return Qnil; #endif ost = rb_string_value( &oth); i = RSTRING_LEN( ost); if (i > RSTRING_LEN( str)) return Qnil; s = RSTRING_PTR( str); o = RSTRING_PTR( ost); for (; i; i--, s++, o++) { if (*s != *o) return Qnil; } #ifdef HAVE_HEADER_RUBY_H return INT2FIX( RSTRING_LEN( ost)); #else return INT2FIX( rb_str_strlen( ost)); #endif }
VALUE rb_str_cut_bang( VALUE str, VALUE len) { int l; #ifdef HAVE_HEADER_RUBY_H #else int n; #endif rb_str_modify( str); l = NUM2INT( len); if (l < 0) l = 0; #ifdef HAVE_HEADER_RUBY_H if (l < RSTRING_LEN( str)) { RSTRING_LEN( str) = l; return str; } #else n = rb_str_strlen( str); if (l < n) { rb_str_update( str, l, n - l, rb_str_new( NULL, 0)); return str; } #endif return Qnil; }
VALUE rb_str_axe( int argc, VALUE *argv, VALUE str) { VALUE n; VALUE ret; long newlen, oldlen; if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n)) newlen = NUM2LONG( n); else newlen = 80; if (newlen < 0) return Qnil; #ifdef HAVE_HEADER_RUBY_H oldlen = RSTRING_LEN( str); #else oldlen = rb_str_strlen( str); #endif if (newlen < oldlen) { VALUE ell; long e; ell = rb_str_new2( "..."); #ifdef HAVE_HEADER_RUBY_H e = RSTRING_LEN( ell); #else e = rb_str_strlen( ell); #endif if (newlen > e) { ret = rb_str_substr( str, 0, newlen - e); rb_str_append( ret, ell); } else ret = rb_str_substr( str, 0, newlen); OBJ_INFECT( ret, str); } else ret = str; return ret; }
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; }
VALUE rb_str_rest( int argc, VALUE *argv, VALUE str) { VALUE n; long l, beg, len; beg = rb_scan_args( argc, argv, "01", &n) == 1 ? NUM2LONG( n) : 1; if (beg < 0) beg = 0; #ifdef HAVE_HEADER_RUBY_H l = RSTRING_LEN( str); #else l = rb_str_strlen( str); #endif return rb_str_substr( str, beg, l - beg); }
VALUE rb_str_tail( int argc, VALUE *argv, VALUE str) { VALUE n; long l, beg, len; len = rb_scan_args( argc, argv, "01", &n) == 1 ? NUM2LONG( n) : 1; #ifdef HAVE_HEADER_RUBY_H l = RSTRING_LEN( str); #else l = rb_str_strlen( str); #endif beg = l - len; if (beg < 0) beg = 0, len = l; return rb_str_substr( str, beg, len); }
static VALUE apply_actions(VALUE field, VALUE actions) { long j, actions_len = RARRAY_LEN(actions); long beg, len; VALUE num = 0, modi = 0; for (j = 0; j < actions_len; j++) { VALUE action = rb_ary_entry(actions, j); VALUE klass = rb_class_of(action); if (klass == rb_cRange) { /* copied from rb_str_aref */ len = rb_str_strlen(field); if (RTEST(rb_range_beg_len(action, &beg, &len, len, 0))) field = rb_str_substr(field, beg, len); } else if (klass == rb_cArray) { num = rb_str_to_inum(field, 10, 0); modi = rb_ary_entry(action, 1); if ( (FIXNUM_P(num) || TYPE(num) == T_BIGNUM && RBIGNUM_LEN(num) <= (SIZEOF_LONG/SIZEOF_BDIGITS) ) && FIXNUM_P(modi) && FIX2LONG(modi)) { long modl = NUM2LONG(modi); long numl = (NUM2LONG(num) / modl) * modl; char buf[30]; int wrtn = snprintf(buf, 30, RSTRING_PTR(rb_ary_entry(action, 0)), numl); if (wrtn < 30) { field = rb_str_new(buf, wrtn); continue; } } else { num = rb_funcall2(num, idDiv, 1, &modi); num = rb_funcall2(num, idMul, 1, &modi); } field = rb_str_format(1, &num, rb_ary_entry(action, 0)); } } return field; }