Module: Arugula::MatchNTimes

Includes:
Wrapping
Included in:
PlusPart, QuantifierPart, QuestionPart, StarPart
Defined in:
lib/arugula/parts.rb

Instance Attribute Summary

Attributes included from Wrapping

#wrapped

Instance Method Summary collapse

Instance Method Details

#initialize(*args, times: 1..1) ⇒ Object


210
211
212
213
# File 'lib/arugula/parts.rb', line 210

def initialize(*args, times: 1..1)
  @times = times
  super(*args)
end

#match(str, index, match_data) ⇒ Object


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/arugula/parts.rb', line 215

def match(str, index, match_data)
  match_count = 0
  end_index = index

  loop do
    matches, index = wrapped.match(str, index, match_data)
    if matches
      end_index = index
      match_count += 1
    end
    break if !matches || match_count > @times.end
  end

  matches = @times.member?(match_count)
  [matches, matches ? end_index : index]
end