validator/httpd/cgi-bin check,1.706,1.707

Update of /sources/public/validator/httpd/cgi-bin
In directory hutz:/tmp/cvs-serv24031

Modified Files:
	check 
Log Message:
Don't truncate source lines that fit within our limits; when truncating,
replace only one char with ellipsis instead of three; simplify truncation
code in general.


Index: check
===================================================================
RCS file: /sources/public/validator/httpd/cgi-bin/check,v
retrieving revision 1.706
retrieving revision 1.707
diff -u -d -r1.706 -r1.707
--- check	4 Oct 2009 20:38:25 -0000	1.706
+++ check	4 Oct 2009 20:42:01 -0000	1.707
@@ -1896,48 +1896,37 @@
 
 #
 # Truncate source lines for report.
-#
+# Expects 1-based column indexes.
 sub truncate_line {
-  my $line  = shift;
-  my $col   = shift;
+  my $line   = shift;
+  my $col    = shift;
+  my $maxlen = 80;    # max line length to truncate to
 
-  my $start = $col;
-  my $end   = $col;
+  my $diff = length($line) - $maxlen;
 
-  for (1..40) {
-    $start-- if ($start - 1 >= 0);            # in/de-crement until...
-    $end++   if ($end   + 1 <= length $line); # ...we hit end of line.
-  }
+  # Don't truncate at all if it fits.
+  return ($line, $col) if ($diff <= 0);
 
-  unless ($end - $start == 80) {
-    if ($start == 0) { # Hit start of line, maybe grab more at end.
-      my $diff = 40 - $col;
-      for (1..$diff) {
-        $end++ if ($end + 1 <= length $line);
-      }
-    } elsif ($end == length $line) { # Hit end of line, maybe grab more at beginning.
-      my $diff = 80 - $col;
-      for (1..$diff) {
-        $start-- if ($start - 1 >= 0);
-      }
-    }
+  my $start = $col - int($maxlen/2);
+  if ($start < 0) {
+    # Truncate only from end of line.
+    $start = 0;
+    $line = substr($line, $start, $maxlen - 1) . '…';
+  }
+  elsif ($start > $diff) {
+    # Truncate only from beginning of line.
+    $start = $diff;
+    $line = '…' . substr($line, $start + 1);
+  }
+  else {
+    # Truncate from both beginning and end of line.
+    $line = '…' . substr($line, $start + 1, $maxlen - 2) . '…';
   }
 
-  #
-  # Add ellipsis at end if necessary.
-  unless ($end   == length $line) {substr $line, -3, 3, '…'};
-
-  $col = $col - $start; # New offset is diff from $col to $start.
-  $line = substr $line, $start, $end - $start; # Truncate.
-
-  #
-  # Add ellipsis at start if necessary.
-  unless ($start == 0)  {
-      substr $line,  0, 3, '…';
-      $col = $col - 2;
-    };
+  # Shift column if we truncated from beginning of line.
+  $col -= $start;
 
-  return $line, $col;
+  return ($line, $col);
 }
 
 #

Received on Sunday, 4 October 2009 20:42:07 UTC