Skip to content

Commit

Permalink
fixed the bug of sprintf() within "%{named}" format.
Browse files Browse the repository at this point in the history
Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_equal("value", sprintf("%{key}", :key => "value"))
assert_equal("1234.56", sprintf("%{key}", :key => 1234.56))
assert_equal("value{key2}", sprintf("%{key}{key2}", :key => "value"))
assert_raise(ArgumentError) {sprintf("%1${key2}", :key => "value")}

puts :ok
}}}

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@4948 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
Watson1978 committed Nov 26, 2010
1 parent 9031bde commit d8ba17f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sprintf.c
Expand Up @@ -609,14 +609,24 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)

case '<':
case '{':
{
char term = (format_str[i] == '<') ? '>' : '}';

if (named_flag) {
rb_raise(rb_eArgError, "named given twice");
}
named_flag = true;
SET_REF_TYPE(NAMED_REF);
arg = get_named_arg(format_str, format_len, &i,
GETNTHARG(0));
if (term == '}') {
if (TYPE(arg) != T_STRING) {
arg = rb_obj_as_string(arg);
}
goto format_s;
}
break;
}

case 'd':
case 'D':
Expand Down Expand Up @@ -737,6 +747,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
arg = (tolower(format_str[i]) != 's'
? rb_inspect(arg) : TYPE(arg) == T_STRING
? rb_str_new3(arg) : rb_obj_as_string(arg));
format_s:
if (precision_flag && precision < rb_str_chars_len(arg)) {
CFStringPad((CFMutableStringRef)arg, NULL, precision,
0);
Expand Down

0 comments on commit d8ba17f

Please sign in to comment.