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

ruby-changes:49397

From: nobu <ko1@a...>
Date: Fri, 29 Dec 2017 21:19:09 +0900 (JST)
Subject: [ruby-changes:49397] nobu:r61513 (trunk): string.c: chomp rs at the end

nobu	2017-12-29 21:19:03 +0900 (Fri, 29 Dec 2017)

  New Revision: 61513

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

  Log:
    string.c: chomp rs at the end
    
    * string.c (rb_str_enumerate_lines): should chomp record separator
      only, but not a newline, at the end of the receiver as well as
      middle, if the separator is given.
      [ruby-core:84552] [Bug #14257]

  Modified files:
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 61512)
+++ test/ruby/test_string.rb	(revision 61513)
@@ -1119,6 +1119,10 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1119
     res = []
     S("\r\n").each_line(chomp: true) {|x| res << x}
     assert_equal([S("")], res)
+
+    res = []
+    S("a\n b\n").each_line(" ", chomp: true) {|x| res << x}
+    assert_equal([S("a\n"), S("b\n")], res)
   end
 
   def test_lines
Index: string.c
===================================================================
--- string.c	(revision 61512)
+++ string.c	(revision 61513)
@@ -8012,7 +8012,12 @@ rb_str_enumerate_lines(int argc, VALUE * https://github.com/ruby/ruby/blob/trunk/string.c#L8012
 
     if (subptr != pend) {
 	if (chomp) {
-	    pend = chomp_newline(subptr, pend, enc);
+	    if (rsnewline) {
+		pend = chomp_newline(subptr, pend, enc);
+	    }
+	    else if (memcmp(pend - rslen, rsptr, rslen) == 0) {
+		pend -= rslen;
+	    }
 	}
 	line = rb_str_subseq(str, subptr - ptr, pend - subptr);
 	ENUM_ELEM(ary, line);

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

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