<commit_before>module Rack::Accept
  # Represents an HTTP Accept header according to the HTTP 1.1 specification,
  # and provides several convenience methods for determining acceptable media
  # types.
  #
  # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
  class MediaType
    include Header

    attr_accessor :extensions

    # The name of this header.
    def name
      'Accept'
    end

    # Determines the quality factor (qvalue) of the given +media_type+.
    def qvalue(media_type)
      return 1 if @qvalues.empty?
      m = matches(media_type)
      return 0 if m.empty?
      normalize_qvalue(@qvalues[m.first])
    end

    # Returns an array of media types from this header that match the given
    # +media_type+, ordered by precedence.
    def matches(media_type)
      type, subtype, params = parse_media_type(media_type)
      values.select {|v|
        if v == media_type || v == '*/*'
          true
        else
          t, s, p = parse_media_type(v)
          t == type && (s == '*' || s == subtype) && (p == '' || params_match?(params, p))
        end
      }.sort_by {|v|
        # Most specific gets precedence.
        v.length
      }.reverse
    end

  private

    def initialize(header)
      @extensions = {}
      header.to_s.split(',').each do |raw_media_type|
        params = { 'q' => 1 }
        parts = raw_media_type.split(';')
        media_type = parts.shift.strip.downcase
        parts.each do |part|
          pair = part.split('=', 2)
          pair[0].strip.downcase
          pair[1].strip
          params[pair[0]] = pair[0] == 'q' ? normalize_qvalue(pair[1]).to_f : pair[1]
        end
        @extensions[media_type] = params
      end

      @qvalues = {}
      @extensions.each do |k, v|
        @qvalues[k] = v['q']
      end
    end

    # Returns true if all parameters and values in +match+ are also present in
    # +params+.
    def params_match?(params, match)
      return true if params == match
      parsed = parse_range_params(params)
      parsed == parsed.merge(parse_range_params(match))
    end
  end
end<commit_msg>populate @qvalues and @extensions in the same loop<commit_after>module Rack::Accept
  # Represents an HTTP Accept header according to the HTTP 1.1 specification,
  # and provides several convenience methods for determining acceptable media
  # types.
  #
  # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
  class MediaType
    include Header

    attr_accessor :extensions

    # The name of this header.
    def name
      'Accept'
    end

    # Determines the quality factor (qvalue) of the given +media_type+.
    def qvalue(media_type)
      return 1 if @qvalues.empty?
      m = matches(media_type)
      return 0 if m.empty?
      normalize_qvalue(@qvalues[m.first])
    end

    # Returns an array of media types from this header that match the given
    # +media_type+, ordered by precedence.
    def matches(media_type)
      type, subtype, params = parse_media_type(media_type)
      values.select {|v|
        if v == media_type || v == '*/*'
          true
        else
          t, s, p = parse_media_type(v)
          t == type && (s == '*' || s == subtype) && (p == '' || params_match?(params, p))
        end
      }.sort_by {|v|
        # Most specific gets precedence.
        v.length
      }.reverse
    end

  private

    def initialize(header)
      @extensions = {}
      @qvalues = {}

      header.to_s.split(',').each do |raw_media_type|
        params = { 'q' => 1 }
        parts = raw_media_type.split(';')
        media_type = parts.shift.strip.downcase
        parts.each do |part|
          pair = part.split('=', 2)
          pair[0].strip.downcase
          pair[1].strip
          params[pair[0]] = pair[0] == 'q' ? normalize_qvalue(pair[1]).to_f : pair[1]
        end
        @extensions[media_type] = params
        @qvalues[media_type] = params['q']
      end
    end

    # Returns true if all parameters and values in +match+ are also present in
    # +params+.
    def params_match?(params, match)
      return true if params == match
      parsed = parse_range_params(params)
      parsed == parsed.merge(parse_range_params(match))
    end
  end
end
{'fraction_non_alphanumeric': 0.11324786324786325, 'fraction_numerical': 0.012345679012345678, 'mean_word_length': 2.30690737833595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3803528', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1300, 'n_words': 369}