ruby-changes:47465
From: nobu <ko1@a...>
Date: Sun, 13 Aug 2017 16:31:55 +0900 (JST)
Subject: [ruby-changes:47465] nobu:r59582 (trunk): stringio.c: encoding at empty chomped result
nobu 2017-08-13 16:31:49 +0900 (Sun, 13 Aug 2017) New Revision: 59582 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59582 Log: stringio.c: encoding at empty chomped result * ext/stringio/stringio.c (strio_gets): should return string with the external encoding, at empty chomped result . Modified files: trunk/ext/stringio/stringio.c trunk/test/stringio/test_stringio.rb Index: test/stringio/test_stringio.rb =================================================================== --- test/stringio/test_stringio.rb (revision 59581) +++ test/stringio/test_stringio.rb (revision 59582) @@ -96,6 +96,8 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L96 stringio = StringIO.new("abc\n\ndef\n") assert_equal("abc\n", stringio.gets("", chomp: true)) assert_equal("def", stringio.gets("", chomp: true)) + + assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true)) end def test_gets_chomp_eol Index: ext/stringio/stringio.c =================================================================== --- ext/stringio/stringio.c (revision 59581) +++ ext/stringio/stringio.c (revision 59582) @@ -107,15 +107,14 @@ enc_subseq(VALUE str, long pos, long len https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L107 } static VALUE -strio_substr(struct StringIO *ptr, long pos, long len) +strio_substr(struct StringIO *ptr, long pos, long len, rb_encoding *enc) { VALUE str = ptr->string; - rb_encoding *enc = get_enc(ptr); long rlen = RSTRING_LEN(str) - pos; if (len > rlen) len = rlen; if (len < 0) len = 0; - if (len == 0) return rb_str_new(0,0); + if (len == 0) return rb_enc_str_new(0, 0, enc); return enc_subseq(str, pos, len, enc); } @@ -1059,6 +1058,7 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1058 long n, limit = arg->limit; VALUE str = arg->rs; int w = 0; + rb_encoding *enc = get_enc(ptr); if (ptr->pos >= (n = RSTRING_LEN(ptr->string))) { return Qnil; @@ -1073,7 +1073,7 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1073 if (arg->chomp) { w = chomp_newline_width(s, e); } - str = strio_substr(ptr, ptr->pos, e - s - w); + str = strio_substr(ptr, ptr->pos, e - s - w, enc); } else if ((n = RSTRING_LEN(str)) == 0) { p = s; @@ -1099,14 +1099,14 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1099 if (!w && arg->chomp) { w = chomp_newline_width(s, e); } - str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s - w); + str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s - w, enc); } else if (n == 1) { if ((p = memchr(s, RSTRING_PTR(str)[0], e - s)) != 0) { e = p + 1; w = (arg->chomp ? (p > s && *(p-1) == '\r') + 1 : 0); } - str = strio_substr(ptr, ptr->pos, e - s - w); + str = strio_substr(ptr, ptr->pos, e - s - w, enc); } else { if (n < e - s) { @@ -1127,7 +1127,7 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1127 } } } - str = strio_substr(ptr, ptr->pos, e - s - w); + str = strio_substr(ptr, ptr->pos, e - s - w, enc); } ptr->pos = e - RSTRING_PTR(ptr->string); ptr->lineno++; @@ -1409,8 +1409,8 @@ strio_read(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1409 break; } if (NIL_P(str)) { - str = strio_substr(ptr, ptr->pos, len); - if (binary) rb_enc_associate(str, rb_ascii8bit_encoding()); + rb_encoding *enc = binary ? rb_ascii8bit_encoding() : get_enc(ptr); + str = strio_substr(ptr, ptr->pos, len, enc); } else { long rest = RSTRING_LEN(ptr->string) - ptr->pos; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/