diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-05 07:28:09 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-05 07:28:09 +0000 |
commit | a2144bd72aad7c25e160cf283f957d59fe7c90b2 (patch) | |
tree | 06a002592baeb95dc5db6eebf2e697f504b26b20 /string.c | |
parent | cc7b3feb097fce18b207da8366f247e8b487fe34 (diff) |
chomp option
* io.c (extract_getline_opts): extract chomp option.
[Feature #12553]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56581 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -7975,6 +7975,20 @@ chomp_rs(int argc, const VALUE *argv) } } +VALUE +rb_str_chomp_string(VALUE str, VALUE rs) +{ + long olen = RSTRING_LEN(str); + long len = chompped_length(str, rs); + if (len >= olen) return Qnil; + STR_SET_LEN(str, len); + TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str)); + if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) { + ENC_CODERANGE_CLEAR(str); + } + return str; +} + /* * call-seq: * str.chomp!(separator=$/) -> str or nil @@ -7987,21 +8001,11 @@ static VALUE rb_str_chomp_bang(int argc, VALUE *argv, VALUE str) { VALUE rs; - long olen; str_modify_keep_cr(str); - if ((olen = RSTRING_LEN(str)) > 0 && !NIL_P(rs = chomp_rs(argc, argv))) { - long len; - len = chompped_length(str, rs); - if (len < olen) { - STR_SET_LEN(str, len); - TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str)); - if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) { - ENC_CODERANGE_CLEAR(str); - } - return str; - } - } - return Qnil; + if (RSTRING_LEN(str) == 0) return Qnil; + rs = chomp_rs(argc, argv); + if (NIL_P(rs)) return Qnil; + return rb_str_chomp_string(str, rs); } |