fix chomping newline only line

* string.c (chomp_newline): fix chomping newline only line.
  rb_enc_prev_char return NULL if no previous character and must
  not call rb_enc_ascget on it.  a patch by Ary Borenszweig
  <asterite AT gmail.com> at [ruby-core:78666].  [Bug #13037]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57088 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-12-16 01:12:09 +00:00
parent 17c2828581
commit 75755ef159
2 changed files with 13 additions and 1 deletions

View File

@ -7409,7 +7409,7 @@ chomp_newline(const char *p, const char *e, rb_encoding *enc)
if (rb_enc_is_newline(prev, e, enc)) {
e = prev;
prev = rb_enc_prev_char(p, e, e, enc);
if (rb_enc_ascget(prev, e, NULL, enc) == '\r')
if (prev && rb_enc_ascget(prev, e, NULL, enc) == '\r')
e = prev;
}
return e;

View File

@ -843,6 +843,18 @@ CODE
assert_equal "hello", S("hello\nworld").each_line(chomp: true).next
assert_equal "hello\nworld", S("hello\nworld").each_line(nil, chomp: true).next
res = []
S("").each_line(chomp: true) {|x| res << x}
assert_equal([], res)
res = []
S("\n").each_line(chomp: true) {|x| res << x}
assert_equal([S("")], res)
res = []
S("\r\n").each_line(chomp: true) {|x| res << x}
assert_equal([S("")], res)
end
def test_lines