Skip to content

Commit 81209ea

Browse files
committed
Small code cleanup and spec for StringScanner#scan_internal size.
1 parent c8ae854 commit 81209ea

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/truffle/strscan.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,23 +308,30 @@ def peep(len)
308308
peek len
309309
end
310310

311-
def scan_check_args(pattern, headonly)
311+
private def scan_check_args(pattern, headonly)
312312
unless pattern.kind_of? Regexp
313313
raise TypeError, "bad pattern argument: #{pattern.inspect}"
314314
end
315315
raise ArgumentError, 'uninitialized StringScanner object' unless @string
316316
end
317317

318-
def scan_internal(pattern, advance_pos, getstr, headonly)
318+
# This method is kept very small so that it should fit within 100
319+
# AST nodes and can be split. This is done to avoid indirect calls
320+
# to TRegex.
321+
private def scan_internal(pattern, advance_pos, getstr, headonly)
319322
scan_check_args(pattern, headonly)
320323

321324
md = Truffle::RegexpOperations.match_in_region pattern, @string, pos, @string.bytesize, headonly, pos
322-
Primitive.matchdata_fixup_positions(md, pos) if md
323-
@match = md
324-
scan_internal2(advance_pos, getstr)
325+
if md
326+
Primitive.matchdata_fixup_positions(md, pos)
327+
@match = md
328+
scan_internal_set_pos_and_str(advance_pos, getstr)
329+
else
330+
@match = nil
331+
end
325332
end
326333

327-
def scan_internal2(advance_pos, getstr)
334+
private def scan_internal_set_pos_and_str(advance_pos, getstr)
328335
return nil unless @match
329336

330337
fin = Primitive.match_data_byte_end(@match, 0)
@@ -337,6 +344,5 @@ def scan_internal2(advance_pos, getstr)
337344

338345
@string.byteslice(@prev_pos, width)
339346
end
340-
private :scan_internal, :scan_internal2, :scan_check_args
341347

342348
end

spec/truffle/strscan/strscan_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative '../../ruby/spec_helper'
2+
3+
require 'strscan'
4+
5+
describe 'StringScanner' do
6+
it 'has a `scan_internal` method of under 100 AST nodes' do
7+
Truffle::Debug.ast_size(StringScanner.instance_method(:scan_internal)).should < 100
8+
end
9+
end

0 commit comments

Comments
 (0)