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

ruby-changes:44970

From: nobu <ko1@a...>
Date: Sat, 10 Dec 2016 17:54:44 +0900 (JST)
Subject: [ruby-changes:44970] nobu:r57043 (trunk): stringio.c: chomp CR

nobu	2016-12-10 17:54:40 +0900 (Sat, 10 Dec 2016)

  New Revision: 57043

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

  Log:
    stringio.c: chomp CR
    
    * ext/stringio/stringio.c (strio_getline): chomp CR not only LF,
      as well as String#chomp.

  Modified files:
    trunk/ext/stringio/stringio.c
    trunk/test/stringio/test_stringio.rb
Index: test/stringio/test_stringio.rb
===================================================================
--- test/stringio/test_stringio.rb	(revision 57042)
+++ test/stringio/test_stringio.rb	(revision 57043)
@@ -96,6 +96,21 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L96
     assert_equal("def", stringio.gets("", chomp: true))
   end
 
+  def test_gets_chomp_eol
+    assert_equal(nil, StringIO.new("").gets(chomp: true))
+    assert_equal("", StringIO.new("\r\n").gets(chomp: true))
+    assert_equal("a", StringIO.new("a\r\n").gets(chomp: true))
+    assert_equal("a", StringIO.new("a\r\nb\r\n").gets(chomp: true))
+    assert_equal("a", StringIO.new("a").gets(chomp: true))
+    assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true))
+    assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
+    assert_equal("abc\r\n\r\ndef", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
+    assert_equal("abc\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
+    stringio = StringIO.new("abc\r\n\r\ndef\r\n")
+    assert_equal("abc\r\n", stringio.gets("", chomp: true))
+    assert_equal("def", stringio.gets("", chomp: true))
+  end
+
   def test_readlines
     assert_equal([], StringIO.new("").readlines)
     assert_equal(["\n"], StringIO.new("\n").readlines)
@@ -497,6 +512,11 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L512
     assert_equal(["foo\nbar\n\n", "baz\n"], f.each("").to_a)
     f.rewind
     assert_equal(["foo\nbar\n", "baz"], f.each("", chomp: true).to_a)
+
+    f = StringIO.new("foo\r\nbar\r\n\r\nbaz\r\n")
+    assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a)
+    f.rewind
+    assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a)
   end
 
   def test_putc
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 57042)
+++ ext/stringio/stringio.c	(revision 57043)
@@ -1041,6 +1041,7 @@ static inline int https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1041
 chomp_newline_width(const char *s, const char *e)
 {
     if (e > s && *--e == '\n') {
+	if (e > s && *--e == '\r') return 2;
 	return 1;
     }
     return 0;
@@ -1071,7 +1072,8 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1072
     }
     else if ((n = RSTRING_LEN(str)) == 0) {
 	p = s;
-	while (*p == '\n') {
+	while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
+	    p += *p == '\r';
 	    if (++p == e) {
 		return Qnil;
 	    }
@@ -1083,6 +1085,11 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1085
 		w = (arg->chomp ? 1 : 0);
 		break;
 	    }
+	    else if (*p == '\r' && p < e && p[1] == '\n') {
+		e = p + 2;
+		w = (arg->chomp ? 2 : 0);
+		break;
+	    }
 	}
 	if (!w && arg->chomp) {
 	    w = chomp_newline_width(s, e);
@@ -1092,7 +1099,7 @@ strio_getline(struct getline_arg *arg, s https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1099
     else if (n == 1) {
 	if ((p = memchr(s, RSTRING_PTR(str)[0], e - s)) != 0) {
 	    e = p + 1;
-	    w = (arg->chomp ? 1 : 0);
+	    w = (arg->chomp ? (p > s && *(p-1) == '\r') + 1 : 0);
 	}
 	str = strio_substr(ptr, ptr->pos, e - s - w);
     }

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

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