<commit_before>require 'tempfile'
module Wdiff
  class << self
    def included(target)
      verify_wdiff_in_path
    end

    def verify_wdiff_in_path
      path = %x{which #{bin_path}}
      raise "GNU wdiff (http://www.gnu.org/software/wdiff/) not found in $PATH" if path.empty?
    end

    def bin_path
      'wdiff'
    end

    # TODO - this will fail if option strings include quotes, need to escape quotes automagically
    def options_string_from_hash(options={})
      parts = []

      options[:inserts].each_with_index do |token, index|
        token.gsub!(/^\"$/,'\"') if token == '"'
        parts << ( index.even? ?  %Q(--start-insert="#{token}")  :  %Q(--end-insert="#{token}") )
        break if index >= 1
      end if options[:inserts]

      
      options[:deletes].each_with_index do |token, index|
        token.gsub!(/^\"$/,'\"') if token == '"'
        parts << ( index.even? ?  %Q(--start-delete="#{token}")  :  %Q(--end-delete="#{token}") )
        break if index >= 1
      end if options[:deletes]
      
      parts.join(" ")
    end

    def diff(old_str, new_str, options={})
      f1, f2 = Tempfile.new('f1'), Tempfile.new('f2')
      f1.write(old_str); f1.flush
      f2.write(new_str); f2.flush
      opt_str = options_string_from_hash(options)
      raw = %x{#{bin_path} #{opt_str} #{f1.path} #{f2.path}}
      f1.close; f2.close
      raw
    end
  end

  def wdiff(str_new, options={})
    Wdiff::diff(self, str_new, options)
  end
<commit_msg>Better tmpfile handling<commit_after>require 'tempfile'
module Wdiff
  class << self
    def included(target)
      verify_wdiff_in_path
    end

    def verify_wdiff_in_path
      path = %x{which #{bin_path}}
      raise "GNU wdiff (http://www.gnu.org/software/wdiff/) not found in $PATH" if path.empty?
    end

    def bin_path
      'wdiff'
    end

    # TODO - this will fail if option strings include quotes, need to escape quotes automagically
    def options_string_from_hash(options={})
      parts = []

      options[:inserts].each_with_index do |token, index|
        token.gsub!(/^\"$/,'\"') if token == '"'
        parts << ( index.even? ?  %Q(--start-insert="#{token}")  :  %Q(--end-insert="#{token}") )
        break if index >= 1
      end if options[:inserts]

      
      options[:deletes].each_with_index do |token, index|
        token.gsub!(/^\"$/,'\"') if token == '"'
        parts << ( index.even? ?  %Q(--start-delete="#{token}")  :  %Q(--end-delete="#{token}") )
        break if index >= 1
      end if options[:deletes]
      
      parts.join(" ")
    end

    def diff(old_str, new_str, options={})
      f1, f2 = Tempfile.new('f1'), Tempfile.new('f2')
      f1.write(old_str); f1.flush
      f2.write(new_str); f2.flush
      opt_str = options_string_from_hash(options)
      %x{#{bin_path} #{opt_str} #{f1.path} #{f2.path}}
    ensure
      f1.close
      f1.unlink
      f2.close
      f2.unlink
    end
  end

  def wdiff(str_new, options={})
    Wdiff::diff(self, str_new, options)
  end

{'fraction_non_alphanumeric': 0.16146179401993355, 'fraction_numerical': 0.009966777408637873, 'mean_word_length': 2.488991888760139, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5469462', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1060, 'n_words': 235}