diff options
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | hash.c | 10 | ||||
-rw-r--r-- | include/ruby/intern.h | 1 | ||||
-rw-r--r-- | sprintf.c | 49 |
4 files changed, 71 insertions, 3 deletions
@@ -2,6 +2,20 @@ Tue Sep 30 16:53:55 2008 Nobuyoshi Nakada <nobu@ruby-lang.org> * parse.y (stmt): returns non zero. [ruby-dev:36633] +Tue Sep 30 16:52:38 2008 Yukihiro Matsumoto <matz@ruby-lang.org> + + * 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. + Tue Sep 30 13:19:01 2008 Eric Hodel <drbrain@segment7.net> * test/rubygems/test_gem_installer.rb: remove extra call to setup to @@ -503,7 +503,7 @@ rb_hash_lookup(VALUE hash, VALUE key) */ static VALUE -rb_hash_fetch(int argc, VALUE *argv, VALUE hash) +rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash) { VALUE key, if_none; VALUE val; @@ -525,6 +525,12 @@ rb_hash_fetch(int argc, VALUE *argv, VALUE hash) return val; } +VALUE +rb_hash_fetch(VALUE hash, VALUE key) +{ + return rb_hash_fetch_m(1, &key, hash); +} + /* * call-seq: * hsh.default(key=nil) => obj @@ -2597,7 +2603,7 @@ Init_Hash(void) rb_define_method(rb_cHash,"[]", rb_hash_aref, 1); rb_define_method(rb_cHash,"hash", rb_hash_hash, 0); rb_define_method(rb_cHash,"eql?", rb_hash_eql, 1); - rb_define_method(rb_cHash,"fetch", rb_hash_fetch, -1); + rb_define_method(rb_cHash,"fetch", rb_hash_fetch_m, -1); rb_define_method(rb_cHash,"[]=", rb_hash_aset, 2); rb_define_method(rb_cHash,"store", rb_hash_aset, 2); rb_define_method(rb_cHash,"default", rb_hash_default, -1); diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 5f6882d56a..d99c39636b 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -358,6 +358,7 @@ VALUE rb_hash_dup(VALUE); VALUE rb_hash_freeze(VALUE); VALUE rb_hash_aref(VALUE, VALUE); VALUE rb_hash_lookup(VALUE, VALUE); +VALUE rb_hash_fetch(VALUE, VALUE); VALUE rb_hash_aset(VALUE, VALUE, VALUE); VALUE rb_hash_delete_if(VALUE); VALUE rb_hash_delete(VALUE,VALUE); @@ -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; |