Sha256: eb5b6e2587aa1015969f6299364749788c171449455ae42b1e70fa66c8df3fad

Contents?: true

Size: 966 Bytes

Versions: 1

Compression:

Stored size: 966 Bytes

Contents

# 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, @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_data.reset_captures!
      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

  def to_s
    "/#{@root}/"
  end

  alias inspect to_s

  def hash
    to_s.hash
  end

  def ==(other)
    return false unless other.is_a?(Arugula) || other.is_a?(Regexp)
    inspect == other.inspect
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arugula-0.4.0 lib/arugula.rb