Compare commits

..

No commits in common. "master" and "v1.1.0" have entirely different histories.

1 changed files with 7 additions and 13 deletions

View File

@ -10,15 +10,17 @@ 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)));
foreach($lines as $line_idx => $line)
{
//normalizing tabs by converting them to spaces
$out[] = str_replace("\t", " ", $line);
//adding line numbers and handling tabs
$out[] = sprintf('%0'.$fmt_num.'d', $line_idx) . ' ' . str_replace("\t", " ", $line);
$left = $p - (strlen($line) + 1/*taking into account \n*/);
if($left <= 0)
//var_dump($left, $p, $line, $fmt_num);
if($left < 0)
{
$arrow = '';
//let's take into account tabs
$arrow = str_repeat('-', $fmt_num) . '-'; //line numbers
for($i=0;$i<$p;++$i)
$arrow .= ($line[$i] === "\t" ? '----' : '-');
$arrow .= '^';
@ -30,14 +32,6 @@ 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);
}