class Regexp::MatchLength

Attributes

base_max[RW]
base_min[RW]
exp_class[RW]
max_rep[RW]
min_rep[RW]
reify[RW]

Public Class Methods

new(exp, opts = {}) click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 9
def initialize(exp, opts = {})
  self.exp_class = exp.class
  self.min_rep = exp.repetitions.min
  self.max_rep = exp.repetitions.max
  if (base = opts[:base])
    self.base_min = base
    self.base_max = base
    self.reify = ->{ '.' * base }
  else
    self.base_min = opts.fetch(:base_min)
    self.base_max = opts.fetch(:base_max)
    self.reify = opts.fetch(:reify)
  end
end
of(obj) click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 4
def self.of(obj)
  exp = obj.is_a?(Regexp::Expression::Base) ? obj : Regexp::Parser.parse(obj)
  exp.match_length
end

Public Instance Methods

each(opts = {}) { |num| ... } click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 24
def each(opts = {})
  return enum_for(__method__, opts) unless block_given?
  limit = opts[:limit] || 1000
  yielded = 0
  (min..max).each do |num|
    next unless include?(num)
    yield(num)
    break if (yielded += 1) >= limit
  end
end
endless_each() { |num| ... } click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 35
def endless_each
  return enum_for(__method__) unless block_given?
  (min..max).each { |num| yield(num) if include?(num) }
end
fixed?() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 44
def fixed?
  min == max
end
include?(length) click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 40
def include?(length)
  test_regexp.match?('X' * length)
end
inspect() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 60
def inspect
  type = exp_class.name.sub('Regexp::Expression::', '')
  "#<#{self.class}<#{type}> min=#{min} max=#{max}>"
end
max() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 52
def max
  max_rep * base_max
end
min() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 48
def min
  min_rep * base_min
end
minmax() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 56
def minmax
  [min, max]
end
to_re() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 65
def to_re
  /(?:#{reify.call}){#{min_rep},#{max_rep unless max_rep == Float::INFINITY}}/
end

Private Instance Methods

test_regexp() click to toggle source
# File lib/regexp_parser/expression/methods/match_length.rb, line 74
def test_regexp
  @test_regexp ||= /^#{to_re}$/
end