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

ruby-changes:43557

From: nobu <ko1@a...>
Date: Mon, 11 Jul 2016 16:01:17 +0900 (JST)
Subject: [ruby-changes:43557] nobu:r55629 (trunk): stringio.c: convert arguments just once

nobu	2016-07-11 16:00:58 +0900 (Mon, 11 Jul 2016)

  New Revision: 55629

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

  Log:
    stringio.c: convert arguments just once
    
    * ext/stringio/stringio.c (strio_each, strio_readlines): convert
      arguments just once before reading, instead of conversions for
      each lines, as r55603.

  Modified files:
    trunk/ChangeLog
    trunk/ext/stringio/stringio.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55628)
+++ ChangeLog	(revision 55629)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jul 11 16:00:56 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/stringio/stringio.c (strio_each, strio_readlines): convert
+	  arguments just once before reading, instead of conversions for
+	  each lines, as r55603.
+
 Sun Jul 10 19:53:41 2016  Martin Duerst  <duerst@i...>
 
 	* enc/iso_8859_10.c, test/ruby/enc/test_case_comprehensive.rb:
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 55628)
+++ ext/stringio/stringio.c	(revision 55629)
@@ -982,12 +982,16 @@ bm_search(const char *little, long llen, https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L982
     return -1;
 }
 
-static VALUE
-strio_getline(int argc, VALUE *argv, struct StringIO *ptr)
+struct getline_arg {
+    VALUE rs;
+    long limit;
+};
+
+static struct getline_arg *
+prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
 {
-    const char *s, *e, *p;
-    long n, limit = 0;
     VALUE str, lim;
+    long limit = -1;
 
     rb_scan_args(argc, argv, "02", &str, &lim);
     switch (argc) {
@@ -1000,7 +1004,6 @@ strio_getline(int argc, VALUE *argv, str https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1004
 	    VALUE tmp = rb_check_string_type(str);
 	    if (NIL_P(tmp)) {
 		limit = NUM2LONG(str);
-		if (limit == 0) return rb_str_new(0,0);
 		str = rb_rs;
 	    }
 	    else {
@@ -1014,6 +1017,17 @@ strio_getline(int argc, VALUE *argv, str https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1017
 	if (!NIL_P(lim)) limit = NUM2LONG(lim);
 	break;
     }
+    arg->rs = str;
+    arg->limit = limit;
+    return arg;
+}
+
+static VALUE
+strio_getline(struct getline_arg *arg, struct StringIO *ptr)
+{
+    const char *s, *e, *p;
+    long n, limit = arg->limit;
+    VALUE str = arg->rs;
 
     if (ptr->pos >= (n = RSTRING_LEN(ptr->string))) {
 	return Qnil;
@@ -1086,8 +1100,14 @@ strio_getline(int argc, VALUE *argv, str https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1100
 static VALUE
 strio_gets(int argc, VALUE *argv, VALUE self)
 {
-    VALUE str = strio_getline(argc, argv, readable(self));
+    struct getline_arg arg;
+    VALUE str;
+
+    if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
+	return rb_str_new(0, 0);
+    }
 
+    str = strio_getline(&arg, readable(self));
     rb_lastline_set(str);
     return str;
 }
@@ -1126,16 +1146,16 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1146
 strio_each(int argc, VALUE *argv, VALUE self)
 {
     VALUE line;
+    struct getline_arg arg;
 
     StringIO(self);
     RETURN_ENUMERATOR(self, argc, argv);
 
-    if (argc > 0 && !NIL_P(argv[argc-1]) && NIL_P(rb_check_string_type(argv[argc-1])) &&
-	NUM2LONG(argv[argc-1]) == 0) {
+    if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
 	rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
     }
 
-    while (!NIL_P(line = strio_getline(argc, argv, readable(self)))) {
+    while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
 	rb_yield(line);
     }
     return self;
@@ -1165,15 +1185,15 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1185
 strio_readlines(int argc, VALUE *argv, VALUE self)
 {
     VALUE ary, line;
+    struct getline_arg arg;
 
     StringIO(self);
     ary = rb_ary_new();
-    if (argc > 0 && !NIL_P(argv[argc-1]) && NIL_P(rb_check_string_type(argv[argc-1])) &&
-	NUM2LONG(argv[argc-1]) == 0) {
+    if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
 	rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
     }
 
-    while (!NIL_P(line = strio_getline(argc, argv, readable(self)))) {
+    while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
 	rb_ary_push(ary, line);
     }
     return ary;

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

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