method
chomp!
v1_9_1_378 -
Show latest stable
- Class:
String
chomp!(p1 = v1)public
Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made.
static VALUE
rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
{
rb_encoding *enc;
VALUE rs;
int newline;
char *p, *pp, *e;
long len, rslen;
len = RSTRING_LEN(str);
if (len == 0) return Qnil;
p = RSTRING_PTR(str);
e = p + len;
if (argc == 0) {
rs = rb_rs;
if (rs == rb_default_rs) {
smart_chomp:
str_modify_keep_cr(str);
enc = rb_enc_get(str);
if (rb_enc_mbminlen(enc) > 1) {
pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
if (rb_enc_is_newline(pp, e, enc)) {
e = pp;
}
pp = e - rb_enc_mbminlen(enc);
if (pp >= p) {
pp = rb_enc_left_char_head(p, pp, e, enc);
if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
e = pp;
}
}
if (e == RSTRING_END(str)) {
return Qnil;
}
len = e - RSTRING_PTR(str);
STR_SET_LEN(str, len);
}
else {
if (RSTRING_PTR(str)[len-1] == '\n') {
STR_DEC_LEN(str);
if (RSTRING_LEN(str) > 0 &&
RSTRING_PTR(str)[RSTRING_LEN(str)-1] == '\r') {
STR_DEC_LEN(str);
}
}
else if (RSTRING_PTR(str)[len-1] == '\r') {
STR_DEC_LEN(str);
}
else {
return Qnil;
}
}
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
return str;
}
}
else {
rb_scan_args(argc, argv, "01", &rs);
}
if (NIL_P(rs)) return Qnil;
StringValue(rs);
rslen = RSTRING_LEN(rs);
if (rslen == 0) {
while (len>0 && p[len-1] == '\n') {
len--;
if (len>0 && p[len-1] == '\r')
len--;
}
if (len < RSTRING_LEN(str)) {
str_modify_keep_cr(str);
STR_SET_LEN(str, len);
RSTRING_PTR(str)[len] = '\0';
return str;
}
return Qnil;
}
if (rslen > len) return Qnil;
newline = RSTRING_PTR(rs)[rslen-1];
if (rslen == 1 && newline == '\n')
goto smart_chomp;
enc = rb_enc_check(str, rs);
if (is_broken_string(rs)) {
return Qnil;
}
pp = e - rslen;
if (p[len-1] == newline &&
(rslen <= 1 ||
memcmp(RSTRING_PTR(rs), pp, rslen) == 0)) {
if (rb_enc_left_char_head(p, pp, e, enc) != pp)
return Qnil;
rb_str_modify(str);
STR_SET_LEN(str, RSTRING_LEN(str) - rslen);
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
return str;
}
return Qnil;
} Related methods
- Instance methods
- %
- *
- +
- <<
- <=>
- ==
- ===
- =~
- []
- []=
- ascii_only?
- block_scanf
- bytes
- bytesize
- capitalize
- capitalize!
- casecmp
- center
- chars
- chomp
- chomp!
- chop
- chop!
- chr
- clear
- codepoints
- concat
- count
- crypt
- delete
- delete!
- downcase
- downcase!
- dump
- each_byte
- each_char
- each_codepoint
- each_line
- empty?
- encode
- encode!
- encoding
- end_with?
- eql?
- force_encoding
- getbyte
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- is_binary_data?
- is_complex_yaml?
- iseuc
- isjis
- issjis
- isutf8
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- next
- next!
- oct
- ord
- parse_csv
- partition
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- setbyte
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ!
- sum
- swapcase
- swapcase!
- to_c
- to_d
- to_f
- to_i
- to_r
- to_s
- to_str
- to_sym
- to_yaml
- toeuc
- tojis
- tolocale
- tosjis
- toutf16
- toutf32
- toutf8
- tr
- tr!
- tr_s
- tr_s!
- unpack
- upcase
- upcase!
- upto
- valid_encoding?
- Class methods
- new
- try_convert
- yaml_new