<commit_before>module Raven
  class Processor::UTF8Conversion < Processor
    def process(data)
      case data
      when Hash
        data.each do |k, v|
          data[k] = process(v)
        end
        data.to_h
      when Array
        data.map! { |v| process(v).as(typeof(v)) }
      when String
        !data.valid_encoding? ? clean_invalid_utf8_bytes(data) : data
      else
        data
      end
    end

    private def clean_invalid_utf8_bytes(str : String)
      str = str.encode("UTF-16", invalid: :skip)
      str = String.new(str, "UTF-16")<commit_msg>Cleanup invalid utf-8 in Exception messages<commit_after>module Raven
  class Processor::UTF8Conversion < Processor
    def process(data)
      case data
      when Hash
        data.each do |k, v|
          data[k] = process(v)
        end
        data.to_h
      when Array
        data.map! { |v| process(v).as(typeof(v)) }
      when Exception
        return data unless message = data.message
        return data if message.valid_encoding?
        data.class.new(clean_invalid_utf8_bytes(message), data.cause).tap do |ex|
          ex.callstack = data.callstack
        end
      when String
        !data.valid_encoding? ? clean_invalid_utf8_bytes(data) : data
      else
        data
      end
    end

    private def clean_invalid_utf8_bytes(str : String)
      str = str.encode("UTF-16", invalid: :skip)
      str = String.new(str, "UTF-16")
{'fraction_non_alphanumeric': 0.09647887323943662, 'fraction_numerical': 0.011267605633802818, 'mean_word_length': 2.178970917225951, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3385997', 'n_tokens_mistral': 452, 'n_tokens_neox': 441, 'n_words': 110}