<commit_before><?php
namespace Slik\Uncia;
use Slik\Uncia\Exception\SyntaxError;

function run($cmd, $values = null, $options = null)
{
	if (strpos($cmd, '?') === false && is_null($options)) {
		$options = $values;
		$values = null;
	}

	$cmd = _run_placeholders($cmd, $values);

	$options = (object) array_merge(array(
		'stdin' => null,
	), $options ?: array());

	$desc = array(
		0 => array('pipe', 'r'),
		1 => array('pipe', 'w'),
		2 => array('pipe', 'w'),
	);

	$process = proc_open($cmd, $desc, $pipes);

	if ($options->stdin) {
		fwrite($pipes[0], $options->stdin);
	}
	fclose($pipes[0]);

	$stdout = stream_get_contents($pipes[1]);
	fclose($pipes[1]);

	$stderr = stream_get_contents($pipes[2]);
	fclose($pipes[2]);

	$code = proc_close($process);

	if ($code > 0) {
		$error = trim($stderr) ?: trim($stdout);
		throw new Exception\RunError($error);
	}

	return $stdout;
};

function runthru($cmd, $values = null)
{
	$cmd = _run_placeholders($cmd, $values);
	passthru($cmd, $code);
	if ($code > 0) {
		throw new Exception\RunError('Command failed with code '.$code);
	}
}

function _run_placeholders($cmd, $values)
{
	$values = (array) $values ?: array();

	if (preg_match_all('/(?<= )\\?($| )/', $cmd) !== count($values)) {
		throw new SyntaxError('Parameter count does not match values count');
	}

	foreach ($values as $v) {
		$v = escapeshellarg($v);
		$v = str_replace('\\', '\\\\', str_replace('?', '\\?', $v));
		$cmd = preg_replace('/(?<= )\\?($| )/', $v.'\1', $cmd, 1);
	}

	$cmd = str_replace('\\?', '?', $cmd);

	return $cmd;
}<commit_msg>run: fix for question mark at the beginning<commit_after><?php
namespace Slik\Uncia;
use Slik\Uncia\Exception\SyntaxError;

function run($cmd, $values = null, $options = null)
{
	if (strpos($cmd, '?') === false && is_null($options)) {
		$options = $values;
		$values = null;
	}

	$cmd = _run_placeholders($cmd, $values);

	$options = (object) array_merge(array(
		'stdin' => null,
	), $options ?: array());

	$desc = array(
		0 => array('pipe', 'r'),
		1 => array('pipe', 'w'),
		2 => array('pipe', 'w'),
	);

	$process = proc_open($cmd, $desc, $pipes);

	if ($options->stdin) {
		fwrite($pipes[0], $options->stdin);
	}
	fclose($pipes[0]);

	$stdout = stream_get_contents($pipes[1]);
	fclose($pipes[1]);

	$stderr = stream_get_contents($pipes[2]);
	fclose($pipes[2]);

	$code = proc_close($process);

	if ($code > 0) {
		$error = trim($stderr) ?: trim($stdout);
		throw new Exception\RunError($error);
	}

	return $stdout;
};

function runthru($cmd, $values = null)
{
	$cmd = _run_placeholders($cmd, $values);
	passthru($cmd, $code);
	if ($code > 0) {
		throw new Exception\RunError('Command failed with code '.$code);
	}
}

function _run_placeholders($cmd, $values)
{
	$values = (array) $values ?: array();

	$questions = '/^\\?(?= )|(?<= )\\?($| )/';

	if (preg_match_all($questions, $cmd) !== count($values)) {
		throw new SyntaxError('Parameter count does not match values count');
	}

	foreach ($values as $v) {
		$v = escapeshellarg($v);
		$v = str_replace('\\', '\\\\', str_replace('?', '\\?', $v));
		$cmd = preg_replace($questions, $v.'\1', $cmd, 1);
	}

	$cmd = str_replace('\\?', '?', $cmd);

	return $cmd;
}
{'fraction_non_alphanumeric': 0.2393834539163259, 'fraction_numerical': 0.00817867253853413, 'mean_word_length': 4.408163265306122, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 19, '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': '5129372', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1232, 'n_words': 262}