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

ruby-changes:30199

From: nobu <ko1@a...>
Date: Tue, 30 Jul 2013 17:16:32 +0900 (JST)
Subject: [ruby-changes:30199] nobu:r42251 (trunk): sprintf.c: QUOTE flag

nobu	2013-07-30 17:16:20 +0900 (Tue, 30 Jul 2013)

  New Revision: 42251

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42251

  Log:
    sprintf.c: QUOTE flag
    
    * sprintf.c (ruby__sfvextra): add QUOTE flag to escape unprintable
      characters.

  Modified files:
    trunk/ChangeLog
    trunk/ext/-test-/printf/printf.c
    trunk/sprintf.c
    trunk/test/-ext-/test_printf.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42250)
+++ ChangeLog	(revision 42251)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jul 30 17:16:15 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* sprintf.c (ruby__sfvextra): add QUOTE flag to escape unprintable
+	  characters.
+
+Tue Jul 30 17:09:51 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* sprintf.c (ruby__sfvextra): add QUOTE flag to escape unprintable
+	  characters.
+
 Tue Jul 30 11:00:52 2013  Zachary Scott  <e@z...>
 
 	* ext/curses/extconf.rb: [DOC] nodoc to reduce Object pollution
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 42250)
+++ sprintf.c	(revision 42251)
@@ -1171,6 +1171,7 @@ ruby__sfvextra(rb_printf_buffer *fp, siz https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1171
     }
     else {
 	value = rb_obj_as_string(value);
+	if (sign == ' ') value = QUOTE(value);
     }
     enc = rb_enc_compatible(result, value);
     if (enc) {
Index: ext/-test-/printf/printf.c
===================================================================
--- ext/-test-/printf/printf.c	(revision 42250)
+++ ext/-test-/printf/printf.c	(revision 42251)
@@ -21,6 +21,12 @@ printf_test_v(VALUE self, VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/-test-/printf/printf.c#L21
     return rb_enc_sprintf(rb_usascii_encoding(), "{%+"PRIsVALUE"}", obj);
 }
 
+static VALUE
+printf_test_q(VALUE self, VALUE obj)
+{
+    return rb_enc_sprintf(rb_usascii_encoding(), "[% "PRIsVALUE"]", obj);
+}
+
 void
 Init_printf(void)
 {
@@ -28,4 +34,5 @@ Init_printf(void) https://github.com/ruby/ruby/blob/trunk/ext/-test-/printf/printf.c#L34
     rb_define_singleton_method(m, "i", printf_test_i, 1);
     rb_define_singleton_method(m, "s", printf_test_s, 1);
     rb_define_singleton_method(m, "v", printf_test_v, 1);
+    rb_define_singleton_method(m, "q", printf_test_q, 1);
 }
Index: test/-ext-/test_printf.rb
===================================================================
--- test/-ext-/test_printf.rb	(revision 42250)
+++ test/-ext-/test_printf.rb	(revision 42251)
@@ -22,6 +22,12 @@ class Test_SPrintf < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_printf.rb#L22
     assert_equal("{<#{self.class}:#{object_id}>}", Bug::Printf.v(self))
   end
 
+  def test_quote
+    assert_equal('["\n"]', Bug::Printf.q("\n"))
+    assert_equal('[aaa]', Bug::Printf.q('aaa'))
+    assert_equal('[a a]', Bug::Printf.q('a a'))
+  end
+
   def test_encoding
     def self.to_s
       "\u{3042 3044 3046 3048 304a}"

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

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