From c58736dbdc74400d7ce58804e46b10c406b91c20 Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 18 Jan 2008 04:49:14 +0000 Subject: * string.c (rb_str_dump): preserve the encoding of source string if it is ASCII compatible. otherwise, add '.force_encoding()' for ugly work around. maybe we should implement some other way to keep non ASCII encoding in dumped string. [ruby-dev:33142] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15108 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ string.c | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index b691e6cc87..86f5b595b5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -98,6 +98,13 @@ Thu Jan 17 21:01:25 2008 Tadayoshi Funaba * lib/date.rb, lib/date/format.rb: some trivial changes. +Thu Jan 17 13:07:18 2008 Yukihiro Matsumoto + + * string.c (rb_str_dump): preserve the encoding of source string + if it is ASCII compatible. otherwise, add '.force_encoding()' + for ugly work around. maybe we should implement some other way + to keep non ASCII encoding in dumped string. [ruby-dev:33142] + Thu Jan 17 10:30:32 2008 Yukihiro Matsumoto * io.c (io_fwrite): always flush IO on tty, even without newlines. diff --git a/string.c b/string.c index 9b79453755..502144c309 100644 --- a/string.c +++ b/string.c @@ -3210,6 +3210,7 @@ escape_codepoint: VALUE rb_str_dump(VALUE str) { + rb_encoding *enc0 = rb_enc_get(str); rb_encoding *enc = rb_enc_from_index(0); long len; char *p, *pend; @@ -3242,6 +3243,10 @@ rb_str_dump(VALUE str) break; } } + if (!rb_enc_asciicompat(enc0)) { + len += 19; /* ".force_encoding('')" */ + len += strlen(enc0->name); + } result = rb_str_new5(str, 0, len); p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str); @@ -3301,10 +3306,14 @@ rb_str_dump(VALUE str) } } *q++ = '"'; + if (!rb_enc_asciicompat(enc0)) { + sprintf(q, ".force_encoding(\"%s\")", enc0->name); + enc0 = enc; + } OBJ_INFECT(result, str); /* result from dump is ASCII */ - rb_enc_associate(result, enc); + rb_enc_associate(result, enc0); return result; } -- cgit v1.2.3