Skip to content

Commit

Permalink
Add support for gets(chomp: true).
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 13, 2024
1 parent 39eaa9f commit 8aa3849
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/openssl/buffering.rb
Expand Up @@ -229,7 +229,7 @@ def read_nonblock(maxlen, buf=nil, exception: true)
#
# Unlike IO#gets the separator must be provided if a limit is provided.

def gets(eol=$/, limit=nil)
def gets(eol=$/, limit=nil, chomp: false)
idx = @rbuffer.index(eol)
until @eof
break if idx
Expand All @@ -244,7 +244,11 @@ def gets(eol=$/, limit=nil)
if size && limit && limit >= 0
size = [size, limit].min
end
consume_rbuff(size)
line = consume_rbuff(size)
if chomp && line
line.chomp!(eol)
end
line
end

##
Expand Down
11 changes: 11 additions & 0 deletions test/openssl/test_pair.rb
Expand Up @@ -115,6 +115,17 @@ def test_gets
}
end

def test_gets_chomp
ssl_pair {|s1, s2|
s1 << "line1\r\nline2\r\nline3\r\n"
s1.close

assert_equal("line1", s2.gets("\r\n", chomp: true))
assert_equal("line2\r\n", s2.gets("\r\n", chomp: false))
assert_equal("line3", s2.gets(chomp: true))
}
end

def test_gets_eof_limit
ssl_pair {|s1, s2|
s1.write("hello")
Expand Down

0 comments on commit 8aa3849

Please sign in to comment.