<commit_before>##
# States of a connection
class Midori::Connection
  include Midori::Server

  # @!attribute data
  #   @return [String] string buffer of data to send
  attr_accessor :data

  # Init Connection with socket
  # @param [IO] socket raw socket
  def initialize(socket)
    @registered = false
    @socket = socket
    @monitor = nil
    @close_flag = false
    @data = ''
    listen(socket)
  end

  # Register events of connection
  # @param [IO] socket raw socket
  def listen(socket)
    EventLoop.register(socket, :rw) do |monitor|
      @monitor = monitor
      if monitor.readable?
        receive_data(monitor)
      end
      if monitor.writable?
        if !@data.empty?
          # :nocov:
          # Leave for corner cases
          monitor.io.write_nonblock(@data)
          @data = ''
          # :nocov:
        elsif @close_flag
          close_connection
        end
      end
    end
  end

  # Send message to client
  # @param [String] data data to send
  def send_data(data)
    @monitor.writable? ? @socket.write_nonblock(data) : @data << data
  end

  # Close the connection
  def close_connection
    EventLoop.deregister @socket
    @socket.close
  end

  # Close the connection after writing
  def close_connection_after_writing
    @close_flag = true<commit_msg>Improve performance by not using String#empty?<commit_after>##
# States of a connection
class Midori::Connection
  include Midori::Server

  # @!attribute data
  #   @return [String] string buffer of data to send
  attr_accessor :data

  # Init Connection with socket
  # @param [IO] socket raw socket
  def initialize(socket)
    @registered = false
    @socket = socket
    @monitor = nil
    @close_flag = false
    @data = ''
    listen(socket)
  end

  # Register events of connection
  # @param [IO] socket raw socket
  def listen(socket)
    EventLoop.register(socket, :rw) do |monitor|
      @monitor = monitor
      if monitor.readable?
        receive_data(monitor)
      end
      if monitor.writable?
        if !@data == ''
          # :nocov:
          # Leave for corner cases
          monitor.io.write_nonblock(@data)
          @data = ''
          # :nocov:
        elsif @close_flag
          close_connection
        end
      end
    end
  end

  # Send message to client
  # @param [String] data data to send
  def send_data(data)
    @monitor.writable? ? @socket.write_nonblock(data) : @data << data
  end

  # Close the connection
  def close_connection
    EventLoop.deregister @socket
    @socket.close
  end

  # Close the connection after writing
  def close_connection_after_writing
    @close_flag = true
{'fraction_non_alphanumeric': 0.08421851289833081, 'fraction_numerical': 0.0, 'mean_word_length': 2.4927152317880794, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5252538', 'n_tokens_mistral': 792, 'n_tokens_neox': 776, 'n_words': 240}