class Milkode::Grep
Public Class Methods
new(content)
click to toggle source
# File lib/milkode/cdweb/lib/grep.rb, line 12 def initialize(content) @content = content end
Public Instance Methods
match_lines_and(patterns, is_sensitive, wide_match_range)
click to toggle source
# File lib/milkode/cdweb/lib/grep.rb, line 45 def match_lines_and(patterns, is_sensitive, wide_match_range) regexps = strs2regs(patterns, is_sensitive) result = [] index = 0 matcher = WideMatcher.create(wide_match_range) @content.each_line do |line| matcher.add_line_matchs(index, match_regexps(line, regexps)) result += matcher.match_lines if matcher.match? index += 1 end result.uniq end
match_lines_stopover(patterns, max_match, start_index, is_sensitive, wide_match_range)
click to toggle source
# File lib/milkode/cdweb/lib/grep.rb, line 16 def match_lines_stopover(patterns, max_match, start_index, is_sensitive, wide_match_range) regexps = strs2regs(patterns, is_sensitive) result = [] index = start_index matcher = WideMatcher.create(wide_match_range) lines = @content.split($/) while (index < lines.size) do line = lines[index] matcher.add_line_matchs(index, match_regexps(line, regexps)) if matcher.match? result += matcher.match_lines if result.size >= max_match index += 1 break end end index += 1 end index = 0 if (index >= lines.size) {:result => result.uniq, :next_line => index} end
one_match_and(patterns, is_sensitive, wide_match_range)
click to toggle source
# File lib/milkode/cdweb/lib/grep.rb, line 61 def one_match_and(patterns, is_sensitive, wide_match_range) match_lines_stopover(patterns, 1, 0, is_sensitive, wide_match_range) end
Private Instance Methods
match_regexps(line, regexps)
click to toggle source
# File lib/milkode/cdweb/lib/grep.rb, line 79 def match_regexps(line, regexps) regexps.reduce([]) {|result, v| result << v.match(line); result} end
strs2regs(strs, is_sensitive)
click to toggle source
# File lib/milkode/cdweb/lib/grep.rb, line 67 def strs2regs(strs, is_sensitive) regs = [] strs.each do |v| option = 0 option |= Regexp::IGNORECASE if (!is_sensitive && Util.downcase?(v)) regs << Regexp.new(Regexp.escape(v), option) end regs end