stringio.c: encoding at empty chomped result

* ext/stringio/stringio.c (strio_gets): should return string with
  the external encoding, at empty chomped result .

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59582 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-08-13 07:31:49 +00:00
Родитель 24d03b2204
Коммит e022015c62
2 изменённых файлов: 11 добавлений и 9 удалений

Просмотреть файл

@ -107,15 +107,14 @@ enc_subseq(VALUE str, long pos, long len, rb_encoding *enc)
}
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, struct StringIO *ptr)
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, struct StringIO *ptr)
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, struct StringIO *ptr)
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, struct StringIO *ptr)
}
}
}
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 self)
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;

Просмотреть файл

@ -96,6 +96,8 @@ class TestStringIO < Test::Unit::TestCase
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