<commit_before><?php

namespace HandlerSocket;

class ReadSocket
{
	const EOL = "\n";
	const SEP = "\t";
	const NULL = "\0";
	const ESC = "\1";
	const ESC_SHIFT = 0x40;
	
	protected $socket = NULL;
	/**
	 * @var array
	 */
	protected $indexes = array();
	/**
	 * @var iteger
	 */
	protected $currindex = 1;
	
	/**
	 * Connect to Handler Socket
	 *
	 * @param string $server
	 * @param integer $port
	 * @throws \HandlerSocket\IOException
	 */
	public function connect($server='localhost',$port=9998)
	{
		$addr = "tcp://$server:$port";
		$this->socket = stream_socket_client($addr,$errc,$errs,STREAM_CLIENT_CONNECT);
		if(!$this->socket)
			throw new \HandlerSocket\IOException("Connection to $server:$port failed");
	}
	
	public function __destruct()
	{
		$this->disconnect();
	}
	
	/**
	 * Disconnect from server
	 */
	public function disconnect()
	{
		@fclose($this->socket);
		$this->socket = NULL;
		$this->indexes = array();
		$this->currindex = 1;
	}
	
	/**
	 * Is connected
	 *
	 * @return bool
	 */
	public function isConnected()
	{
		return is_resource($this->socket);
	}
	
	/**
	 * Receive one string from server,string havn't trailing \n
	 *
	 * @param bool $read Specifies socket
	 * @throws \HandlerSocket\IOException
	 * @return string
	 */
	protected function recvStr($read = true)
	{
		$str = @fgets($this->socket);
		if(!$str)
		{
			$this->disconnect();
			throw new \HandlerSocket\IOException('Cannot read from socket');
		}
		return substr($str,0,-1);
	}
	
	/**
	 * Send command to server
	 *
	 * @throws \HandlerSocket\IOException
	 * @param string $string
	 */
	protected function sendStr($string)
	{
		if(!$this->isConnected()) throw new \HandlerSocket\IOException('No active connection');
		$string = (string)$string;                                                                   
		while ($string)
		{                                                                                           
			$bytes = @fwrite($this->socket, $string);                                                              
			if($bytes === false)
			{
				$this->disconnect();
				throw new \HandlerSocket\IOException('Cannot write to socket');
			}
			if ($bytes == 0)
			{
				return;
			}
			$string = substr($string, $bytes);                                                                      
		}
	}
	
	/**
	 * Encode string for sending to server
	 *
	 * @param string $string
	 * @return string
	 */
	protected function encodeString($string)
	{
		$encoded = '';
		$len = strlen($string);
		for($t=0;$t<$len;$t++)
		{
			if(ord($string[$t])>0x0f)
				$encoded.=$string[$t];
			else
				$encoded.=self::ESC.chr(ord($string[$t])+self::ESC_SHIFT);
		}
		return $encoded;
	}
	
	/**
	 * Decode string from server
	 *
	 * @param string $encoded
	 * @return string
	 */
	protected function decodeString($encoded)
	{
		$string = '';
		$len = strlen($encoded);
		for($t=0;$t<$len;$t++)
		{
			if(ord($encoded[$t])!=self::ESC)
				$string.=$encoded[$t];
			else
				$string.=chr(ord($encoded[++$t])-self::ESC_SHIFT);
		}
		return $string;
	}
	
	/**
	 * Read response from server
	 *
	 * @return \HandlerSocket\ErrorMessage
	 * @return array
	 */
	public function readResponse()
	{
		$response = $this->recvStr();
		$vals = explode(self::SEP,$response);
		if($vals[0]!=0)
		{
			//error occured
			return new \HandlerSocket\ErrorMessage($vals[2],$vals[0]);
		}
		else
		{
			$numcols = intval($vals[1]);
			$result = array();
			reset($vals);
			next($vals);
			$group = array();<commit_msg>Fixed bug with no NULL recognition Optimized encode and decode for more speed<commit_after><?php

namespace HandlerSocket;

class ReadSocket
{
	const EOL = "\n";
	const SEP = "\t";
	const NULL = "\0";
	const ESC = "\1";
	const ESC_SHIFT = 0x40;
	
	protected $socket = NULL;
	/**
	 * @var array
	 */
	protected $indexes = array();
	/**
	 * @var iteger
	 */
	protected $currindex = 1;
	
	/**
	 * Connect to Handler Socket
	 *
	 * @param string $server
	 * @param integer $port
	 * @throws \HandlerSocket\IOException
	 */
	public function connect($server='localhost',$port=9998)
	{
		$addr = "tcp://$server:$port";
		$this->socket = stream_socket_client($addr,$errc,$errs,STREAM_CLIENT_CONNECT);
		if(!$this->socket)
			throw new \HandlerSocket\IOException("Connection to $server:$port failed");
	}
	
	public function __destruct()
	{
		$this->disconnect();
	}
	
	/**
	 * Disconnect from server
	 */
	public function disconnect()
	{
		@fclose($this->socket);
		$this->socket = NULL;
		$this->indexes = array();
		$this->currindex = 1;
	}
	
	/**
	 * Is connected
	 *
	 * @return bool
	 */
	public function isConnected()
	{
		return is_resource($this->socket);
	}
	
	/**
	 * Receive one string from server,string havn't trailing \n
	 *
	 * @param bool $read Specifies socket
	 * @throws \HandlerSocket\IOException
	 * @return string
	 */
	protected function recvStr($read = true)
	{
		$str = @fgets($this->socket);
		if(!$str)
		{
			$this->disconnect();
			throw new \HandlerSocket\IOException('Cannot read from socket');
		}
		return substr($str,0,-1);
	}
	
	/**
	 * Send command to server
	 *
	 * @throws \HandlerSocket\IOException
	 * @param string $string
	 */
	protected function sendStr($string)
	{
		if(!$this->isConnected()) throw new \HandlerSocket\IOException('No active connection');
		$string = (string)$string;                                                                   
		while ($string)
		{                                                                                           
			$bytes = @fwrite($this->socket, $string);                                                              
			if($bytes === false)
			{
				$this->disconnect();
				throw new \HandlerSocket\IOException('Cannot write to socket');
			}
			if ($bytes == 0)
			{
				return;
			}
			$string = substr($string, $bytes);                                                                      
		}
	}
	
	/**
	 * Encode string for sending to server
	 *
	 * @param string $string
	 * @return string
	 */
	protected function encodeString($string)
	{
		if(is_null($string))
		{
			return "\0";
		}
		else
		{
            return strtr($string,
						 array(	"\x00" => "\x01\x40",
								"\x01" => "\x01\x41",
								"\x02" => "\x01\x42",
								"\x03" => "\x01\x43",
								"\x04" => "\x01\x44",
								"\x05" => "\x01\x45",
								"\x06" => "\x01\x46",
								"\x07" => "\x01\x47",
								"\x08" => "\x01\x48",
								"\x09" => "\x01\x49",
								"\x0A" => "\x01\x4A",
								"\x0B" => "\x01\x4B",
								"\x0C" => "\x01\x4C",
								"\x0D" => "\x01\x4D",
								"\x0E" => "\x01\x4E",
								"\x0F" => "\x01\x4F"));
        }
	}
	
	/**
	 * Decode string from server
	 *
	 * @param string $encoded
	 * @return string
	 */
	protected function decodeString($encoded)
	{
		if($encoded === "\0")
		{
            return NULL;
		}
        else
		{
            return strtr($encoded,
						 array(	"\x01\x40" => "\x00",
								"\x01\x41" => "\x01",
								"\x01\x42" => "\x02",
								"\x01\x43" => "\x03",
								"\x01\x44" => "\x04",
								"\x01\x45" => "\x05",
								"\x01\x46" => "\x06",
								"\x01\x47" => "\x07",
								"\x01\x48" => "\x08",
								"\x01\x49" => "\x09",
								"\x01\x4A" => "\x0A",
								"\x01\x4B" => "\x0B",
								"\x01\x4C" => "\x0C",
								"\x01\x4D" => "\x0D",
								"\x01\x4E" => "\x0E",
								"\x01\x4F" => "\x0F"));
		}
	}
	
	/**
	 * Read response from server
	 *
	 * @return \HandlerSocket\ErrorMessage
	 * @return array
	 */
	public function readResponse()
	{
		$response = $this->recvStr();
		$vals = explode(self::SEP,$response);
		if($vals[0]!=0)
		{
			//error occured
			return new \HandlerSocket\ErrorMessage($vals[2],$vals[0]);
		}
		else
		{
			$numcols = intval($vals[1]);
			$result = array();
			reset($vals);
			next($vals);
			$group = array();
{'fraction_non_alphanumeric': 0.1788232277913088, 'fraction_numerical': 0.02717600307652865, 'mean_word_length': 2.0862341772151898, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4189771', 'n_tokens_mistral': 3201, 'n_tokens_neox': 2562, 'n_words': 540}