class Regextest::RegexOption

A class that manages options of regular expression

Constants

TstRegOptAscii
TstRegOptDefault

Constants for the class

TstRegOptUnicode

Attributes

char_set[RW]
reg_options[RW]

Public Class Methods

new(options = nil) click to toggle source
# File lib/regextest/regex-option.rb, line 14
def initialize(options = nil)
  self.set(options)
  @char_set = TstRegOptDefault
end

Public Instance Methods

charset() click to toggle source

get charset (“a”, “u”, or “d”)

# File lib/regextest/regex-option.rb, line 106
def charset
  case @char_set
  when TstRegOptDefault
    "d"
  when TstRegOptAscii
    "a"
  when TstRegOptUnicode
    "u"
  else
    raise "Internal error. Invalid char_set (#{@char_set})"
  end
end
initialize_copy(source_obj) click to toggle source

a method for copy (maybe unnecessary)

# File lib/regextest/regex-option.rb, line 20
def initialize_copy(source_obj)
  @reg_options = source_obj.reg_options
  @char_set = source_obj.char_set
end
is_ascii?() click to toggle source
# File lib/regextest/regex-option.rb, line 136
def is_ascii?
  (@char_set == TstRegOptAscii)
end
is_default_char_set?() click to toggle source
# File lib/regextest/regex-option.rb, line 132
def is_default_char_set?
  (@char_set == TstRegOptDefault)
end
is_extended?() click to toggle source
# File lib/regextest/regex-option.rb, line 124
def is_extended?
  (@reg_options & Regexp::EXTENDED != 0)
end
is_ignore?() click to toggle source

methods for checking each flag

# File lib/regextest/regex-option.rb, line 120
def is_ignore?
  (@reg_options & Regexp::IGNORECASE != 0)
end
is_multiline?() click to toggle source
# File lib/regextest/regex-option.rb, line 128
def is_multiline?
  (@reg_options & Regexp::MULTILINE != 0)
end
is_unicode?() click to toggle source
# File lib/regextest/regex-option.rb, line 140
def is_unicode?
  (@char_set == TstRegOptUnicode)
end
modify(options) click to toggle source

modify one or more options

# File lib/regextest/regex-option.rb, line 32
def modify(options)
  case options
  when String
    modify_string(options)
  when Integer
    modify_integer(options)
  end
end
modify_integer(options) click to toggle source

modify options by integer

# File lib/regextest/regex-option.rb, line 87
def modify_integer(options)
  @reg_options |= options
end
modify_string(opt_string) click to toggle source

modify regoption by string (like ‘x-im’)

# File lib/regextest/regex-option.rb, line 42
def modify_string(opt_string)
  opts = opt_string.split("-")
  raise "Option string (#{opt_string}) is invalid." if(opts.size > 2)
  
  # set option bits
  if(opts[0])
    opts[0].split(//).each do | opt |
      case opt
      when 'i'
        @reg_options |= Regexp::IGNORECASE
      when 'x'
        @reg_options |= Regexp::EXTENDED
      when 'm'
        @reg_options |= Regexp::MULTILINE
      when 'd'
        @char_set = TstRegOptDefault
      when 'u'
        @char_set = TstRegOptUnicode
      when 'a'
        @char_set = TstRegOptAscii
      else
        raise "Invalid char (#{opt}) found in regexp option"
      end
    end
  end
  
  # reset option bits
  if(opts[1])
    opts[1].split(//).each do | opt |
      case opt
      when 'i'
        @reg_options &= ~Regexp::IGNORECASE
      when 'x'
        @reg_options &= ~Regexp::EXTENDED
      when 'm'
        @reg_options &= ~Regexp::MULTILINE
      else
        raise "Invalid char (#{opt}) found in regexp option"
      end
    end
  end
  @reg_options
end
prefix_reg() click to toggle source

generate prefix regex

# File lib/regextest/regex-option.rb, line 92
def prefix_reg
  prefix = ""
  prefix += "i" if is_ignore?
  prefix += "m" if is_multiline?
  prefix += "x" if is_extended?
  
  if prefix.size > 0
    "(?#{prefix})"
  else
    ""
  end
end
set(options) click to toggle source

set one or more options

# File lib/regextest/regex-option.rb, line 26
def set(options)
  @reg_options = 0
  modify(options)
end