lib/regextest/back/element.rb in regextest-0.1.5 vs lib/regextest/back/element.rb in regextest-0.1.6
- old
+ new
@@ -6,14 +6,18 @@
require 'regextest/common'
class Regextest::Back::Element
include Regextest::Common
def initialize(param)
- # puts "Element param:#{param[:cmd]} data:#{param[:data].size}"
- @command = param[:cmd]
+ # puts "Element param:#{param[:cmd]} data:#{param[:ranges].size}"
@param = param
- @candidates = param[:data] if @command == :CMD_SELECT
+ @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
attr_reader :param, :command, :candidates
# random fix
@@ -31,11 +35,12 @@
# size of candidates
def size
if(@candidates)
@candidates.size
else
- raise "internal error: candidates not found at size-method"
+ # raise "internal error: candidates not found at size-method"
+ 0
end
end
# [] of candidates
def [](num)
@@ -80,17 +85,18 @@
case @command
when :CMD_SELECT
if(@candidates)
@candidates.inspect
else
- @param[:data].inspect
+ @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_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
@@ -105,38 +111,55 @@
@candidates = [0xa]
end
# Is word-elements only?
def word_elements?
- letters = @candidates.map{|elem| [elem].pack("U*")}
- letters.join("").match(/^\p{Word}+$/)
+ letters = @candidates.map{|elem| [elem].pack("U*")}.join("")
+ if @charset == "u" || @charset == "d"
+ letters.match(/^\p{Word}+$/)
+ else
+ letters.match(/^\w+$/)
+ end
end
# is non-word-elements only?
def non_word_elements?
- letters = @candidates.map{|elem| [elem].pack("U*")}
- letters.join("").match(/^\p{^Word}+$/)
+ letters = @candidates.map{|elem| [elem].pack("U*")}.join("")
+ if @charset == "u" || @charset == "d"
+ letters.match(/^\p{^Word}+$/)
+ else
+ letters.match(/^\W+$/)
+ end
end
# set word-elements
def set_word_elements
- @candidates.select!{|elem| [elem].pack("U*").match(/^\w$/)}
+ 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
# set non_word-elements
def set_non_word_elements
- @candidates.select!{|elem| [elem].pack("U*").match(/^\W$/)}
+ 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
# checks empty
def empty?
@candidates.size == 0
end
# factory method to generate any char element
def self.any_char
# BUG: must consider other character set!
- Regextest::Back::Element.new({cmd: :CMD_SELECT, data: (0x20..0x7e).to_a})
+ Regextest::Back::Element.new({cmd: :CMD_SELECT, ranges: [0x20..0x7e]})
end
# factory method to generate any char element
def reverse
@candidates = ((0x20..0x7e).to_a) - @candidates