[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[lintian] 02/02: c/cruft: Rewrite "strip comments" regex



This is an automated email from the git hooks/post-receive script.

nthykier pushed a commit to branch master
in repository lintian.

commit 0803b0c032ab01197c7b5a2c2ef01b15b6f50496
Author: Niels Thykier <niels@thykier.net>
Date:   Sun Aug 30 18:46:02 2015 +0200

    c/cruft: Rewrite "strip comments" regex
    
    Rewrite the "strip comments" regex for readability.  Also add some "no
    backtracking" markers and greedy modifiers where possible.  These
    changes are not enough to avoid the backtrack limit when exposed to
    the full "block", but hopefully they make it slightly faster.
    
    Signed-off-by: Niels Thykier <niels@thykier.net>
---
 checks/cruft.pm | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/checks/cruft.pm b/checks/cruft.pm
index baca13c..69cc690 100644
--- a/checks/cruft.pm
+++ b/checks/cruft.pm
@@ -966,14 +966,23 @@ sub _linelength_test {
         my $strip = substr($block,0,8192);
         # strip indention
         $strip =~ s/^\s+//g;
-        # from perl faq strip comments (split into 3 and rewritten a bit)
-        # - Part 1) Strip /* */ comments
-        $strip =~ s# /\* [^*]*+\*++ (?:[^/*][^*]*+\*++)*+ / ##xgs;
-        # - Part 2) Strip // comments (allowing "\\\n" line continuation)
-        $strip =~ s# // (?:[^\\]++|[^\n][\n]?)*? (?=\n)##xgs;
-        # - Part 3) "De-stringify" elements ("foo" => foo)
-        $strip
-          =~ s#("(\\.|[^"\\]++)*"|'(\\.|[^'\\]++)*'|.[^/"'\\]*+)#defined $1 ? $1 : ""#xgse;
+        # Based on the "strip comments" from the Perl FAQ
+        # (rewritten a bit)
+        $strip =~ s{
+                   # Part 1) Strip /* */ comments
+                     /\* [^*]*+\*++ (?:[^/*][^*]*+\*++)*+ /
+                   # Part 2) Strip // comments (C++-style)
+                   | // (?:[^\\]++|[^\n][\n]?)*? (?=\n)
+                   # Part 3) Retain any non-comment (capture)
+                   | (
+                   # Part 3a) Non-string content
+                          .[^/"'\\]*+
+                   # Part 3b) ""-strings
+                        | "(?:\\.|[^"\\]++)*+"
+                   # Part 3c) ''-blocks
+                        | '(?:\\.|[^'\\]++)*+'
+                   )
+                }{defined $1 ? $1 : ""}xgse;
         # strip empty line
         $strip =~ s/^\s*\n//mg;
         # remove last \n

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/lintian/lintian.git


Reply to: