class Regextest::Back::Element

Attributes

candidates[R]
command[R]
param[R]

Public Class Methods

any_char() click to toggle source

factory method to generate any char element

# File lib/regextest/back/element.rb, line 159
def self.any_char
  # BUG: must consider other character set!
  Regextest::Back::Element.new({cmd: :CMD_SELECT, ranges:  [0x20..0x7e]})
end
new(param) click to toggle source
# File lib/regextest/back/element.rb, line 10
def initialize(param)
  # puts "Element param:#{param[:cmd]} data:#{param[:ranges].size}"
  @param = param
  @command = param[:cmd]
  @charset = param[:charset]
  if @command == :CMD_SELECT
    @candidates = param[:ranges].inject([]){|result, range| result += range.to_a}
  end
  # @candidates = param[:data] if @command == :CMD_SELECT
end

Public Instance Methods

[](num) click to toggle source

of candidates

# File lib/regextest/back/element.rb, line 46
def [](num)
  if(@candidates)
    @candidates[num]
  else
    raise "internal error: candidates not found at at-method"
  end
end
empty?() click to toggle source

checks empty

# File lib/regextest/back/element.rb, line 154
def empty?
  @candidates.size == 0
end
exclude(other_obj) click to toggle source

exclude

# File lib/regextest/back/element.rb, line 66
def exclude(other_obj)
  raise "invalid command at exclude" if(other_obj.command != :CMD_SELECT)
  work = @candidates - other_obj.candidates
  if work.size > 0
    @candidates = work
  else
    nil
  end
end
inspect() click to toggle source

for simple pretty print

# File lib/regextest/back/element.rb, line 84
def inspect
  case @command
  when :CMD_SELECT
    if(@candidates)
      @candidates.inspect
    else
      @param[:ranges].inspect
    end
  when :CMD_LOOK_BEHIND, :CMD_LOOK_AHEAD, :CMD_NOT_LOOK_BEHIND, :CMD_NOT_LOOK_AHEAD
    @param.inspect
  when :CMD_ANC_LINE_BEGIN, :CMD_ANC_LINE_END, :CMD_ANC_WORD_BOUND, :CMD_ANC_WORD_UNBOUND,
       :CMD_ANC_STRING_BEGIN, :CMD_ANC_STRING_END, :CMD_ANC_STRING_END2, :CMD_ANC_MATCH_START,
       :CMD_ANC_LOOK_BEHIND2, :CMD_ANC_RELUCTANT_BEGIN, :CMD_ANC_RELUCTANT_END,
       :CMD_ANC_POSSESSIVE_BEGIN, :CMD_ANC_POSSESSIVE_END
    @param.inspect
  else
    raise "inner error, invalid command #{@command}"
  end
end
intersect(other_obj) click to toggle source

narrow down candidates

# File lib/regextest/back/element.rb, line 55
def intersect(other_obj)
  raise "invalid command at intersect" if(other_obj.command != :CMD_SELECT)
  work = @candidates & other_obj.candidates
  if work.size > 0
    @candidates = work
  else
    nil
  end
end
new_line?() click to toggle source

Includes new line or not

# File lib/regextest/back/element.rb, line 105
def new_line?
  @candidates.index(0xa)
end
non_word_elements?() click to toggle source

is non-word-elements only?

# File lib/regextest/back/element.rb, line 125
def non_word_elements?
  letters = @candidates.map{|elem| [elem].pack("U*")}.join("")
  if @charset == "u" || @charset == "d"
    letters.match(/^\p{^Word}+$/)
  else
    letters.match(/^\W+$/)
  end
end
random_fix() click to toggle source

random fix

# File lib/regextest/back/element.rb, line 24
def random_fix
  if @command == :CMD_SELECT
    offset = (@candidates.size > 1)?TstRand(@candidates.size):0
    result = @candidates[offset]
    @candidates = [result]   # fixed!
  else
    raise "invalid command at random_fix: #{@command}"
  end
  [result].pack("U*")   # tranforms from code point to a letter
end
reverse() click to toggle source

factory method to generate any char element

# File lib/regextest/back/element.rb, line 165
def reverse
  @candidates = ((0x20..0x7e).to_a) - @candidates
  self
end
set_new_line() click to toggle source

Sets new line

# File lib/regextest/back/element.rb, line 110
def set_new_line
  @candidates = [0xa]
end
set_non_word_elements() click to toggle source

set non_word-elements

# File lib/regextest/back/element.rb, line 144
def set_non_word_elements
  if @charset == "u" || @charset == "d"
    @candidates.select!{|elem| [elem].pack("U*").match(/^\p{^Word}$/)}
    #@candidates.select!{|elem| [elem].pack("U*").match(/^[[:^word:]]$/)}
  else
    @candidates.select!{|elem| [elem].pack("U*").match(/^[[:^word:]]$/)}
  end
end
set_word_elements() click to toggle source

set word-elements

# File lib/regextest/back/element.rb, line 135
def set_word_elements
  if @charset == "u" || @charset == "d"
    @candidates.select!{|elem| [elem].pack("U*").match(/^\p{Word}$/)}
  else
    @candidates.select!{|elem| [elem].pack("U*").match(/^\w$/)}
  end
end
size() click to toggle source

size of candidates

# File lib/regextest/back/element.rb, line 36
def size
  if(@candidates)
    @candidates.size
  else
    # raise "internal error: candidates not found at size-method"
    0
  end
end
union(other_obj) click to toggle source

join candidates

# File lib/regextest/back/element.rb, line 77
def union(other_obj)
  raise "invalid command at union" if(other_obj.command != :CMD_SELECT)
  #@candidates |= other_obj.candidates
  @candidates += other_obj.candidates # to be faster
end
word_elements?() click to toggle source

Is word-elements only?

# File lib/regextest/back/element.rb, line 115
def word_elements?
  letters = @candidates.map{|elem| [elem].pack("U*")}.join("")
  if @charset == "u" || @charset == "d"
    letters.match(/^\p{Word}+$/)
  else
    letters.match(/^\w+$/)
  end
end