lib/arugula.rb in arugula-0.2.1 vs lib/arugula.rb in arugula-0.3.0

- old
+ new

@@ -1,18 +1,31 @@ # frozen_string_literal: true class Arugula require 'arugula/version' + attr_reader :captures + + autoload :MatchData, 'arugula/match_data' autoload :Parser, 'arugula/parser' def initialize(pattern) - @root = Parser.new(pattern).parse! + @root, @captures = Parser.new(pattern).parse! end def match?(str, index = 0) + match_data = match(str, index) + match_data && match_data.start_index + end + + def match(str, index = 0) + match_data = MatchData.new(self, str) loop do - match, = @root.match(str, index) - return index if match + match, end_index = @root.match(str, index, match_data) + if match + match_data.start_index = index + match_data.end_index = end_index + return match_data.freeze + end index += 1 return if index > str.size end end