[前][次][番号順一覧][スレッド一覧]

ruby-changes:42179

From: nobu <ko1@a...>
Date: Fri, 25 Mar 2016 00:49:48 +0900 (JST)
Subject: [ruby-changes:42179] nobu:r54253 (trunk): strftime.c: Bignum conversion

nobu	2016-03-25 00:49:43 +0900 (Fri, 25 Mar 2016)

  New Revision: 54253

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54253

  Log:
    strftime.c: Bignum conversion
    
    * strftime.c (format_value): convert from Bignum to String,
      instead of rb_str_format.

  Modified files:
    trunk/strftime.c
    trunk/test/ruby/test_time.rb
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 54252)
+++ test/ruby/test_time.rb	(revision 54253)
@@ -680,6 +680,12 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L680
     t = Time.at(946684800, 123456.789)
     assert_equal("946684800", t.strftime("%s"))
     assert_equal("946684800", t.utc.strftime("%s"))
+
+    t = Time.at(10000000000000000000000)
+    assert_equal("<<10000000000000000000000>>", t.strftime("<<%s>>"))
+    assert_equal("<<010000000000000000000000>>", t.strftime("<<%24s>>"))
+    assert_equal("<<010000000000000000000000>>", t.strftime("<<%024s>>"))
+    assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24s>>"))
   end
 
   def test_strftime_zone
@@ -759,6 +765,9 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L765
 
     t = Time.utc(10000000000000000000000,1,1)
     assert_equal("<<10000000000000000000000>>", t.strftime("<<%Y>>"))
+    assert_equal("<<010000000000000000000000>>", t.strftime("<<%24Y>>"))
+    assert_equal("<<010000000000000000000000>>", t.strftime("<<%024Y>>"))
+    assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24Y>>"))
   end
 
   def test_strftime_weeknum
Index: strftime.c
===================================================================
--- strftime.c	(revision 54252)
+++ strftime.c	(revision 54253)
@@ -196,14 +196,11 @@ case_conv(char *s, ptrdiff_t i, int flag https://github.com/ruby/ruby/blob/trunk/strftime.c#L196
 }
 
 static VALUE
-format_value(const char *fmt, VALUE val, int precision)
+format_value(VALUE val, int base)
 {
-	struct RString fmtv;
-	VALUE str = rb_setup_fake_str(&fmtv, fmt, strlen(fmt), 0);
-	VALUE args[2];
-	args[0] = INT2FIX(precision);
-	args[1] = val;
-	return rb_str_format(2, args, str);
+	if (!RB_TYPE_P(val, T_BIGNUM))
+		val = rb_Integer(val);
+	return rb_big2str(val, base);
 }
 
 /*
@@ -323,9 +320,14 @@ rb_strftime_with_timespec(VALUE ftime, c https://github.com/ruby/ruby/blob/trunk/strftime.c#L320
                                 FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
                         } \
                         else { \
-				const char *fmts = FMT_PADDING(fmt, def_pad); \
+				const int base = ((fmt[0] == 'x') ? 16 : \
+						  (fmt[0] == 'o') ? 8 : \
+						  10); \
 				precision = FMT_PRECISION(def_prec); \
-				tmp = format_value(fmts, tmp, precision); \
+				if (!padding) padding = (def_pad); \
+				tmp = format_value(tmp, base); \
+				i = RSTRING_LEN(tmp); \
+				FILL_PADDING(i); \
 				rb_str_set_len(ftime, s-start); \
 				rb_str_append(ftime, tmp); \
 				RSTRING_GETMEM(ftime, s, len); \

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]