Sha256: e3551ca996fa672eacc7c2dd3b1e53a3f8f5c1a91f951bc0181ebc6936c38b11

Contents?: true

Size: 1.95 KB

Versions: 12

Compression:

Stored size: 1.95 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief  grenwebで使用する行指向の検索
# @author ongaeshi
# @date   2010/10/18

module Milkode
  class Grep
    def initialize(content)
      @content = content
    end

    MatchLineResult = Struct.new(:index, :match_datas)

    def match_lines_stopover(patterns, max_match, start_index)
      result = []
      patternRegexps = strs2regs(patterns, true)
      index = start_index

      lines = @content.split($/)

      while (index < lines.size) do
        line = lines[index]

        match_datas = []
        patternRegexps.each {|v| match_datas << v.match(line)}

        if (match_datas.all?)
          result << MatchLineResult.new(index, match_datas)
          if result.size >= max_match
            index += 1
            break
          end
        end

        index += 1
      end

      index = 0 if (index >= lines.size)
      {:result => result, :next_line => index}
    end
    
    def match_lines_and(patterns)
      result = []
      patternRegexps = strs2regs(patterns, true)
      index = 0
      
      @content.each_line do |line|
        match_datas = []
        patternRegexps.each {|v| match_datas << v.match(line)}

        if (match_datas.all?)
          result << MatchLineResult.new(index, match_datas)
        end

        index += 1
      end
      
      result
    end

    def one_match_and(patterns)
      patternRegexps = strs2regs(patterns, true)
      index = 0
      
      @content.each_line do |line|
        match_datas = []
        patternRegexps.each {|v| match_datas << v.match(line)}

        if (match_datas.all?)
          return MatchLineResult.new(index, match_datas)
        end

        index += 1
      end
      
      nil
    end

    private
    
    def strs2regs(strs, ignore = false)
      regs = []

      strs.each do |v|
        option = 0
        option |= Regexp::IGNORECASE if (ignore)
        regs << Regexp.new(Regexp.escape(v), option)
      end

      regs
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
milkode-0.8.1 lib/milkode/cdweb/lib/grep.rb
milkode-0.8.0 lib/milkode/cdweb/lib/grep.rb
milkode-0.7.1 lib/milkode/cdweb/lib/grep.rb
milkode-0.7.0 lib/milkode/cdweb/lib/grep.rb
milkode-0.6.3 lib/milkode/cdweb/lib/grep.rb
milkode-0.6.2 lib/milkode/cdweb/lib/grep.rb
milkode-0.6.1 lib/milkode/cdweb/lib/grep.rb
milkode-0.6.0 lib/milkode/cdweb/lib/grep.rb
milkode-0.5.3 lib/milkode/cdweb/lib/grep.rb
milkode-0.5.2 lib/milkode/cdweb/lib/grep.rb
milkode-0.5.1 lib/milkode/cdweb/lib/grep.rb
milkode-0.5.0 lib/milkode/cdweb/lib/grep.rb