class List::Matcher::CharClass

Constants

CI_WORD_CHARS
NUM_CHARS
SPACE_CHARS
WORD_CHARS

Attributes

num[RW]
space[RW]
word[RW]

Public Class Methods

new(engine, children) click to toggle source
Calls superclass method List::Matcher::Node::new
# File lib/list_matcher.rb, line 645
def initialize(engine, children)
  super(engine, nil)
  if engine.case_insensitive
    if ( CI_WORD_CHARS - children ).empty?
      self.word = true
      self.num = false
      children -= CI_WORD_CHARS
    end
  elsif ( WORD_CHARS - children ).empty?
    self.word = true
    self.num = false
    children -= WORD_CHARS
  end
  if num.nil? && ( NUM_CHARS - children ).empty?
    self.num = true
    children -= NUM_CHARS
  end
  if ( SPACE_CHARS - children ).empty?
    self.space = true
    children -= SPACE_CHARS
  end
  @children = children
end

Public Instance Methods

atomic?() click to toggle source
# File lib/list_matcher.rb, line 669
def atomic?
  true
end
cc_quote(c) click to toggle source
# File lib/list_matcher.rb, line 713
def cc_quote(c)
  return Regexp.quote(c) if c =~ /\s/
  case c
  when '[' then '\['
  when ']' then '\]'
  when '\\' then '\\\\'
  when '-' then '\-'
  when '^' then '\^'
  else c
  end
end
char_class(chars) click to toggle source

takes a list of characters and returns a character class expression matching it

# File lib/list_matcher.rb, line 684
def char_class(chars)
  mid = if chars.empty?
    ''
  else
    rs = ranges(chars)
    if rs.size == 1 && rs[0][0] == rs[0][1]
      cc_quote rs[0][0].chr(engine.encoding)
    else
      mid = rs.map do |s, e|
        if s == e
          cc_quote s.chr(engine.encoding)
        elsif e == s + 1
          "#{ cc_quote s.chr(engine.encoding) }#{ cc_quote e.chr(engine.encoding) }"
        else
          "#{ cc_quote s.chr(engine.encoding) }-#{ cc_quote e.chr(engine.encoding) }"
        end
      end.join
    end
  end
  mid += '\w' if word
  mid += '\d' if num
  mid += '\s' if space
  if mid.length == 1 || mid =~ /\A\\\w\z/
    mid
  else
    "[#{mid}]"
  end
end
convert() click to toggle source
# File lib/list_matcher.rb, line 675
def convert
  rx = char_class children
  if optional?
    rx += qmark
  end
  rx
end
flatten() click to toggle source
# File lib/list_matcher.rb, line 673
def flatten; end
ranges(chars) click to toggle source
# File lib/list_matcher.rb, line 725
def ranges(chars)
  chars = chars.map(&:ord).sort
  rs = []
  c  = chars.shift
  r  = [ c, c ]
  while chars.size > 0
    c = chars.shift
    if c == r[1] + 1
      r[1] = c
    else
      rs << r
      r = [ c, c ]
    end
  end
  rs << r
end