method
chomp!
v1_8_7_330 -
Show latest stable
- Class:
String
chomp!(...)public
Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made.
/*
* call-seq:
* str.chomp!(separator=$/) => str or nil
*
* Modifies <i>str</i> in place as described for <code>String#chomp</code>,
* returning <i>str</i>, or <code>nil</code> if no modifications were made.
*/
static VALUE
rb_str_chomp_bang(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE rs;
int newline;
char *p;
long len, rslen;
if (rb_scan_args(argc, argv, "01", &rs) == 0) {
len = RSTRING(str)->len;
if (len == 0) return Qnil;
p = RSTRING(str)->ptr;
rs = rb_rs;
if (rs == rb_default_rs) {
smart_chomp:
rb_str_modify(str);
if (RSTRING(str)->ptr[len-1] == '\n') {
RSTRING(str)->len--;
if (RSTRING(str)->len > 0 &&
RSTRING(str)->ptr[RSTRING(str)->len-1] == '\r') {
RSTRING(str)->len--;
}
}
else if (RSTRING(str)->ptr[len-1] == '\r') {
RSTRING(str)->len--;
}
else {
return Qnil;
}
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
return str;
}
}
if (NIL_P(rs)) return Qnil;
StringValue(rs);
len = RSTRING(str)->len;
if (len == 0) return Qnil;
p = RSTRING(str)->ptr;
rslen = RSTRING(rs)->len;
if (rslen == 0) {
while (len>0 && p[len-1] == '\n') {
len--;
if (len>0 && p[len-1] == '\r')
len--;
}
if (len < RSTRING(str)->len) {
rb_str_modify(str);
RSTRING(str)->len = len;
RSTRING(str)->ptr[len] = '\0';
return str;
}
return Qnil;
}
if (rslen > len) return Qnil;
newline = RSTRING(rs)->ptr[rslen-1];
if (rslen == 1 && newline == '\n')
goto smart_chomp;
if (p[len-1] == newline &&
(rslen <= 1 ||
rb_memcmp(RSTRING(rs)->ptr, p+len-rslen, rslen) == 0)) {
rb_str_modify(str);
RSTRING(str)->len -= rslen;
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
return str;
}
return Qnil;
} Related methods
- Instance methods
- %
- *
- +
- <<
- <=>
- ==
- =~
- []
- []=
- block_scanf
- bytes
- bytesize
- capitalize
- capitalize!
- casecmp
- center
- chars
- chomp
- chomp!
- chop
- chop
- chop!
- chop!
- concat
- count
- crypt
- delete
- delete
- delete!
- delete!
- downcase
- downcase!
- dump
- each
- each_byte
- each_char
- each_char
- each_line
- empty?
- end_regexp
- end_with?
- eql?
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- is_binary_data?
- is_complex_yaml?
- iseuc
- issjis
- isutf8
- jcount
- jlength
- jsize
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- mbchar?
- next
- next!
- oct
- partition
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze
- squeeze!
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ
- succ!
- succ!
- sum
- swapcase
- swapcase!
- to_f
- to_i
- to_s
- to_str
- to_sym
- to_yaml
- toeuc
- tojis
- tosjis
- toutf16
- toutf8
- tr
- tr
- tr!
- tr!
- tr_s
- tr_s
- tr_s!
- tr_s!
- unpack
- upcase
- upcase!
- upto
- Class methods
- new
- yaml_new
- Private methods
-
_expand_ch -
_regex_quote -
expand_ch_hash -
original_succ -
original_succ!