Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed the bug of sprintf() within "%{named}" format.

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...
commit d8ba17fe057b73e1358f2ae122e07ef2aef87817 1 parent 9031bde
Watson Watson1978 authored
Showing with 11 additions and 0 deletions.
  1. +11 −0 sprintf.c
11 sprintf.c
View
@@ -609,6 +609,9 @@ 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");
}
@@ -616,7 +619,14 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
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':
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.