Class: Packr::RegexpGroup

Inherits:
Collection show all
Defined in:
lib/packr/base2/regexp_group.rb

Direct Known Subclasses

Parser

Defined Under Namespace

Classes: Item

Constant Summary collapse

IGNORE =
"\\0"
BACK_REF =
/\\(\d+)/
ESCAPE_CHARS =
/\\./
ESCAPE_BRACKETS =
/\(\?[:=!]|\[[^\]]+\]/
BRACKETS =
/\(/
LOOKUP =
/\\(\d+)/
LOOKUP_SIMPLE =
/^\\\d+$/

Instance Attribute Summary

Attributes inherited from Collection

#keys, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#add, #clear, #copy, create, #each, #get_at, #get_keys, #index_of, #item, #map, #merge, #put, #put_at, #remove, #remove_at, #reverse!, #size, #slice, #sort!

Methods inherited from Map

#clear, #copy, #each, #get, #get_keys, #get_values, #has?, #merge, #put, #remove, #size, #union

Constructor Details

#initialize(values = nil, ignore_case = false) ⇒ RegexpGroup

Returns a new instance of RegexpGroup.



12
13
14
15
# File 'lib/packr/base2/regexp_group.rb', line 12

def initialize(values = nil, ignore_case = false)
  super(values)
  @ignore_case = !!ignore_case
end

Class Method Details

.count(expression) ⇒ Object



121
122
123
124
125
# File 'lib/packr/base2/regexp_group.rb', line 121

def self.count(expression)
  # Count the number of sub-expressions in a Regexp/RegexpGroup::Item.
  expression = expression.to_s.gsub(ESCAPE_CHARS, "").gsub(ESCAPE_BRACKETS, "")
  expression.scan(BRACKETS).length
end

Instance Method Details

#exec(string, override = nil) {|sections| ... } ⇒ Object

Yields:

  • (sections)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/packr/base2/regexp_group.rb', line 17

def exec(string, override = nil)
  string = string.to_s # type-safe
  return string if @keys.empty?
  sections = []
  override = 0 if override == IGNORE
  ret = string.gsub(Regexp.new(self.to_s, @ignore_case && Regexp::IGNORECASE)) do |match|
    removed_section = [$~.begin(0), match.size]
    offset, i, result = 1, 0, match
    arguments = [match] + $~.captures + [$~.begin(0), string]
    # Loop through the items.
    each do |item, key|
      nxt = offset + item.length + 1
      if arguments[offset] # do we have a result?
        replacement = override.nil? ? item.replacement : override
        case replacement
        when Proc
          result = replacement.call(*arguments[offset...nxt])
        when Numeric
          result = arguments[offset + replacement]
        else
          result = replacement
        end
      end
      offset = nxt
    end
    result = result.to_s
    removed_section += [result.size, match, result]
    sections << removed_section if %w[// /*].include?(match[0..1])
    result
  end
  yield(sections) if block_given?
  ret
end

#insert_at(index, expression, replacement) ⇒ Object



51
52
53
54
# File 'lib/packr/base2/regexp_group.rb', line 51

def insert_at(index, expression, replacement)
  expression = expression.is_a?(Regexp) ? expression.source : expression.to_s
  super(index, expression, replacement)
end

#test(string) ⇒ Object



56
57
58
# File 'lib/packr/base2/regexp_group.rb', line 56

def test(string)
  exec(string) != string
end

#to_sObject



60
61
62
63
64
65
66
67
68
# File 'lib/packr/base2/regexp_group.rb', line 60

def to_s
  offset = 1
  "(" + map { |item, key|
    # Fix back references.
    expression = item.to_s.gsub(BACK_REF) { |m| "\\" + (offset + $1.to_i) }
    offset += item.length + 1
    expression
  }.join(")|(") + ")"
end