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

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 22 17:14:28 PST 2010


Revision: 4931
          http://trac.macosforge.org/projects/ruby/changeset/4931
Author:   watson1978 at gmail.com
Date:     2010-11-22 17:14:26 -0800 (Mon, 22 Nov 2010)
Log Message:
-----------
sprintf() will throw an exception when was given width format twice.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_raise(ArgumentError){ sprintf("%**s", 5, 5, 5) }
assert_raise(ArgumentError){ sprintf("%5*s", 5, 5) }

puts :ok
}}}

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

Modified: MacRuby/trunk/sprintf.c
===================================================================
--- MacRuby/trunk/sprintf.c	2010-11-22 22:53:00 UTC (rev 4930)
+++ MacRuby/trunk/sprintf.c	2010-11-23 01:14:26 UTC (rev 4931)
@@ -445,6 +445,7 @@
 	bool plus_flag = false;
 	bool minus_flag = false;
 	bool zero_flag = false;
+	bool width_flag = false;
 	bool precision_flag = false;
 	bool complete = false;
 	VALUE arg = 0;
@@ -455,6 +456,11 @@
 	VALUE sharp_pad = rb_str_new2("");
 	const long start = i;
 
+#define CHECK_FOR_WIDTH()				 \
+	if (width_flag) {				 \
+	    rb_raise(rb_eArgError, "width given twice"); \
+	}
+
 	while (i++ < format_len) {
 	    switch (format_str[i]) {
 		case '#':
@@ -462,6 +468,8 @@
 		    break;
 
 		case '*':
+		    CHECK_FOR_WIDTH();
+		    width_flag = true;
 		    if (format_str[++i] == '<' || format_str[i] == '{') {
 			SET_REF_TYPE(NAMED_REF);
 			width = NUM2LONG(rb_Integer(get_named_arg(format_str,
@@ -535,6 +543,8 @@
 			SET_REF_TYPE(REL_REF);
 			width = num;
 			i--;
+			CHECK_FOR_WIDTH();
+			width_flag = true;
 		    }
 		    break;
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101122/eb42dcca/attachment-0001.html>


More information about the macruby-changes mailing list