Skip to content

Commit 8aa3849

Browse files
committed
Add support for gets(chomp: true).
1 parent 39eaa9f commit 8aa3849

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/openssl/buffering.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def read_nonblock(maxlen, buf=nil, exception: true)
229229
#
230230
# Unlike IO#gets the separator must be provided if a limit is provided.
231231

232-
def gets(eol=$/, limit=nil)
232+
def gets(eol=$/, limit=nil, chomp: false)
233233
idx = @rbuffer.index(eol)
234234
until @eof
235235
break if idx
@@ -244,7 +244,11 @@ def gets(eol=$/, limit=nil)
244244
if size && limit && limit >= 0
245245
size = [size, limit].min
246246
end
247-
consume_rbuff(size)
247+
line = consume_rbuff(size)
248+
if chomp && line
249+
line.chomp!(eol)
250+
end
251+
line
248252
end
249253

250254
##

test/openssl/test_pair.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ def test_gets
115115
}
116116
end
117117

118+
def test_gets_chomp
119+
ssl_pair {|s1, s2|
120+
s1 << "line1\r\nline2\r\nline3\r\n"
121+
s1.close
122+
123+
assert_equal("line1", s2.gets("\r\n", chomp: true))
124+
assert_equal("line2\r\n", s2.gets("\r\n", chomp: false))
125+
assert_equal("line3", s2.gets(chomp: true))
126+
}
127+
end
128+
118129
def test_gets_eof_limit
119130
ssl_pair {|s1, s2|
120131
s1.write("hello")

0 commit comments

Comments
 (0)