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

ruby-changes:43136

From: nobu <ko1@a...>
Date: Mon, 30 May 2016 14:55:05 +0900 (JST)
Subject: [ruby-changes:43136] nobu:r55210 (trunk): stringio.c: share strings

nobu	2016-05-30 14:54:59 +0900 (Mon, 30 May 2016)

  New Revision: 55210

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

  Log:
    stringio.c: share strings
    
    * ext/stringio/stringio.c (enc_subseq): share the return value and
      the buffer as possible.

  Modified files:
    trunk/ChangeLog
    trunk/ext/stringio/stringio.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55209)
+++ ChangeLog	(revision 55210)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon May 30 14:54:58 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/stringio/stringio.c (enc_subseq): share the return value and
+	  the buffer as possible.
+
 Mon May 30 14:50:25 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* string.c (str_substr, rb_str_aref): refactor not to create
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 55209)
+++ ext/stringio/stringio.c	(revision 55210)
@@ -96,6 +96,14 @@ get_strio(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L96
 }
 
 static VALUE
+enc_subseq(VALUE str, long pos, long len, rb_encoding *enc)
+{
+    str = rb_str_subseq(str, pos, len);
+    rb_enc_associate(str, enc);
+    return str;
+}
+
+static VALUE
 strio_substr(struct StringIO *ptr, long pos, long len)
 {
     VALUE str = ptr->string;
@@ -105,7 +113,7 @@ strio_substr(struct StringIO *ptr, long https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L113
     if (len > rlen) len = rlen;
     if (len < 0) len = 0;
     if (len == 0) return rb_str_new(0,0);
-    return rb_enc_str_new(RSTRING_PTR(str)+pos, len, enc);
+    return enc_subseq(str, pos, len, enc);
 }
 
 #define StringIO(obj) get_strio(obj)
@@ -690,16 +698,18 @@ strio_getc(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L698
 {
     struct StringIO *ptr = readable(self);
     rb_encoding *enc = get_enc(ptr);
+    VALUE str = ptr->string;
+    long pos = ptr->pos;
     int len;
     char *p;
 
-    if (ptr->pos >= RSTRING_LEN(ptr->string)) {
+    if (pos >= RSTRING_LEN(str)) {
 	return Qnil;
     }
-    p = RSTRING_PTR(ptr->string)+ptr->pos;
-    len = rb_enc_mbclen(p, RSTRING_END(ptr->string), enc);
+    p = RSTRING_PTR(str)+pos;
+    len = rb_enc_mbclen(p, RSTRING_END(str), enc);
     ptr->pos += len;
-    return rb_enc_str_new(p, len, enc);
+    return enc_subseq(str, pos, len, enc);
 }
 
 /*

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

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