<commit_before># frozen_string_literal: true

module Query
  module Modules
    # Turn a query into a string and vice versa.
    module Serialization
      def self.included(base)
        base.extend(ClassMethods)
      end

      def serialize
        hash = params.merge(
          model: model.to_s.to_sym,
          flavor: flavor
        )
        hash.keys.sort_by(&:to_s).map do |key|
          serialize_key_value(key, hash[key])
        end.join(";")
      end

      def serialize_key_value(key, val)
        "#{key}=#{serialize_value(val)}"
      end

      def serialize_value(val)
        case val
        when Array      then "@#{val.map { |v| serialize_value(v) }.join(",")}"
        when String     then "$#{serialize_string(val)}"
        when Symbol     then ":#{serialize_string(val.to_s)}"
        when Integer    then "##{val}"
        when Float      then "##{val}"
        when TrueClass  then "1"
        when FalseClass then "0"
        when NilClass   then "-"
        end
      end

      def serialize_string(val)
        # The "n" forces the Regexp to be in ascii 8 bit encoding = binary.
        String.new(val).force_encoding("binary").
          gsub(%r{[,;:#%&=/?\x00-\x1f\x7f-\xff]}n) do |char|
          format("%%%02.2X", char.ord)
        end
      end
<commit_msg>Fix Serialization Lint/DuplicateBranch offense<commit_after># frozen_string_literal: true

module Query
  module Modules
    # Turn a query into a string and vice versa.
    module Serialization
      def self.included(base)
        base.extend(ClassMethods)
      end

      def serialize
        hash = params.merge(
          model: model.to_s.to_sym,
          flavor: flavor
        )
        hash.keys.sort_by(&:to_s).map do |key|
          serialize_key_value(key, hash[key])
        end.join(";")
      end

      def serialize_key_value(key, val)
        "#{key}=#{serialize_value(val)}"
      end

      def serialize_value(val)
        case val
        when Array      then "@#{val.map { |v| serialize_value(v) }.join(",")}"
        when String     then "$#{serialize_string(val)}"
        when Symbol     then ":#{serialize_string(val.to_s)}"
        when Integer, Float then "##{val}"
        when TrueClass  then "1"
        when FalseClass then "0"
        when NilClass   then "-"
        end
      end

      def serialize_string(val)
        # The "n" forces the Regexp to be in ascii 8 bit encoding = binary.
        String.new(val).force_encoding("binary").
          gsub(%r{[,;:#%&=/?\x00-\x1f\x7f-\xff]}n) do |char|
          format("%%%02.2X", char.ord)
        end
      end

{'fraction_non_alphanumeric': 0.12375096079938509, 'fraction_numerical': 0.007686395080707148, 'mean_word_length': 2.0057736720554273, 'pattern_counts': {'":': 2, '<': 3, '<?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': '120926', 'n_tokens_mistral': 848, 'n_tokens_neox': 832, 'n_words': 210}