Improving line error reporter
Publish PHP Package / docker (push) Successful in 7s Details

This commit is contained in:
Pavel Shevaev 2025-02-28 01:15:44 +03:00
parent 1a24065c72
commit d56e6dceea
1 changed files with 12 additions and 7 deletions

View File

@ -10,18 +10,15 @@ function jzon_show_position(int $p, string $in, int $context_lines = 5) : string
{
$out = array();
$lines = explode("\n", $in);
//let's find out the maximum leading zeros for line numbers
$fmt_num = (int)round(log10(count($lines)))+1;
foreach($lines as $line_idx => $line)
{
//adding line numbers and normalizing tabs by converting them to spaces
$out[] = sprintf('%0'.$fmt_num.'d', ($line_idx+1)) . ' ' . str_replace("\t", " ", $line);
//normalizing tabs by converting them to spaces
$out[] = str_replace("\t", " ", $line);
$left = $p - (strlen($line) + 1/*taking into account \n*/);
//var_dump($left, $p, $line, $fmt_num);
if($left <= 0)
{
$arrow = str_repeat('-', $fmt_num) . '-'; //line numbers
//let's normalize tabs
$arrow = '';
//let's take into account tabs
for($i=0;$i<$p;++$i)
$arrow .= ($line[$i] === "\t" ? '----' : '-');
$arrow .= '^';
@ -33,6 +30,14 @@ function jzon_show_position(int $p, string $in, int $context_lines = 5) : string
}
if(count($out) > $context_lines)
$out = array_slice($out, -$context_lines);
//let's add line numbers
end($out);
//let's find out the maximum leading zeros for line numbers
$fmt_num = (int)round(log10(key($out)))+1;
foreach($out as $idx => $line)
$out[$idx] = sprintf('%0'.$fmt_num.'d', ($idx+1)) . ' ' . $line;
return implode("\n", $out);
}