<commit_before>module Excon
  class Response

    def self.parse(socket, params = {}, &block)
      if params[:block]
        print "  \e[33m[WARN] params[:block] is deprecated, please pass the block to the request\e[0m"
        block = params[:block]
      end

      response = new

      response.status = socket.readline[9..11].to_i
      while true
        data = socket.readline.chop!
        unless data.empty?
          key, value = data.split(': ')
          response.headers[key] = value
        else
          break
        end
      end

      unless params[:method].to_s.upcase == 'HEAD'
        if !block || (params[:expects] && ![*params[:expects]].include?(response.status))
          response.body = ''
          block = lambda { |chunk| response.body << chunk }
        end

        if response.headers['Transfer-Encoding'] && response.headers['Transfer-Encoding'].downcase == 'chunked'
          while true
            chunk_size = socket.readline.chop!.to_i(16)
            chunk = socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
            if chunk_size > 0
              block.call(chunk)
            else
              break
            end
          end
        elsif response.headers['Connection'] && response.headers['Connection'].downcase == 'close'
          block.call(socket.read)
        elsif response.headers['Content-Length']
          remaining = response.headers['Content-Length'].to_i
          while remaining > 0
            block.call(socket.read([CHUNK_SIZE, remaining].min))
            remaining -= CHUNK_SIZE
          end
        end
      end

      response
    end

    attr_accessor :body, :headers, :status

    def initialize(attributes = {})
      @body    = attributes[:body] || ''
      @headers = attributes[:headers] || {}
      @status  = attributes[:status]
    end

  end
end<commit_msg>Micro-optimization: replace lambda with a class.<commit_after>module Excon
  class Response

    def self.parse(socket, params = {}, &block)
      if params[:block]
        print "  \e[33m[WARN] params[:block] is deprecated, please pass the block to the request\e[0m"
        block = params[:block]
      end

      response = new

      response.status = socket.readline[9..11].to_i
      while true
        data = socket.readline.chop!
        unless data.empty?
          key, value = data.split(': ')
          response.headers[key] = value
        else
          break
        end
      end

      unless params[:method].to_s.upcase == 'HEAD'
        if !block || (params[:expects] && ![*params[:expects]].include?(response.status))
          response.body = ''
          block = Concatenator.new(response.body)
        end

        if response.headers['Transfer-Encoding'] && response.headers['Transfer-Encoding'].downcase == 'chunked'
          while true
            chunk_size = socket.readline.chop!.to_i(16)
            chunk = socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
            if chunk_size > 0
              block.call(chunk)
            else
              break
            end
          end
        elsif response.headers['Connection'] && response.headers['Connection'].downcase == 'close'
          block.call(socket.read)
        elsif response.headers['Content-Length']
          remaining = response.headers['Content-Length'].to_i
          while remaining > 0
            block.call(socket.read([CHUNK_SIZE, remaining].min))
            remaining -= CHUNK_SIZE
          end
        end
      end

      response
    end

    attr_accessor :body, :headers, :status

    def initialize(attributes = {})
      @body    = attributes[:body] || ''
      @headers = attributes[:headers] || {}
      @status  = attributes[:status]
    end

  end

  class Concatenator
    def initialize(string)
      @string = string
    end

    def call(data)
      @string << data
    end
  end
end
{'fraction_non_alphanumeric': 0.1196691651589558, 'fraction_numerical': 0.0062031532695787026, 'mean_word_length': 1.9496951219512195, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2446822', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1075, 'n_words': 244}