Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,17 @@ private IRubyObject getline(ThreadContext context, final IRubyObject rs, int lim
int p;
int w = 0;

if (rs != context.nil && ((RubyString) rs).size() == 0) {
// paragraph mode: skip leading newlines before applying the limit
p = s;
while (stringBytes[p] == '\n') {
if (++p == e) {
ptr.pos = e - begin;
return context.nil;
}
}
s = p;
}
if (limit > 0 && s + limit < e) {
e = getEncoding().rightAdjustCharHead(stringBytes, s, s + limit, e);
}
Expand All @@ -906,12 +917,6 @@ private IRubyObject getline(ThreadContext context, final IRubyObject rs, int lim
} else if ((n = ((RubyString) rs).size()) == 0) {
int paragraph_end = 0;
p = s;
while (stringBytes[p] == '\n') {
if (++p == e) {
return context.nil;
}
}
s = p;
while ((p = Helpers.memchr(stringBytes, p, '\n', e - p)) != -1 && (p != e)) {
p++;
if (!((p < e && stringBytes[p] == '\n') ||
Expand Down
19 changes: 12 additions & 7 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,18 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
s = RSTRING_PTR(ptr->string);
e = s + RSTRING_LEN(ptr->string);
s += ptr->pos;
if (!NIL_P(str) && RSTRING_LEN(str) == 0) {
/* paragraph mode: skip leading newlines before applying the limit */
p = s;
while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
p += *p == '\r';
if (++p == e) {
ptr->pos = e - RSTRING_PTR(ptr->string);
return Qnil;
}
}
s = p;
}
if (limit > 0 && (size_t)limit < (size_t)(e - s)) {
e = rb_enc_right_char_head(s, s + limit, e, get_enc(ptr));
}
Expand All @@ -1423,13 +1435,6 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
else if ((n = RSTRING_LEN(str)) == 0) {
const char *paragraph_end = NULL;
p = s;
while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
p += *p == '\r';
if (++p == e) {
return Qnil;
}
}
s = p;
while ((p = memchr(p, '\n', e - p)) && (p != e)) {
p++;
if (!((p < e && *p == '\n') ||
Expand Down
20 changes: 20 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ def test_gets
assert_string("", Encoding::UTF_8, StringIO.new("foo").gets(0))
end

def test_gets_paragraph_with_limit
stringio = StringIO.new("a\n\n\n\nb\n\n\nc")
assert_equal("a", stringio.gets("", 1))
assert_equal("b", stringio.gets("", 1))
assert_equal("c", stringio.gets("", 1))
assert_nil(stringio.gets("", 1))

stringio = StringIO.new("a\n\n\n\nb")
assert_equal("a\n\n", stringio.gets("", 3))
assert_equal("b", stringio.gets("", 3))
assert_nil(stringio.gets("", 3))
end

def test_gets_paragraph_only_newlines
stringio = StringIO.new("\n\n\n")
assert_nil(stringio.gets(""))
assert_equal(3, stringio.pos)
assert_predicate(stringio, :eof?)
end

def test_gets_utf_16
stringio = StringIO.new("line1\nline2\nline3\n".encode("utf-16le"))
assert_equal("line1\n".encode("utf-16le"), stringio.gets)
Expand Down