From d56e6dceea80b2d5146abdcd99595f88e1ff9d7e Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Fri, 28 Feb 2025 01:15:44 +0300 Subject: [PATCH] Improving line error reporter --- jzon.inc.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/jzon.inc.php b/jzon.inc.php index 03699f8..4c6168c 100644 --- a/jzon.inc.php +++ b/jzon.inc.php @@ -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); }