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

ruby-changes:42171

From: nobu <ko1@a...>
Date: Thu, 24 Mar 2016 14:20:28 +0900 (JST)
Subject: [ruby-changes:42171] nobu:r54245 (trunk): strftime.c: case conversion

nobu	2016-03-24 14:20:22 +0900 (Thu, 24 Mar 2016)

  New Revision: 54245

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

  Log:
    strftime.c: case conversion
    
    * strftime.c (STRFTIME): deal with case conversion flags for
      recursive formats.

  Modified files:
    trunk/ChangeLog
    trunk/strftime.c
    trunk/test/ruby/test_time.rb
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 54244)
+++ test/ruby/test_time.rb	(revision 54245)
@@ -730,6 +730,13 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L730
     assert_equal(" 2", t.strftime("%l"))
     assert_equal("02", t.strftime("%0l"))
     assert_equal(" 2", t.strftime("%_l"))
+    assert_equal("MON", t.strftime("%^a"))
+    assert_equal("OCT", t.strftime("%^b"))
+
+    t = get_t2000
+    assert_equal("UTC", t.strftime("%^Z"))
+    assert_equal("utc", t.strftime("%#Z"))
+    assert_equal("SAT JAN  1 00:00:00 2000", t.strftime("%^c"))
   end
 
   def test_strftime_invalid_flags
Index: strftime.c
===================================================================
--- strftime.c	(revision 54244)
+++ strftime.c	(revision 54245)
@@ -157,6 +157,9 @@ max(int a, int b) https://github.com/ruby/ruby/blob/trunk/strftime.c#L157
 
 /* strftime --- produce formatted time */
 
+enum {LEFT, CHCASE, LOWER, UPPER};
+#define BIT_OF(n) (1U<<(n))
+
 static char *
 resize_buffer(VALUE ftime, char *s, const char **start, const char **endp,
 	      ptrdiff_t n)
@@ -171,6 +174,27 @@ resize_buffer(VALUE ftime, char *s, cons https://github.com/ruby/ruby/blob/trunk/strftime.c#L174
 	return s += len;
 }
 
+static char *
+case_conv(char *s, ptrdiff_t i, int flags)
+{
+	switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
+	case BIT_OF(UPPER):
+		do {
+			if (ISLOWER(*s)) *s = TOUPPER(*s);
+		} while (s++, --i);
+		break;
+	case BIT_OF(LOWER):
+		do {
+			if (ISUPPER(*s)) *s = TOLOWER(*s);
+		} while (s++, --i);
+		break;
+	default:
+		s += i;
+		break;
+	}
+	return s;
+}
+
 /*
  * enc is the encoding of the format. It is used as the encoding of resulted
  * string, but the name of the month and weekday are always US-ASCII. So it
@@ -195,8 +219,6 @@ rb_strftime_with_timespec(VALUE ftime, c https://github.com/ruby/ruby/blob/trunk/strftime.c#L219
 	long y;
 	int precision, flags, colons;
 	char padding;
-	enum {LEFT, CHCASE, LOWER, UPPER};
-#define BIT_OF(n) (1U<<(n))
 #ifdef MAILHEADER_EXT
 	int sign;
 #endif
@@ -269,6 +291,7 @@ rb_strftime_with_timespec(VALUE ftime, c https://github.com/ruby/ruby/blob/trunk/strftime.c#L291
 			i = RSTRING_LEN(ftime) - len; \
 			endp = (start = s) + rb_str_capacity(ftime); \
 			s += len; \
+			if (i > 0) case_conv(s, i, flags); \
 			if (precision > i) {\
 				NEEDS(precision); \
 				memmove(s + precision - i, s, i);\
@@ -834,21 +857,7 @@ rb_strftime_with_timespec(VALUE ftime, c https://github.com/ruby/ruby/blob/trunk/strftime.c#L857
 		if (i) {
 			FILL_PADDING(i);
 			memcpy(s, tp, i);
-			switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
-			case BIT_OF(UPPER):
-				do {
-					if (ISLOWER(*s)) *s = TOUPPER(*s);
-				} while (s++, --i);
-				break;
-			case BIT_OF(LOWER):
-				do {
-					if (ISUPPER(*s)) *s = TOLOWER(*s);
-				} while (s++, --i);
-				break;
-			default:
-				s += i;
-				break;
-			}
+			s = case_conv(s, i, flags);
 		}
 	}
 	if (format != format_end) {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54244)
+++ ChangeLog	(revision 54245)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Mar 24 14:20:21 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* strftime.c (STRFTIME): deal with case conversion flags for
+	  recursive formats.
+
 Thu Mar 24 12:43:26 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/date/date_core.c (dt_lite_iso8601): strftimev() always

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

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