Improving line error reporter
Publish PHP Package / docker (push) Successful in 7s
Details
Publish PHP Package / docker (push) Successful in 7s
Details
This commit is contained in:
parent
1a24065c72
commit
d56e6dceea
19
jzon.inc.php
19
jzon.inc.php
|
@ -10,18 +10,15 @@ function jzon_show_position(int $p, string $in, int $context_lines = 5) : string
|
||||||
{
|
{
|
||||||
$out = array();
|
$out = array();
|
||||||
$lines = explode("\n", $in);
|
$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)
|
foreach($lines as $line_idx => $line)
|
||||||
{
|
{
|
||||||
//adding line numbers and normalizing tabs by converting them to spaces
|
//normalizing tabs by converting them to spaces
|
||||||
$out[] = sprintf('%0'.$fmt_num.'d', ($line_idx+1)) . ' ' . str_replace("\t", " ", $line);
|
$out[] = str_replace("\t", " ", $line);
|
||||||
$left = $p - (strlen($line) + 1/*taking into account \n*/);
|
$left = $p - (strlen($line) + 1/*taking into account \n*/);
|
||||||
//var_dump($left, $p, $line, $fmt_num);
|
|
||||||
if($left <= 0)
|
if($left <= 0)
|
||||||
{
|
{
|
||||||
$arrow = str_repeat('-', $fmt_num) . '-'; //line numbers
|
$arrow = '';
|
||||||
//let's normalize tabs
|
//let's take into account tabs
|
||||||
for($i=0;$i<$p;++$i)
|
for($i=0;$i<$p;++$i)
|
||||||
$arrow .= ($line[$i] === "\t" ? '----' : '-');
|
$arrow .= ($line[$i] === "\t" ? '----' : '-');
|
||||||
$arrow .= '^';
|
$arrow .= '^';
|
||||||
|
@ -33,6 +30,14 @@ function jzon_show_position(int $p, string $in, int $context_lines = 5) : string
|
||||||
}
|
}
|
||||||
if(count($out) > $context_lines)
|
if(count($out) > $context_lines)
|
||||||
$out = array_slice($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);
|
return implode("\n", $out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue