[ruby/stringio] stringio: encoding support

https://github.com/ruby/stringio/commit/7b20075ab0
This commit is contained in:
Nobuyoshi Nakada 2016-06-10 14:18:14 +09:00
Родитель 765d603153
Коммит c8f9e9a2a0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4BC7D6DF58D8DF60
2 изменённых файлов: 31 добавлений и 28 удалений

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

@ -190,40 +190,33 @@ strio_initialize(int argc, VALUE *argv, VALUE self)
static VALUE static VALUE
strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self) strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
{ {
VALUE string, mode; VALUE string, vmode, opt;
int trunc = 0; int oflags;
struct rb_io_enc_t convconfig;
switch (rb_scan_args(argc, argv, "02", &string, &mode)) { argc = rb_scan_args(argc, argv, "02:", &string, &vmode, &opt);
case 2: rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &ptr->flags, &convconfig);
if (FIXNUM_P(mode)) { if (argc) {
int flags = FIX2INT(mode); StringValue(string);
ptr->flags = rb_io_oflags_fmode(flags);
trunc = flags & O_TRUNC;
} }
else { else {
const char *m = StringValueCStr(mode); string = rb_enc_str_new("", 0, rb_default_external_encoding());
ptr->flags = rb_io_modestr_fmode(m);
trunc = *m == 'w';
} }
StringValue(string); if (OBJ_FROZEN_RAW(string)) {
if ((ptr->flags & FMODE_WRITABLE) && OBJ_FROZEN(string)) { if (ptr->flags & FMODE_WRITABLE) {
rb_syserr_fail(EACCES, 0); rb_syserr_fail(EACCES, 0);
} }
if (trunc) { }
else {
if (NIL_P(vmode)) {
ptr->flags |= FMODE_WRITABLE;
}
}
if (ptr->flags & FMODE_TRUNC) {
rb_str_resize(string, 0); rb_str_resize(string, 0);
} }
break;
case 1:
StringValue(string);
ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
break;
case 0:
string = rb_enc_str_new("", 0, rb_default_external_encoding());
ptr->flags = FMODE_READWRITE;
break;
}
ptr->string = string; ptr->string = string;
ptr->enc = 0; ptr->enc = convconfig.enc;
ptr->pos = 0; ptr->pos = 0;
ptr->lineno = 0; ptr->lineno = 0;
RBASIC(self)->flags |= (ptr->flags & FMODE_READWRITE) * (STRIO_READABLE / FMODE_READABLE); RBASIC(self)->flags |= (ptr->flags & FMODE_READWRITE) * (STRIO_READABLE / FMODE_READABLE);

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

@ -22,7 +22,6 @@ class TestStringIO < Test::Unit::TestCase
assert_raise(ArgumentError) { StringIO.new('', 'rx') } assert_raise(ArgumentError) { StringIO.new('', 'rx') }
assert_raise(ArgumentError) { StringIO.new('', 'rbt') } assert_raise(ArgumentError) { StringIO.new('', 'rbt') }
assert_raise(TypeError) { StringIO.new(nil) } assert_raise(TypeError) { StringIO.new(nil) }
assert_raise(TypeError) { StringIO.new('str', nil) }
o = Object.new o = Object.new
def o.to_str def o.to_str
@ -785,6 +784,17 @@ class TestStringIO < Test::Unit::TestCase
end; end;
end end
def test_encoding_write
s = StringIO.new("", "w:utf-32be")
s.print "abc"
assert_equal("abc".encode("utf-32be"), s.string)
end
def test_encoding_read
s = StringIO.new("abc".encode("utf-32be"), "r:utf-8")
assert_equal("\0\0\0a\0\0\0b\0\0\0c", s.read)
end
def assert_string(content, encoding, str, mesg = nil) def assert_string(content, encoding, str, mesg = nil)
assert_equal([content, encoding], [str, str.encoding], mesg) assert_equal([content, encoding], [str, str.encoding], mesg)
end end