summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-10 08:54:40 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-10 08:54:40 +0000
commit30540c567569d3486ccbf59b59d903d5778f04d5 ()
treedde00c0acb36d195dbea52b14403e9e07cd45233
parentf46a1377464d080ccbfb2b943d0fe7df18500e6a (diff)
stringio.c: chomp CR
* ext/stringio/stringio.c (strio_getline): chomp CR not only LF, as well as String#chomp. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57043 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ext/stringio/stringio.c11
-rw-r--r--test/stringio/test_stringio.rb20
2 files changed, 29 insertions, 2 deletions
@@ -1041,6 +1041,7 @@ static inline int
chomp_newline_width(const char *s, const char *e)
{
if (e > s && *--e == '\n') {
return 1;
}
return 0;
@@ -1071,7 +1072,8 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
}
else if ((n = RSTRING_LEN(str)) == 0) {
p = s;
- while (*p == '\n') {
if (++p == e) {
return Qnil;
}
@@ -1083,6 +1085,11 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
w = (arg->chomp ? 1 : 0);
break;
}
}
if (!w && arg->chomp) {
w = chomp_newline_width(s, e);
@@ -1092,7 +1099,7 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
else if (n == 1) {
if ((p = memchr(s, RSTRING_PTR(str)[0], e - s)) != 0) {
e = p + 1;
- w = (arg->chomp ? 1 : 0);
}
str = strio_substr(ptr, ptr->pos, e - s - w);
}
@@ -96,6 +96,21 @@ class TestStringIO < Test::Unit::TestCase
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::TestCase
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)
end
def test_putc