class String
Public Instance Methods
Cut off everthing beyond then nth character. Replace the last
bytes by ellipses.
a = "Redistribution and use in source and binary forms, with or without" a.axe( 16) #=> "Redistributio..."
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;
}
Set to empty string. Equivalent to str.replace "".
a = "hello" #=> "hello" a.clear #=> "" a.empty? #=> true
VALUE
rb_str_clear( VALUE str)
{
rb_str_modify( str);
rb_str_resize( str, 0);
return str;
}
Cut string to length. If nothing was removed, nil
is returned.
a = "hello" a.cut! 4 #=> "hell" a #=> "hell" a.cut! 4 #=> nil
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;
}
Returns first n characters of self or whole
string if n is nil. The returned substring will
be deleted from self. If n is negative,
characters will be eaten from the right.
a = "upcase" a.eat 2 #=> "up" a #=> "case"
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;
}
Checks whether the tail is oth.
"sys-apps".end_with?( "-apps") #=> true
VALUE
rb_str_end_with_p( VALUE str, VALUE oth)
{
return NIL_P( rb_str_ends_with_p( str, oth)) ? Qfalse : Qtrue;
}
Checks whether the tail is oth. Returns the position where
oth starts when matching.
"sys-apps".ends_with?( "-apps") #=> 3
Caution! The Ruby 1.9.3 method start_with? (notice the
missing s) just returns true or false.
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
}
Returns first n bytes in str.
"hello".head( 2) #=> "he"
VALUE
rb_str_head( int argc, VALUE *argv, VALUE str)
{
VALUE n;
VALUE str2;
long len;
len = rb_scan_args( argc, argv, "01", &n) == 1 ? NUM2LONG( n) : 1;
return rb_str_substr( str, 0, len);
}
Returns another string that may be modified without touching the original
object. This means dup for a string and to_s for
any other object.
VALUE
rb_str_new_string( VALUE obj)
{
return rb_str_dup( obj);
}
Returns self if and only if str is not empty,
nil otherwise.
"hello".notempty? #=> "hello" "".notempty? #=> nil
VALUE
rb_str_notempty_p( VALUE str)
{
#if 0
/* Ruby Coding style */
if (RSTRING_LEN( str) == 0)
return Qnil;
return str;
#else
return RSTRING_LEN( str) ? str : Qnil;
#endif
}
Returns the ASCII value of the first character, if any.
Caution! For UTF-8 characters, this will return the first byte's value. This will do no harm in Ruby 1.8 but the standard Ruby 1.9 +String#ord+ returns the first codepoint. Please do not overwrite that method.
VALUE rb_str_ord( VALUE str)
{
return RSTRING_LEN( str) > 0 ? INT2FIX( RSTRING_PTR( str)[ 0]) : Qnil;
}
Return rest after n bytes in str.
"hello".rest( 2) #=> "llo"
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);
}
Checks whether the head is oth.
"sys-apps".start_with?( "sys-") #=> true
VALUE
rb_str_start_with_p( VALUE str, VALUE oth)
{
return NIL_P( rb_str_starts_with_p( str, oth)) ? Qfalse : Qtrue;
}
Checks whether the head is oth. Returns length of
oth when matching.
"sys-apps".starts_with?( "sys-") #=> 4
Caution! The Ruby 1.9.3 method start_with? (notice the
missing s) just returns true or false.
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
}
Returns last n bytes in str.
"hello".tail( 2) #=> "lo"
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);
}