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

ruby-changes:46593

From: nobu <ko1@a...>
Date: Sun, 14 May 2017 09:21:05 +0900 (JST)
Subject: [ruby-changes:46593] nobu:r58709 (trunk): string.c: cut down intermediate string

nobu	2017-05-14 09:21:00 +0900 (Sun, 14 May 2017)

  New Revision: 58709

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

  Log:
    string.c: cut down intermediate string
    
    * string.c (rb_external_str_new_with_enc): cut down intermediate
      string for conversion source, by appending with conversion.

  Modified files:
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 58708)
+++ string.c	(revision 58709)
@@ -997,10 +997,37 @@ rb_str_conv_enc(VALUE str, rb_encoding * https://github.com/ruby/ruby/blob/trunk/string.c#L997
 VALUE
 rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
 {
+    rb_encoding *ienc;
     VALUE str;
+    const int eidx = rb_enc_to_index(eenc);
 
-    str = rb_tainted_str_new_with_enc(ptr, len, eenc);
-    return rb_external_str_with_enc(str, eenc);
+    /* ASCII-8BIT case, no conversion */
+    if ((eidx == rb_ascii8bit_encindex()) ||
+	(eidx == rb_usascii_encindex() && search_nonascii(ptr, ptr + len))) {
+	return rb_tainted_str_new(ptr, len);
+    }
+    /* no default_internal or same encoding, no conversion */
+    ienc = rb_default_internal_encoding();
+    if (!ienc || eenc == ienc) {
+	return rb_tainted_str_new_with_enc(ptr, len, eenc);
+    }
+    /* ASCII compatible, and ASCII only string, no conversion in
+     * default_internal */
+    if ((eidx == rb_ascii8bit_encindex()) ||
+	(eidx == rb_usascii_encindex()) ||
+	(rb_enc_asciicompat(eenc) && !search_nonascii(ptr, ptr + len))) {
+	return rb_tainted_str_new_with_enc(ptr, len, ienc);
+    }
+    /* convert from the given encoding to default_internal */
+    str = rb_tainted_str_new_with_enc(NULL, 0, ienc);
+    /* when the conversion failed for some reason, just ignore the
+     * default_internal and result in the given encoding as-is. */
+    if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, eenc, 0, Qnil))) {
+	STR_SET_LEN(str, 0);
+	rb_enc_associate(str, eenc);
+	rb_str_cat(str, ptr, len);
+    }
+    return str;
 }
 
 VALUE

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

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