Sha256: 6d2f9f196dc927932d5aba831fc1dbca30ee5b230948c3899e666e73204e8bbf

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module DocumentSearch
  class Search
    # An instance of a search type method: Regular expression
    def self.regex_search_method(line, query, replace)
      if line =~ /#{query}/
        startoff = $`.length
        endoff   = (startoff + $&.length) - 1
        line[startoff..endoff] = replace
        return line, startoff, startoff + replace.length
      end
      return nil
    end

    # An instance of a search type method: Plain text search
    def self.plain_search_method(line, query, replace)
      i = line.index(query)
      if i
        startoff = i
        endoff   = i + query.length - 1
        line[startoff..endoff] = replace
        return line, startoff, startoff + replace.length
      end
      return nil
    end
    
    # An instance of a search type method: Glob text search
    # Converts a glob pattern (* or ?) into a regex pattern
    def self.glob_search_method(line, query, replace)
      # convert the glob pattern to a regex pattern
      new_query = ""
      query.each_char do |c| 
        case c
        when "*"
          new_query << ".*"
        when "?"
          new_query << "."
        else          
          new_query << Regexp.escape(c)
        end
      end
      regex_search_method(line, new_query, replace)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redcar-0.3.9 plugins/document_search/lib/document_search/search.rb
redcar-0.3.9.0dev plugins/document_search/lib/document_search/search.rb