rstrip!()
public
Removes trailing whitespace from str, returning nil if no change
was made. See also String#lstrip! and
String#strip!.
" hello ".rstrip
"hello".rstrip!
Show source
/*
* call-seq:
* str.rstrip! => self or nil
*
* Removes trailing whitespace from <i>str</i>, returning <code>nil</code> if
* no change was made. See also <code>String#lstrip!</code> and
* <code>String
*
* " hello ".rstrip
* "hello".rstrip!
*/
static VALUE
rb_str_rstrip_bang(str)
VALUE str;
{
char *s, *t, *e;
s = RSTRING(str)->ptr;
if (!s || RSTRING(str)->len == 0) return Qnil;
e = t = s + RSTRING(str)->len;
/* remove trailing '\0's */
while (s < t && t[-1] == '\0') t--;
/* remove trailing spaces */
while (s < t && ISSPACE(*(t-1))) t--;
if (t < e) {
rb_str_modify(str);
RSTRING(str)->len = t-s;
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
return str;
}
return Qnil;
}