<commit_before><?php
/* Osmium
 * Copyright (C) 2012, 2013 Romain "Artefact2" Dalmaso <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Osmium\Chrome;

const F_USED_SHOW_ABSOLUTE = 1;
const F_USED_SHOW_DIFFERENCE = 2;
const F_USED_SHOW_PERCENTAGE = 4;
const F_USED_SHOW_PROGRESS_BAR = 8;

require __DIR__.'/chrome-layout.php';
require __DIR__.'/chrome-fit.php';

/* Format a number with a fixed number of significant digits.
 *
 * @param $sd number of significant digits. If negative, keep all
 * significant digits.
 *
 * @param $min show this suffix at least (must be one of k, m or b)
 */
function format($number, $sd = 3, $min = '') {
	static $suffixes = [
		'b' => 1e9,
		'm' => 1e6,
		'k' => 1e3,
		'' => 1
	];

	if($number < 0) return '-'.format($number, $sd, $min);

	if($sd < 0) return number_format($number / $suffixes[$min]).$min;

	foreach($suffixes as $s => $limit) {
		if($number >= $limit || $s === $min) {
			return round_sd($number / $limit, $sd - 1).$s;
		}
	}
}

/** Much better than the junk trim() function from the PHP library */
function trim($s) {
	return preg_replace('%^\p{Z}*(.*?)\p{Z}*$%u', '$1', $s);
}

function format_small_progress_bar($percent, $fill = true, $ltr = true, $maxoverflow = 10) {
	$over = min($maxoverflow, max(0, $percent - 100));

	$bclass = $ltr ? 'lbar' : 'bar';
	$oclass = $ltr ? 'bar' : 'lbar';
	if($percent > 100) {
		$c = 'p100';
	} else if($percent >= 95) {
		$c = 'p95';
	} else if($percent >= 80) {
		$c = 'p80';
	} else {
		$c = 'p00';
	}
	$progress = max(0, min(100, $percent));

	$ret = "<span class='{$bclass} {$c}' style='width: ".round($progress, 2)."%;'></span>";
	if($over > 0) {
		$over = round($over, 2).'%';
		$ret .= "<span class='{$oclass} {$c} over' style='width: {$over}; "
			.($ltr ? 'right' : 'left').": -{$over};'></span>";
	}
	if($fill) {
		$ret .= "<span class='bar fill'></span>";
	}

	return $ret;
}

function format_used($rawused, $rawtotal, $unit, $digits, $opts = F_USED_SHOW_ABSOLUTE) {
	if($rawtotal == 0 && $rawused == 0) {
		return '0';
	}

	$lines = array();
	if($unit !== '') $unit = ' '.$unit;

	$used = format_number($rawused).' / '.format_number($rawtotal).$unit;
	$diff = ($rawtotal >= $rawused) ?
		format_number($rawtotal - $rawused).$unit.' left' :
		format_number($rawused - $rawtotal).$unit.' over';

	if($opts & F_USED_SHOW_ABSOLUTE) {
		$lines[] = "<span title='".htmlspecialchars($diff, ENT_QUOTES)."'>"
			.htmlspecialchars($used)
			."</span>";
	}
	if($opts & F_USED_SHOW_DIFFERENCE) {
		$lines[] = "<span title='".htmlspecialchars($used, ENT_QUOTES)."'>"
			.htmlspecialchars($diff)
			."</span>";
	}

    $percent = $rawtotal > 0 ? (100 * $rawused / $rawtotal) : 100;
	if($opts & F_USED_SHOW_PERCENTAGE) {
		$lines[] = ($rawtotal == 0 ? '∞' : round($percent, $digits)).' %';
	}

	$ret = implode("<br />", $lines);

	if($opts & F_USED_SHOW_PROGRESS_BAR) {
		$ret .= format_small_progress_bar($percent);
	}

	return $ret;
}

function format_number($num, $precisionoffset = 0) {
	$num = floatval($num);
	if($num < 0) {
		$sign = '-';
		$num = -$num;
	} else {
		$sign = '';
	}

	if($num < 10000) return $sign.round($num, max(0, 1 + $precisionoffset));
	else if($num < 1000000) {
		return $sign.round($num / 1000, max(0, 2 + $precisionoffset)).'k';
	} else {
		return $sign.round($num / 1000000, max(0, 3 + $precisionoffset)).'m';
	}
}

function format_duration($seconds) {
	$s = fmod($seconds, 60);
	$m = round($seconds - $s) / 60;

	$s = floor($s);

	if($s == 0 && $m == 0) return '0s';
	else {
		$k = '';
		if($m != 0) { $k .= $m.'m'; }
		if($s != 0) { $k .= $s.'s'; }
		return $k;
	}
}

function format_long_duration($seconds, $precision = 6) {
	list($y, $m, $d, $h, $i, $s) = explode('-', date('Y-m-d-H-i-s', time() - $seconds));
	list($Y, $M, $D, $H, $I, $S) = explode('-', date('Y-m-d-H-i-s', time()));

	$years = $Y - $y;
	$months = $M - $m;
	$days = $D - $d;
	$hours = $H - $h;
	$minutes = $I - $i;
	$seconds = $S - $s;

	while($seconds < 0) {
		--$minutes;
		$seconds += 60;
	}
	while($minutes < 0) {
		--$hours;
		$minutes += 60;
	}
	while($hours < 0) {
		--$days;
		$hours += 24;
	}
	while($days < 0) {
		--$months;
		$days += 30;
	}
	while($months < 0) {
		--$years;
		$months += 12;
	}

	$out = array(
		'year' => $years,<commit_msg>Use simpler logic in format_long_duration().<commit_after><?php
/* Osmium
 * Copyright (C) 2012, 2013 Romain "Artefact2" Dalmaso <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Osmium\Chrome;

const F_USED_SHOW_ABSOLUTE = 1;
const F_USED_SHOW_DIFFERENCE = 2;
const F_USED_SHOW_PERCENTAGE = 4;
const F_USED_SHOW_PROGRESS_BAR = 8;

require __DIR__.'/chrome-layout.php';
require __DIR__.'/chrome-fit.php';

/* Format a number with a fixed number of significant digits.
 *
 * @param $sd number of significant digits. If negative, keep all
 * significant digits.
 *
 * @param $min show this suffix at least (must be one of k, m or b)
 */
function format($number, $sd = 3, $min = '') {
	static $suffixes = [
		'b' => 1e9,
		'm' => 1e6,
		'k' => 1e3,
		'' => 1
	];

	if($number < 0) return '-'.format($number, $sd, $min);

	if($sd < 0) return number_format($number / $suffixes[$min]).$min;

	foreach($suffixes as $s => $limit) {
		if($number >= $limit || $s === $min) {
			return round_sd($number / $limit, $sd - 1).$s;
		}
	}
}

/** Much better than the junk trim() function from the PHP library */
function trim($s) {
	return preg_replace('%^\p{Z}*(.*?)\p{Z}*$%u', '$1', $s);
}

function format_small_progress_bar($percent, $fill = true, $ltr = true, $maxoverflow = 10) {
	$over = min($maxoverflow, max(0, $percent - 100));

	$bclass = $ltr ? 'lbar' : 'bar';
	$oclass = $ltr ? 'bar' : 'lbar';
	if($percent > 100) {
		$c = 'p100';
	} else if($percent >= 95) {
		$c = 'p95';
	} else if($percent >= 80) {
		$c = 'p80';
	} else {
		$c = 'p00';
	}
	$progress = max(0, min(100, $percent));

	$ret = "<span class='{$bclass} {$c}' style='width: ".round($progress, 2)."%;'></span>";
	if($over > 0) {
		$over = round($over, 2).'%';
		$ret .= "<span class='{$oclass} {$c} over' style='width: {$over}; "
			.($ltr ? 'right' : 'left').": -{$over};'></span>";
	}
	if($fill) {
		$ret .= "<span class='bar fill'></span>";
	}

	return $ret;
}

function format_used($rawused, $rawtotal, $unit, $digits, $opts = F_USED_SHOW_ABSOLUTE) {
	if($rawtotal == 0 && $rawused == 0) {
		return '0';
	}

	$lines = array();
	if($unit !== '') $unit = ' '.$unit;

	$used = format_number($rawused).' / '.format_number($rawtotal).$unit;
	$diff = ($rawtotal >= $rawused) ?
		format_number($rawtotal - $rawused).$unit.' left' :
		format_number($rawused - $rawtotal).$unit.' over';

	if($opts & F_USED_SHOW_ABSOLUTE) {
		$lines[] = "<span title='".htmlspecialchars($diff, ENT_QUOTES)."'>"
			.htmlspecialchars($used)
			."</span>";
	}
	if($opts & F_USED_SHOW_DIFFERENCE) {
		$lines[] = "<span title='".htmlspecialchars($used, ENT_QUOTES)."'>"
			.htmlspecialchars($diff)
			."</span>";
	}

    $percent = $rawtotal > 0 ? (100 * $rawused / $rawtotal) : 100;
	if($opts & F_USED_SHOW_PERCENTAGE) {
		$lines[] = ($rawtotal == 0 ? '∞' : round($percent, $digits)).' %';
	}

	$ret = implode("<br />", $lines);

	if($opts & F_USED_SHOW_PROGRESS_BAR) {
		$ret .= format_small_progress_bar($percent);
	}

	return $ret;
}

function format_number($num, $precisionoffset = 0) {
	$num = floatval($num);
	if($num < 0) {
		$sign = '-';
		$num = -$num;
	} else {
		$sign = '';
	}

	if($num < 10000) return $sign.round($num, max(0, 1 + $precisionoffset));
	else if($num < 1000000) {
		return $sign.round($num / 1000, max(0, 2 + $precisionoffset)).'k';
	} else {
		return $sign.round($num / 1000000, max(0, 3 + $precisionoffset)).'m';
	}
}

function format_duration($seconds) {
	$s = fmod($seconds, 60);
	$m = round($seconds - $s) / 60;

	$s = floor($s);

	if($s == 0 && $m == 0) return '0s';
	else {
		$k = '';
		if($m != 0) { $k .= $m.'m'; }
		if($s != 0) { $k .= $s.'s'; }
		return $k;
	}
}

function format_long_duration($seconds, $precision = 6) {
	list($y, $m, $d, $h, $i, $s) = explode('-', date('Y-m-d-H-i-s', 0));
	list($Y, $M, $D, $H, $I, $S) = explode('-', date('Y-m-d-H-i-s', $seconds));

	$years = $Y - $y;
	$months = $M - $m;
	$days = $D - $d;
	$hours = $H - $h;
	$minutes = $I - $i;
	$seconds = $S - $s;

	$out = array(
		'year' => $years,
{'fraction_non_alphanumeric': 0.2146454049517415, 'fraction_numerical': 0.024129248845992445, 'mean_word_length': 3.6030902945436987, 'pattern_counts': {'":': 2, '<': 48, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1594345', 'n_tokens_mistral': 4089, 'n_tokens_neox': 3699, 'n_words': 975}