class Regexgen::Ast::CharClass

Represents a character class (e.g. [0-9a-z])

Attributes

precedence[R]

Public Class Methods

new(a, b) click to toggle source
# File lib/regexgen/ast.rb, line 35
def initialize(a, b)
  @precedence = 1
  @set = [a, b].flatten
end

Public Instance Methods

char_class() click to toggle source
# File lib/regexgen/ast.rb, line 56
def char_class
  @set
end
length() click to toggle source
# File lib/regexgen/ast.rb, line 40
def length
  1
end
single_character?() click to toggle source
# File lib/regexgen/ast.rb, line 44
def single_character?
  @set.none? { |c| c.ord > 0xffff }
end
single_codepoint?() click to toggle source
# File lib/regexgen/ast.rb, line 48
def single_codepoint?
  true
end
to_s() click to toggle source
# File lib/regexgen/ast.rb, line 52
def to_s
  "[#{to_ranges_string}]"
end

Private Instance Methods

to_ranges() click to toggle source
# File lib/regexgen/ast.rb, line 74
def to_ranges
  set = @set.sort
  ranges = [[set.first, set.first]]
  set.drop(1).each_with_object(ranges) do |c, acc|
    if acc.last.last.ord.next == c.ord
      acc.last[-1] = c
    else
      acc << [c, c]
    end
  end
end
to_ranges_string() click to toggle source
# File lib/regexgen/ast.rb, line 62
def to_ranges_string
  to_ranges.map do |first, last|
    if first == last
      first
    elsif first.ord.next == last.ord
      "#{first}#{last}"
    else
      "#{first}-#{last}"
    end
  end.join
end