Class: Fluent::GlobMatchPattern

Inherits:
MatchPattern show all
Defined in:
lib/fluent/match.rb

Instance Method Summary collapse

Methods inherited from MatchPattern

create

Constructor Details

#initialize(pat) ⇒ GlobMatchPattern



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fluent/match.rb', line 71

def initialize(pat)
  stack = []
  regex = ['']
  escape = false
  dot = false

  i = 0
  while i < pat.length
    c = pat[i,1]

    if escape
      regex.last << Regexp.escape(c)
      escape = false
      i += 1
      next

    elsif pat[i,2] == "**"
      # recursive any
      if dot
        regex.last << "(?![^\\.])"
        dot = false
      end
      if pat[i+2,1] == "."
        regex.last << "(?:.*\\.|\\A)"
        i += 3
      else
        regex.last << ".*"
        i += 2
      end
      next

    elsif dot
      regex.last << "\\."
      dot = false
    end

    if c == "\\"
      escape = true

    elsif c == "."
      dot = true

    elsif c == "*"
      # any
      regex.last << "[^\\.]*"

      # TODO
      #elsif c == "["
      #  # character class
      #  chars = ''
      #  while i < pat.length
      #    c = pat[i,1]
      #    if c == "]"
      #      break
      #    else
      #      chars << c
      #    end
      #    i += 1
      #  end
      #  regex.last << '['+Regexp.escape(chars).gsub("\\-",'-')+']'

    elsif c == "{"
      # or
      stack.push []
      regex.push ''

    elsif c == "}" && !stack.empty?
      stack.last << regex.pop
      regex.last << Regexp.union(*stack.pop.map {|r| Regexp.new(r) }).to_s

    elsif c == "," && !stack.empty?
      stack.last << regex.pop
      regex.push ''

    elsif c =~ /[a-zA-Z0-9_]/
      regex.last << c

    else
      regex.last << "\\#{c}"
    end

    i += 1
  end

  until stack.empty?
    stack.last << regex.pop
    regex.last << Regexp.union(*stack.pop).to_s
  end

  @regex = Regexp.new("\\A"+regex.last+"\\Z")
end

Instance Method Details

#match(str) ⇒ Object



163
164
165
# File 'lib/fluent/match.rb', line 163

def match(str)
  @regex.match(str) != nil
end