aboutsummaryrefslogtreecommitdiffstats
path: root/sprintf.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-11 09:30:31 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-11 09:30:31 +0000
commit56e3b493043f5d1b25a52d073f25b8c0f825052f (patch)
treefa88b2cc3b0305c9a4808925566af5416e610482 /sprintf.c
parent9cf11364817c58b0a8801e458ee2ee1d0d040789 (diff)
downloadruby-56e3b493043f5d1b25a52d073f25b8c0f825052f.tar.gz
sprintf.c: nil value is valid
* sprintf.c (rb_str_format): look up the key, then get default value and raise KeyError if the returned value is nil. [ruby-dev:49338] [Ruby trunk - Bug #11677] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52537 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/sprintf.c b/sprintf.c
index 5c0f94005c..c2e5de5d64 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -605,12 +605,20 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
}
CHECKNAMEARG(start, len, enc);
get_hash(&hash, argc, argv);
- sym = rb_cstr_intern(start + 1,
- len - 2 /* without parenthesis */,
- enc);
- nextvalue = rb_hash_aref(hash, sym);
- if (NIL_P(nextvalue) && !FL_TEST(hash, HASH_PROC_DEFAULT)) {
- rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
+ sym = rb_check_symbol_cstr(start + 1,
+ len - 2 /* without parenthesis */,
+ enc);
+ if (!NIL_P(sym)) nextvalue = rb_hash_lookup2(hash, sym, Qundef);
+ if (nextvalue == Qundef) {
+ if (NIL_P(sym)) {
+ sym = rb_cstr_intern(start + 1,
+ len - 2 /* without parenthesis */,
+ enc);
+ }
+ nextvalue = rb_hash_default_value(hash, sym);
+ if (NIL_P(nextvalue)) {
+ rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
+ }
}
if (term == '}') goto format_s;
p++;