Sha256: 0408e4b37a93788f736b97242d52d1a5e99e054ce9bf02819630dc5dbf1f4460

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Log2mail

  module File::Parser

    def parse(multiline_text)
      return [] unless multiline_text
      empty_buf! unless @buf
      # add new text to parse buffer
      @buf = @buf << multiline_text
      hits = []
      @patterns.each do |pattern|
        matches = []
        if pattern.from_string?
          matches.concat @buf.lines.find_all{ |line| line.match(pattern) }
          matches.map!(&:chomp)
        else
          matches.concat @buf.gsub(pattern).collect.to_a
        end
        hits.concat matches.map { |match| Hit.new( match, pattern, @path ) }
      end
      unless hits.empty?
        log 'pattern match: ' + hits.inspect
        empty_buf! # match -> clear buffer
      else
        cleanup_buf! # no match -> just keep what's necessary
      end
      hits
    end

    # ---------------------
    # - Buffer management -
    # ---------------------

    def empty_buf!
      @buf = ''
    end

    def cleanup_buf!
      # FIXME: cleanup should clean up to latest match only OR upto last line (if from_string true)
      if @patterns.all? {|p| p.from_string? }
        empty_buf!
      else
        @buf = @buf.byteslice(-@maxbufsize/32,@maxbufsize) if @buf.bytesize > @maxbufsize
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
log2mail-0.0.1.pre4 lib/log2mail/file/parser.rb
log2mail-0.0.1.pre3 lib/log2mail/file/parser.rb
log2mail-0.0.1.pre2 lib/log2mail/file/parser.rb