Skip to content

Commit a1040f0

Browse files
committed
Clean up some code duplication in IO::EachReader.
1 parent 18e4ae0 commit a1040f0

File tree

1 file changed

+16
-35
lines changed
  • src/main/ruby/truffleruby/core

1 file changed

+16
-35
lines changed

src/main/ruby/truffleruby/core/io.rb

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,13 +1162,7 @@ def read_to_separator
11621162
str.clear
11631163
end
11641164

1165-
s = IO.read_encode(@io, s)
1166-
1167-
s.chomp!(@separator) if @chomp
1168-
$. = @io.__send__(:increment_lineno)
1169-
@buffer.discard @skip if @skip
1170-
1171-
yield s
1165+
yield prepare_read_string(s)
11721166

11731167
next
11741168
else
@@ -1210,10 +1204,7 @@ def read_to_separator
12101204
@buffer.put_back(str.byteslice(offset, str.bytesize - offset))
12111205
end
12121206

1213-
res = IO.read_encode(@io, str.byteslice(0, offset))
1214-
res.chomp!(@separator) if @chomp
1215-
$. = @io.__send__(:increment_lineno)
1216-
@buffer.discard @skip if @skip
1207+
res = prepare_read_string(str.byteslice(0, offset))
12171208

12181209
str.clear
12191210
last_scan_end = 0
@@ -1227,7 +1218,7 @@ def read_to_separator
12271218

12281219
str << @buffer.shift
12291220
str.chomp!(@separator) if @chomp
1230-
yield_string(str) { |y| yield y }
1221+
yield prepare_read_string(str) unless str.empty?
12311222
end
12321223

12331224
# method B, E
@@ -1245,27 +1236,16 @@ def read_to_separator_with_limit
12451236
bytes = Primitive.min(count, wanted)
12461237
str << @buffer.shift(bytes)
12471238

1248-
str = IO.read_encode(@io, str)
1249-
1250-
str.chomp!(@separator) if @chomp
1251-
$. = @io.__send__(:increment_lineno)
1252-
@buffer.discard @skip if @skip
1253-
1254-
yield str
1239+
yield prepare_read_string(str)
12551240

12561241
str = +''
12571242
wanted = limit
12581243
else
12591244
if wanted < available
12601245
str << @buffer.shift(wanted)
1261-
12621246
str = @buffer.read_to_char_boundary(@io, str)
12631247

1264-
str.chomp!(@separator) if @chomp
1265-
$. = @io.__send__(:increment_lineno)
1266-
@buffer.discard @skip if @skip
1267-
1268-
yield str
1248+
yield prepare_read_string(str)
12691249

12701250
str = +''
12711251
wanted = limit
@@ -1276,8 +1256,7 @@ def read_to_separator_with_limit
12761256
end
12771257
end
12781258

1279-
str.chomp!(@separator) if @chomp
1280-
yield_string(str) { |s| yield s }
1259+
yield prepare_read_string(str) unless str.empty?
12811260
end
12821261

12831262
# Method G
@@ -1293,7 +1272,7 @@ def read_all
12931272
end
12941273

12951274
str.chomp!(DEFAULT_RECORD_SEPARATOR) if @chomp
1296-
yield_string(str) { |s| yield s }
1275+
yield prepare_read_string(str) unless str.empty?
12971276
end
12981277

12991278
# Method H
@@ -1319,15 +1298,17 @@ def read_to_limit
13191298
end
13201299
end
13211300

1322-
yield_string(str) { |s| yield s }
1301+
yield prepare_read_string(str) unless str.empty?
13231302
end
13241303

1325-
def yield_string(str)
1326-
unless str.empty?
1327-
str = IO.read_encode(@io, str)
1328-
$. = @io.__send__(:increment_lineno)
1329-
yield str
1330-
end
1304+
def prepare_read_string(str)
1305+
s = IO.read_encode(@io, str)
1306+
1307+
s.chomp!(@separator) if @chomp
1308+
$. = @io.__send__(:increment_lineno)
1309+
@buffer.discard @skip if @skip
1310+
1311+
s
13311312
end
13321313
end
13331314

0 commit comments

Comments
 (0)