aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-30 05:54:59 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-30 05:54:59 +0000
commit3a6a54f9d83c7f5f8c83fefd5958fc69797d5061 (patch)
treefe1c41773aac9b2704ec8ad5740f54cad6d79cff
parent3e81f893f7b5674a4c65177d0f332d531cdf28f2 (diff)
downloadruby-3a6a54f9d83c7f5f8c83fefd5958fc69797d5061.tar.gz
stringio.c: share strings
* ext/stringio/stringio.c (enc_subseq): share the return value and the buffer as possible. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55210 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/stringio/stringio.c20
2 files changed, 20 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 616b88549b..aee069a809 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon May 30 14:54:58 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* string.c (str_substr, rb_str_aref): refactor not to create
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 368dcfec4b..7dd6d4f827 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -96,6 +96,14 @@ get_strio(VALUE self)
}
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 pos, long len)
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)
{
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);
}
/*