1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* sprintf.c (rb_str_format): fix exception message encoding.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35299 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-04-11 13:01:16 +00:00
parent 51f2207fc9
commit a5d1da5d74
2 changed files with 48 additions and 9 deletions

View file

@ -119,11 +119,11 @@ sign_bits(int base, const char *p)
#define GETNTHARG(nth) \
(((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])
#define GETNAMEARG(id, name, len) ( \
#define GETNAMEARG(id, name, len, enc) ( \
posarg > 0 ? \
(rb_raise(rb_eArgError, "named%.*s after unnumbered(%d)", (len), (name), posarg), 0) : \
(rb_enc_raise((enc), rb_eArgError, "named%.*s after unnumbered(%d)", (len), (name), posarg), 0) : \
posarg == -1 ? \
(rb_raise(rb_eArgError, "named%.*s after numbered", (len), (name)), 0) : \
(rb_enc_raise((enc), rb_eArgError, "named%.*s after numbered", (len), (name)), 0) : \
(posarg = -2, rb_hash_lookup2(get_hash(&hash, argc, argv), (id), Qundef)))
#define GETNUM(n, val) \
@ -578,19 +578,20 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if ((size_t)(p - start) >= INT_MAX) {
const int message_limit = 20;
len = (int)(rb_enc_right_char_head(start, start + message_limit, p, enc) - start);
rb_raise(rb_eArgError, "too long name (%"PRIdSIZE" bytes) - %.*s...%c",
(size_t)(p - start - 2), len, start, term);
rb_enc_raise(enc, rb_eArgError,
"too long name (%"PRIdSIZE" bytes) - %.*s...%c",
(size_t)(p - start - 2), len, start, term);
}
#endif
len = (int)(p - start + 1); /* including parenthesis */
if (id) {
rb_raise(rb_eArgError, "named%.*s after <%s>",
len, start, rb_id2name(id));
rb_enc_raise(enc, rb_eArgError, "named%.*s after <%s>",
len, start, rb_id2name(id));
}
id = rb_intern3(start + 1, len - 2 /* without parenthesis */, enc);
nextvalue = GETNAMEARG(ID2SYM(id), start, len);
nextvalue = GETNAMEARG(ID2SYM(id), start, len, enc);
if (nextvalue == Qundef) {
rb_raise(rb_eKeyError, "key%.*s not found", len, start);
rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
}
if (term == '}') goto format_s;
p++;

View file

@ -333,6 +333,25 @@ class TestSprintf < Test::Unit::TestCase
assert_equal("named<key2> after <key>", e.message)
end
def test_named_untyped_enc
key = "\u{3012}"
[Encoding::UTF_8, Encoding::EUC_JP].each do |enc|
k = key.encode(enc)
e = assert_raise(ArgumentError) {sprintf("%1$<#{k}>s", key: "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named<#{k}> after numbered", e.message)
e = assert_raise(ArgumentError) {sprintf("%s%s%<#{k}>s", "foo", "bar", key: "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named<#{k}> after unnumbered(2)", e.message)
e = assert_raise(ArgumentError) {sprintf("%<key><#{k}>s", key: "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named<#{k}> after <key>", e.message)
e = assert_raise(ArgumentError) {sprintf("%<#{k}><key>s", k.to_sym => "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named<key> after <#{k}>", e.message)
end
end
def test_named_typed
assert_equal("value", sprintf("%{key}", :key => "value"))
e = assert_raise(ArgumentError) {sprintf("%1${key2}", :key => "value")}
@ -343,4 +362,23 @@ class TestSprintf < Test::Unit::TestCase
assert_equal("named{key2} after <key>", e.message)
assert_equal("value{key2}", sprintf("%{key}{key2}", :key => "value"))
end
def test_named_typed_enc
key = "\u{3012}"
[Encoding::UTF_8, Encoding::EUC_JP].each do |enc|
k = key.encode(enc)
e = assert_raise(ArgumentError) {sprintf("%1${#{k}}s", key: "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named{#{k}} after numbered", e.message)
e = assert_raise(ArgumentError) {sprintf("%s%s%{#{k}}s", "foo", "bar", key: "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named{#{k}} after unnumbered(2)", e.message)
e = assert_raise(ArgumentError) {sprintf("%<key>{#{k}}s", key: "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named{#{k}} after <key>", e.message)
e = assert_raise(ArgumentError) {sprintf("%<#{k}>{key}s", k.to_sym => "value")}
assert_equal(enc, e.message.encoding)
assert_equal("named{key} after <#{k}>", e.message)
end
end
end