[macruby-changes] [4948] MacRuby/trunk/sprintf.c

source_changes at macosforge.org source_changes at macosforge.org
Thu Nov 25 20:34:46 PST 2010


Revision: 4948
          http://trac.macosforge.org/projects/ruby/changeset/4948
Author:   watson1978 at gmail.com
Date:     2010-11-25 20:34:44 -0800 (Thu, 25 Nov 2010)
Log Message:
-----------
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
}}}

Modified Paths:
--------------
    MacRuby/trunk/sprintf.c

Modified: MacRuby/trunk/sprintf.c
===================================================================
--- MacRuby/trunk/sprintf.c	2010-11-26 02:36:11 UTC (rev 4947)
+++ MacRuby/trunk/sprintf.c	2010-11-26 04:34:44 UTC (rev 4948)
@@ -609,6 +609,9 @@
 
 		case '<':
 		case '{':
+		{
+		    char term = (format_str[i] == '<') ? '>' : '}';
+
 		    if (named_flag) {
 			rb_raise(rb_eArgError, "named given twice");
 		    }
@@ -616,7 +619,14 @@
 		    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 @@
 		    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);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101125/25f5f260/attachment.html>


More information about the macruby-changes mailing list