<commit_before>require 'erb'

::String.class_eval do
  def fortitude_append_escaped_string(output)
    raise ArgumentError, "You can only append to a String" unless output.kind_of?(String)

    if html_safe?
      output << self
    else
      output << ERB::Util.html_escape(self)
    end
  end
end

::Hash.class_eval do
  FORTITUDE_HYPHEN = "-".html_safe.freeze
  FORTITUDE_EQUALS_QUOTE = "=\"".html_safe.freeze
  FORTITUDE_QUOTE = "\"".html_safe.freeze
  FORTITUDE_SPACE = " ".html_safe.freeze

  TARGET_BASE = "".html_safe.freeze

  def fortitude_append_as_attributes(output, prefix)
    raise ArgumentError, "You can only append to a String" unless output.kind_of?(String)

    target = TARGET_BASE.dup

    each do |key, value|
      if value.kind_of?(Hash)
        new_prefix = case prefix
        when String then fortitude_append_to(key, prefix.dup)
        when nil then fortitude_append_to(key, "".html_safe)
        else raise ArgumentError, "You can only use a String as a prefix"
        end

        new_prefix << FORTITUDE_HYPHEN
        value.fortitude_append_as_attributes(target, new_prefix)
      else
        target << FORTITUDE_SPACE

        case prefix
        when String then target << prefix
        when nil then nil
        else raise ArgumentError, "You can only use a String as a prefix"
        end

        fortitude_append_to(key, target)
        target << FORTITUDE_EQUALS_QUOTE
        fortitude_append_to(value, target)
        target << FORTITUDE_QUOTE
      end
    end

    output << target
  end

  private
  def fortitude_append_to(object, output)
    case object
    when String then object.fortitude_append_escaped_string(output)
    when Symbol then object.to_s.fortitude_append_escaped_string(output)
    when Array then object.each_with_index do |o,i|
      output << " " if i > 0
      fortitude_append_to(o, output)
    end
    when nil then nil
    when Integer then output << object.to_s
    else object.to_s.fortitude_append_escaped_string(output)
    end
  end
end<commit_msg>Use String#original_concat, not String#<<, in our pure-Ruby extensions -- it's much faster; it doesn't do any #html_safe? checking.<commit_after>require 'erb'

::String.class_eval do
  def fortitude_append_escaped_string(output)
    raise ArgumentError, "You can only append to a String" unless output.kind_of?(String)

    if html_safe?
      output.original_concat(self)
    else
      output.original_concat(ERB::Util.html_escape(self))
    end
  end
end

::Hash.class_eval do
  FORTITUDE_HYPHEN = "-".html_safe.freeze
  FORTITUDE_EQUALS_QUOTE = "=\"".html_safe.freeze
  FORTITUDE_QUOTE = "\"".html_safe.freeze
  FORTITUDE_SPACE = " ".html_safe.freeze

  TARGET_BASE = "".html_safe.freeze

  def fortitude_append_as_attributes(output, prefix)
    raise ArgumentError, "You can only append to a String" unless output.kind_of?(String)

    target = TARGET_BASE.dup

    each do |key, value|
      if value.kind_of?(Hash)
        new_prefix = case prefix
        when String then fortitude_append_to(key, prefix.dup)
        when nil then fortitude_append_to(key, "".html_safe)
        else raise ArgumentError, "You can only use a String as a prefix"
        end

        new_prefix.original_concat(FORTITUDE_HYPHEN)
        value.fortitude_append_as_attributes(target, new_prefix)
      else
        target.original_concat(FORTITUDE_SPACE)

        case prefix
        when String then target.original_concat(prefix)
        when nil then nil
        else raise ArgumentError, "You can only use a String as a prefix"
        end

        fortitude_append_to(key, target)
        target.original_concat(FORTITUDE_EQUALS_QUOTE)
        fortitude_append_to(value, target)
        target.original_concat(FORTITUDE_QUOTE)
      end
    end

    output.original_concat(target)
  end

  private
  def fortitude_append_to(object, output)
    case object
    when String then object.fortitude_append_escaped_string(output)
    when Symbol then object.to_s.fortitude_append_escaped_string(output)
    when Array then object.each_with_index do |o,i|
      output.original_concat(" ") if i > 0
      fortitude_append_to(o, output)
    end
    when nil then nil
    when Integer then output.original_concat(object.to_s)
    else object.to_s.fortitude_append_escaped_string(output)
    end
  end
end
{'fraction_non_alphanumeric': 0.07437442075996292, 'fraction_numerical': 0.0004633920296570899, 'mean_word_length': 3.1994163424124515, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 5, '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': '2473552', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1391, 'n_words': 350}