summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-30 08:01:11 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-30 08:01:11 +0000
commit8bfa0ac8692e0b525e92378605c63ab3a02447ac (patch)
treed37bfe62a6777ad6e512b835a4e393158fe5f5af /sprintf.c
parent9bcbc9060543659c4ae14b64929cec33cddb60b7 (diff)
* sprintf.c (rb_str_format): add %<name>s style named argument
support. a patch from Yusuke Endoh <mame at tsg.ne.jp> in [ruby-dev:35851]. * sprintf.c (rb_str_format): add gettext style named format (%{name}) support. inspired by [ruby-dev:35852]. * sprintf.c (GETNAMEARG): should raise KeyError exception when no named argument found. * hash.c (rb_hash_fetch): export fetch function. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19641 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/sprintf.c b/sprintf.c
index c4fe2f2..0085cfc 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -103,18 +103,28 @@ sign_bits(int base, const char *p)
} while (0)
#define GETARG() (nextvalue != Qundef ? nextvalue : \
- posarg < 0 ? \
+ posarg == -1 ? \
(rb_raise(rb_eArgError, "unnumbered(%d) mixed with numbered", nextarg), 0) : \
+ posarg == -2 ? \
+ (rb_raise(rb_eArgError, "unnumbered(%d) mixed with named", nextarg), 0) : \
(posarg = nextarg++, GETNTHARG(posarg)))
#define GETPOSARG(n) (posarg > 0 ? \
(rb_raise(rb_eArgError, "numbered(%d) after unnumbered(%d)", n, posarg), 0) : \
+ posarg == -2 ? \
+ (rb_raise(rb_eArgError, "numbered(%d) after named", n), 0) : \
((n < 1) ? (rb_raise(rb_eArgError, "invalid index - %d$", n), 0) : \
(posarg = -1, GETNTHARG(n))))
#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) : \
+ posarg == -1 ? \
+ (rb_raise(rb_eArgError, "named after numbered"), 0) : \
+ rb_hash_fetch(get_hash(&hash, argc, argv), id))
+
#define GETNUM(n, val) \
for (; p < end && rb_enc_isdigit(*p, enc); p++) { \
int next_n = 10 * n + (*p - '0'); \
@@ -141,6 +151,21 @@ sign_bits(int base, const char *p)
val = NUM2INT(tmp); \
} while (0)
+static VALUE
+get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
+{
+ VALUE tmp;
+
+ if (*hash != Qundef) return *hash;
+ if (argc != 2) {
+ rb_raise(rb_eArgError, "one hash required");
+ }
+ tmp = rb_check_convert_type(argv[1], T_HASH, "Hash", "to_hash");
+ if (NIL_P(tmp)) {
+ rb_raise(rb_eArgError, "one hash required");
+ }
+ return (*hash = tmp);
+}
/*
* call-seq:
@@ -412,6 +437,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
VALUE nextvalue;
VALUE tmp;
VALUE str;
+ volatile VALUE hash = Qundef;
#define CHECK_FOR_WIDTH(f) \
if ((f) & FWIDTH) { \
@@ -513,6 +539,26 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
flags |= FWIDTH;
goto retry;
+ case '<':
+ case '{':
+ {
+ const char *start = p;
+ char term = (*p == '<') ? '>' : '}';
+ ID id;
+
+ for (; p < end && *p != term; ) {
+ p += rb_enc_mbclen(p, end, enc);
+ }
+ if (p >= end) {
+ rb_raise(rb_eArgError, "malformed name - unmatched parenthesis");
+ }
+ id = rb_intern3(start + 1, p - start - 1, enc);
+ nextvalue = GETNAMEARG(ID2SYM(id));
+ if (term == '}') goto format_s;
+ p++;
+ goto retry;
+ }
+
case '*':
CHECK_FOR_WIDTH(flags);
flags |= FWIDTH;
@@ -597,6 +643,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
case 's':
case 'p':
+ format_s:
{
VALUE arg = GETARG();
long len, slen;