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

ruby-changes:28204

From: nagachika <ko1@a...>
Date: Fri, 12 Apr 2013 04:13:44 +0900 (JST)
Subject: [ruby-changes:28204] nagachika:r40256 (ruby_2_0_0): merge revision(s) 39822,39856: [Backport #8141]

nagachika	2013-04-12 04:13:29 +0900 (Fri, 12 Apr 2013)

  New Revision: 40256

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

  Log:
    merge revision(s) 39822,39856: [Backport #8141]
    
    * string.c (rb_str_conv_enc_opts): convert with one converter, instead
      of re-creating converters for each buffer expansion.

  Modified directories:
    branches/ruby_2_0_0/
  Modified files:
    branches/ruby_2_0_0/ChangeLog
    branches/ruby_2_0_0/string.c
    branches/ruby_2_0_0/version.h

Index: ruby_2_0_0/ChangeLog
===================================================================
--- ruby_2_0_0/ChangeLog	(revision 40255)
+++ ruby_2_0_0/ChangeLog	(revision 40256)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1
+Fri Apr 12 04:12:42 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_conv_enc_opts): convert with one converter, instead
+	  of re-creating converters for each buffer expansion.
+
 Fri Apr 12 03:48:25 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/mkmf.rb (find_library): fix to format message.
Index: ruby_2_0_0/string.c
===================================================================
--- ruby_2_0_0/string.c	(revision 40255)
+++ ruby_2_0_0/string.c	(revision 40256)
@@ -490,12 +490,15 @@ RUBY_ALIAS_FUNCTION(rb_tainted_str_new2( https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/string.c#L490
 VALUE
 rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
 {
+    extern VALUE rb_cEncodingConverter;
     rb_econv_t *ec;
     rb_econv_result_t ret;
-    long len;
+    long len, olen;
+    VALUE econv_wrapper;
     VALUE newstr;
-    const unsigned char *sp;
-    unsigned char *dp;
+    const unsigned char *start, *sp;
+    unsigned char *dest, *dp;
+    size_t converted_output = 0;
 
     if (!to) return str;
     if (!from) from = rb_enc_get(str);
@@ -511,23 +514,39 @@ rb_str_conv_enc_opts(VALUE str, rb_encod https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/string.c#L514
 
     len = RSTRING_LEN(str);
     newstr = rb_str_new(0, len);
+    olen = len;
 
-  retry:
+    econv_wrapper = rb_obj_alloc(rb_cEncodingConverter);
+    RBASIC(econv_wrapper)->klass = 0;
     ec = rb_econv_open_opts(from->name, to->name, ecflags, ecopts);
     if (!ec) return str;
+    DATA_PTR(econv_wrapper) = ec;
 
     sp = (unsigned char*)RSTRING_PTR(str);
-    dp = (unsigned char*)RSTRING_PTR(newstr);
-    ret = rb_econv_convert(ec, &sp, (unsigned char*)RSTRING_END(str),
-			   &dp, (unsigned char*)RSTRING_END(newstr), 0);
+    start = sp;
+    while ((dest = (unsigned char*)RSTRING_PTR(newstr)),
+	   (dp = dest + converted_output),
+	   (ret = rb_econv_convert(ec, &sp, start + len, &dp, dest + olen, 0)),
+	   ret == econv_destination_buffer_full) {
+	/* destination buffer short */
+	size_t converted_input = sp - start;
+	size_t rest = len - converted_input;
+	converted_output = dp - dest;
+	rb_str_set_len(newstr, converted_output);
+	if (converted_input && converted_output &&
+	    rest < (LONG_MAX / converted_output)) {
+	    rest = (rest * converted_output) / converted_input;
+	}
+	else {
+	    rest = olen;
+	}
+	olen += rest < 2 ? 2 : rest;
+	rb_str_resize(newstr, olen);
+    }
+    DATA_PTR(econv_wrapper) = 0;
     rb_econv_close(ec);
+    rb_gc_force_recycle(econv_wrapper);
     switch (ret) {
-      case econv_destination_buffer_full:
-	/* destination buffer short */
-	len = len < 2 ? 2 : len * 2;
-	rb_str_resize(newstr, len);
-	goto retry;
-
       case econv_finished:
 	len = dp - (unsigned char*)RSTRING_PTR(newstr);
 	rb_str_set_len(newstr, len);
Index: ruby_2_0_0/version.h
===================================================================
--- ruby_2_0_0/version.h	(revision 40255)
+++ ruby_2_0_0/version.h	(revision 40256)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1
 #define RUBY_VERSION "2.0.0"
 #define RUBY_RELEASE_DATE "2013-04-12"
-#define RUBY_PATCHLEVEL 117
+#define RUBY_PATCHLEVEL 118
 
 #define RUBY_RELEASE_YEAR 2013
 #define RUBY_RELEASE_MONTH 4

Property changes on: ruby_2_0_0
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r39822,39856


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

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