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

ruby-changes:42167

From: nobu <ko1@a...>
Date: Thu, 24 Mar 2016 12:42:42 +0900 (JST)
Subject: [ruby-changes:42167] nobu:r54241 (trunk): date_core.c: fix inspect

nobu	2016-03-24 12:42:37 +0900 (Thu, 24 Mar 2016)

  New Revision: 54241

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

  Log:
    date_core.c: fix inspect
    
    * ext/date/date_core.c (mk_inspect_raw, mk_inspect): inspect by
      "%+"PRIsVALUE, to prevent intermediate strings from GC.

  Modified files:
    trunk/ChangeLog
    trunk/ext/date/date_core.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54240)
+++ ChangeLog	(revision 54241)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Mar 24 12:42:36 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/date/date_core.c (mk_inspect_raw, mk_inspect): inspect by
+	  "%+"PRIsVALUE, to prevent intermediate strings from GC.
+
 Thu Mar 24 11:43:32 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* strftime.c (rb_strftime_with_timespec): remove unnecessary
Index: ext/date/date_core.c
===================================================================
--- ext/date/date_core.c	(revision 54240)
+++ ext/date/date_core.c	(revision 54241)
@@ -6430,54 +6430,40 @@ d_lite_to_s(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L6430
 
 #ifndef NDEBUG
 static VALUE
-mk_inspect_flags(union DateData *x)
+mk_inspect_raw(union DateData *x, VALUE klass)
 {
-    return rb_enc_sprintf(rb_usascii_encoding(),
-			  "%c%c%c%c%c",
-			  (x->flags & COMPLEX_DAT) ? 'C' : 'S',
-			  (x->flags & HAVE_JD)     ? 'j' : '-',
-			  (x->flags & HAVE_DF)     ? 'd' : '-',
-			  (x->flags & HAVE_CIVIL)  ? 'c' : '-',
-			  (x->flags & HAVE_TIME)   ? 't' : '-');
-}
+    char flags[5];
 
-static VALUE
-mk_inspect_raw(union DateData *x, const char *klass)
-{
-    if (simple_dat_p(x)) {
-	VALUE nth, flags;
-
-	RB_GC_GUARD(nth) = f_inspect(x->s.nth);
-	RB_GC_GUARD(flags) = mk_inspect_flags(x);
+    flags[0] = (x->flags & COMPLEX_DAT) ? 'C' : 'S';
+    flags[1] = (x->flags & HAVE_JD)     ? 'j' : '-';
+    flags[2] = (x->flags & HAVE_DF)     ? 'd' : '-';
+    flags[3] = (x->flags & HAVE_CIVIL)  ? 'c' : '-';
+    flags[4] = (x->flags & HAVE_TIME)   ? 't' : '-';
+    flags[5] = '\0';
 
+    if (simple_dat_p(x)) {
 	return rb_enc_sprintf(rb_usascii_encoding(),
-			      "#<%s: "
-			      "(%sth,%dj),+0s,%.0fj; "
+			      "#<%"PRIsVALUE": "
+			      "(%+"PRIsVALUE"th,%dj),+0s,%.0fj; "
 			      "%dy%dm%dd; %s>",
-			      klass ? klass : "?",
-			      RSTRING_PTR(nth), x->s.jd, x->s.sg,
+			      klass,
+			      x->s.nth, x->s.jd, x->s.sg,
 #ifndef USE_PACK
 			      x->s.year, x->s.mon, x->s.mday,
 #else
 			      x->s.year,
 			      EX_MON(x->s.pc), EX_MDAY(x->s.pc),
 #endif
-			      RSTRING_PTR(flags));
+			      flags);
     }
     else {
-	VALUE nth, sf, flags;
-
-	RB_GC_GUARD(nth) = f_inspect(x->c.nth);
-	RB_GC_GUARD(sf) = f_inspect(x->c.sf);
-	RB_GC_GUARD(flags) = mk_inspect_flags(x);
-
 	return rb_enc_sprintf(rb_usascii_encoding(),
-			      "#<%s: "
-			      "(%sth,%dj,%ds,%sn),%+ds,%.0fj; "
+			      "#<%"PRIsVALUE": "
+			      "(%+"PRIsVALUE"th,%dj,%ds,%+"PRIsVALUE"n),"
+			      "%+ds,%.0fj; "
 			      "%dy%dm%dd %dh%dm%ds; %s>",
-			      klass ? klass : "?",
-			      RSTRING_PTR(nth), x->c.jd, x->c.df,
-			      RSTRING_PTR(sf),
+			      klass,
+			      x->c.nth, x->c.jd, x->c.df, x->c.sf,
 			      x->c.of, x->c.sg,
 #ifndef USE_PACK
 			      x->c.year, x->c.mon, x->c.mday,
@@ -6488,7 +6474,7 @@ mk_inspect_raw(union DateData *x, const https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L6474
 			      EX_HOUR(x->c.pc), EX_MIN(x->c.pc),
 			      EX_SEC(x->c.pc),
 #endif
-			      RSTRING_PTR(flags));
+			      flags);
     }
 }
 
@@ -6496,23 +6482,18 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L6482
 d_lite_inspect_raw(VALUE self)
 {
     get_d1(self);
-    return mk_inspect_raw(dat, rb_obj_classname(self));
+    return mk_inspect_raw(dat, rb_obj_class(self));
 }
 #endif
 
 static VALUE
-mk_inspect(union DateData *x, const char *klass, const char *to_s)
+mk_inspect(union DateData *x, VALUE klass, VALUE to_s)
 {
-    VALUE jd, sf;
-
-    RB_GC_GUARD(jd) = f_inspect(m_real_jd(x));
-    RB_GC_GUARD(sf) = f_inspect(m_sf(x));
-
     return rb_enc_sprintf(rb_usascii_encoding(),
-			  "#<%s: %s ((%sj,%ds,%sn),%+ds,%.0fj)>",
-			  klass ? klass : "?",
-			  to_s ? to_s : "?",
-			  RSTRING_PTR(jd), m_df(x), RSTRING_PTR(sf),
+			  "#<%"PRIsVALUE": %"PRIsVALUE" "
+			  "((%+"PRIsVALUE"j,%ds,%+"PRIsVALUE"n),%+ds,%.0fj)>",
+			  klass, to_s,
+			  m_real_jd(x), m_df(x), m_sf(x),
 			  m_of(x), m_sg(x));
 }
 
@@ -6531,12 +6512,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L6512
 d_lite_inspect(VALUE self)
 {
     get_d1(self);
-    {
-	VALUE to_s;
-
-	RB_GC_GUARD(to_s) = f_to_s(self);
-	return mk_inspect(dat, rb_obj_classname(self), RSTRING_PTR(to_s));
-    }
+    return mk_inspect(dat, rb_obj_class(self), self);
 }
 
 #include <errno.h>

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

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