lib/code_zauker/grep.rb in code_zauker-0.0.2 vs lib/code_zauker/grep.rb in code_zauker-0.0.3

- old
+ new

@@ -38,10 +38,11 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # #++ +require 'code_zauker' module Grep # # Grep works like a shell grep. `file' can be either a string, # containing the name of a file to load and handle, or an IO object @@ -76,55 +77,58 @@ end cache = [] lines = [] + util=CodeZauker::Util.new() + loop do begin - line = file.readline + line = util.ensureUTF8(file.readline) + currentline +=1 cache.shift unless cache.length < pre_context - # GG Patch - # if print_filename==true - # cache.push("#{fileName}:#{line}") - # else cache.push("#{currentline}:#{line}") - # end - - - if line =~ pattern lines += cache cache = [] if post_context > 0 post_context.times do - begin - lines.push(file.readline) + begin + utf8line=util.ensureUTF8(file.readline) + lines.push("#{currentline}:#{utf8line}") currentline +=1 rescue IOError => e break end end end end rescue IOError => e break + rescue ArgumentError =>e2 + # Rethrow a probably UTF-8 fatal error + puts "Pattern Matching failed on \n\t#{fileName}\n\tLine:#{line}" + puts "Encoding of line:#{line.encoding.name} Valid? #{line.valid_encoding?}" + #raise e2 end end - file.each_line do |line| + file.each_line do |untrustedLine| cache.shift unless cache.length < pre_context + line=util.ensureUTF8(untrustedLine) cache.push(line) if line =~ pattern lines += cache if post_context > 0 post_context.times do begin - lines.push(file.readline) + utf8line=util.ensureUTF8(file.readline) + lines.push("#{currentline}:#{utf8line}") currentline +=1 rescue Exception => e break end end