[ruby/stringio] Fix handling of chomp with paragraph separator
Try to mirror IO behavior, where chomp takes out the entire paragraph separators between entries, but does not chomp a single line separator at the end of the string. Partially Fixes [Bug #18768] https://github.com/ruby/stringio/commit/a83ddbb7f0
This commit is contained in:
parent
7e3920f0d9
commit
609d73e892
@ -1204,6 +1204,7 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
|
|||||||
str = strio_substr(ptr, ptr->pos, e - s - w, enc);
|
str = strio_substr(ptr, ptr->pos, e - s - w, enc);
|
||||||
}
|
}
|
||||||
else if ((n = RSTRING_LEN(str)) == 0) {
|
else if ((n = RSTRING_LEN(str)) == 0) {
|
||||||
|
const char *paragraph_end = NULL;
|
||||||
p = s;
|
p = s;
|
||||||
while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
|
while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
|
||||||
p += *p == '\r';
|
p += *p == '\r';
|
||||||
@ -1213,19 +1214,21 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
|
|||||||
}
|
}
|
||||||
s = p;
|
s = p;
|
||||||
while ((p = memchr(p, '\n', e - p)) && (p != e)) {
|
while ((p = memchr(p, '\n', e - p)) && (p != e)) {
|
||||||
if (*++p == '\n') {
|
p++;
|
||||||
e = p + 1;
|
if (!((p < e && *p == '\n') ||
|
||||||
w = (arg->chomp ? 1 : 0);
|
(p + 1 < e && *p == '\r' && *(p+1) == '\n'))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
paragraph_end = p - ((*(p-2) == '\r') ? 2 : 1);
|
||||||
|
while ((p < e && *p == '\n') ||
|
||||||
|
(p + 1 < e && *p == '\r' && *(p+1) == '\n')) {
|
||||||
|
p += (*p == '\r') ? 2 : 1;
|
||||||
|
}
|
||||||
|
e = p;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (*p == '\r' && p < e && p[1] == '\n') {
|
if (arg->chomp && paragraph_end) {
|
||||||
e = p + 2;
|
w = e - paragraph_end;
|
||||||
w = (arg->chomp ? 2 : 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!w && arg->chomp) {
|
|
||||||
w = chomp_newline_width(s, e);
|
|
||||||
}
|
}
|
||||||
str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s - w, enc);
|
str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s - w, enc);
|
||||||
}
|
}
|
||||||
|
@ -93,10 +93,10 @@ class TestStringIO < Test::Unit::TestCase
|
|||||||
assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
|
assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
|
||||||
assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
|
assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
|
||||||
assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
|
assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
|
||||||
assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
|
assert_equal("abc", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
|
||||||
stringio = StringIO.new("abc\n\ndef\n")
|
stringio = StringIO.new("abc\n\ndef\n")
|
||||||
assert_equal("abc\n", stringio.gets("", chomp: true))
|
assert_equal("abc", stringio.gets("", chomp: true))
|
||||||
assert_equal("def", stringio.gets("", chomp: true))
|
assert_equal("def\n", stringio.gets("", chomp: true))
|
||||||
|
|
||||||
assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true))
|
assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true))
|
||||||
end
|
end
|
||||||
@ -110,10 +110,10 @@ class TestStringIO < Test::Unit::TestCase
|
|||||||
assert_equal("a", StringIO.new("a\r\nb").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", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
|
||||||
assert_equal("abc\r\n\r\ndef\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
|
assert_equal("abc\r\n\r\ndef\r\n", 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))
|
assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
|
||||||
stringio = StringIO.new("abc\r\n\r\ndef\r\n")
|
stringio = StringIO.new("abc\r\n\r\ndef\r\n")
|
||||||
assert_equal("abc\r\n", stringio.gets("", chomp: true))
|
assert_equal("abc", stringio.gets("", chomp: true))
|
||||||
assert_equal("def", stringio.gets("", chomp: true))
|
assert_equal("def\r\n", stringio.gets("", chomp: true))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_readlines
|
def test_readlines
|
||||||
@ -596,15 +596,15 @@ class TestStringIO < Test::Unit::TestCase
|
|||||||
assert_equal(["foo\n", "bar\n", "baz\n"], f.each.to_a)
|
assert_equal(["foo\n", "bar\n", "baz\n"], f.each.to_a)
|
||||||
f.rewind
|
f.rewind
|
||||||
assert_equal(["foo", "bar", "baz"], f.each(chomp: true).to_a)
|
assert_equal(["foo", "bar", "baz"], f.each(chomp: true).to_a)
|
||||||
f = StringIO.new("foo\nbar\n\nbaz\n")
|
f = StringIO.new("foo\nbar\n\n\nbaz\n")
|
||||||
assert_equal(["foo\nbar\n\n", "baz\n"], f.each("").to_a)
|
assert_equal(["foo\nbar\n\n\n", "baz\n"], f.each("").to_a)
|
||||||
f.rewind
|
f.rewind
|
||||||
assert_equal(["foo\nbar\n", "baz"], f.each("", chomp: true).to_a)
|
assert_equal(["foo\nbar", "baz\n"], f.each("", chomp: true).to_a)
|
||||||
|
|
||||||
f = StringIO.new("foo\r\nbar\r\n\r\nbaz\r\n")
|
f = StringIO.new("foo\r\nbar\r\n\r\n\r\nbaz\r\n")
|
||||||
assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a)
|
assert_equal(["foo\r\nbar\r\n\r\n\r\n", "baz\r\n"], f.each("").to_a)
|
||||||
f.rewind
|
f.rewind
|
||||||
assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a)
|
assert_equal(["foo\r\nbar", "baz\r\n"], f.each("", chomp: true).to_a)
|
||||||
|
|
||||||
f = StringIO.new("abc\n\ndef\n")
|
f = StringIO.new("abc\n\ndef\n")
|
||||||
assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a)
|
assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user