summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-27 23:49:52 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-27 23:49:52 +0000
commit48379c7309a0603186f6a450f73ea2178185589a (patch)
tree2d84df58e31cb609f86afefc0c7e661f8d04eec1 /sprintf.c
parent456f68f2fd3ee8f0d95641ac98b9550465487c01 (diff)
* sprintf.c (rb_str_format): checks if named argument given twice.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23090 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 8191170..9f3183a 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -119,11 +119,12 @@ 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) (posarg > 0 ? \
- (rb_raise(rb_eArgError, "named after unnumbered(%d)", posarg), 0) : \
+#define GETNAMEARG(id, name, len) ( \
+ posarg > 0 ? \
+ (rb_raise(rb_eArgError, "named%.*s after unnumbered(%d)", (len), (name), posarg), 0) : \
posarg == -1 ? \
- (rb_raise(rb_eArgError, "named after numbered"), 0) : \
- (posarg = -2, rb_hash_lookup(get_hash(&hash, argc, argv), id)))
+ (rb_raise(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) \
for (; p < end && rb_enc_isdigit(*p, enc); p++) { \
@@ -472,6 +473,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
for (; p < end; p++) {
const char *t;
int n;
+ ID id = 0;
for (t = p; t < end && *t != '%'; t++) ;
PUSH(p, t - p);
@@ -544,7 +546,6 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
{
const char *start = p;
char term = (*p == '<') ? '>' : '}';
- ID id;
for (; p < end && *p != term; ) {
p += rb_enc_mbclen(p, end, enc);
@@ -552,8 +553,15 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if (p >= end) {
rb_raise(rb_eArgError, "malformed name - unmatched parenthesis");
}
+ if (id) {
+ rb_raise(rb_eArgError, "name%.*s after <%s>",
+ (int)(p - start + 1), start, rb_id2name(id));
+ }
id = rb_intern3(start + 1, p - start - 1, enc);
- nextvalue = GETNAMEARG(ID2SYM(id));
+ nextvalue = GETNAMEARG(ID2SYM(id), start, (int)(p - start + 1));
+ if (nextvalue == Qundef) {
+ rb_raise(rb_eKeyError, "key%.*s not found", (int)(p - start + 1), start);
+ }
if (term == '}') goto format_s;
p++;
goto retry;