[latexila] Some regex clean-up



commit a01e82ca45fcd2c94d4d576d5724ff52a459490e
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Mon Jul 25 22:23:24 2011 +0200

    Some regex clean-up
    
    Replace [[:space:]] by \\s
    Replace \n by \\R (more portable)
    Replace [0-9] by \\d
    
    And avoid one indentation level.

 src/document_structure.vala   |   37 ++++++++++----------
 src/latex_post_processor.vala |   64 +++++++++++++++++-----------------
 src/post_processors.vala      |   74 ++++++++++++++++++++--------------------
 3 files changed, 87 insertions(+), 88 deletions(-)
---
diff --git a/src/document_structure.vala b/src/document_structure.vala
index 6ac483d..bebc273 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -88,26 +88,25 @@ public class DocumentStructure : GLib.Object
     {
         _doc = doc;
 
-        if (_comment_regex == null)
+        if (_chars_regex != null)
+            return;
+
+        try
         {
-            try
-            {
-                _chars_regex = new Regex ("\\\\|%");
+            _chars_regex = new Regex ("\\\\|%");
 
-                _comment_regex = new Regex (
-                    "^(?P<type>TODO|FIXME)[[:space:]]+:?[[:space:]]*(?P<text>.*)$",
-                    RegexCompileFlags.OPTIMIZE);
+            _comment_regex = new Regex ("^(?P<type>TODO|FIXME)\\s+:?\\s*(?P<text>.*)$",
+                RegexCompileFlags.OPTIMIZE);
 
-                // Stop at the first argument, which can be optional (a '[').
-                // To find the first non-optional argument, it's more robust to do it
-                // character by character.
-                _command_name_regex =
-                    new Regex ("^(?P<name>[a-z]+\\*?)[[:space:]]*(\\[|{)");
-            }
-            catch (RegexError e)
-            {
-                stderr.printf ("Structure: %s\n", e.message);
-            }
+            // Stop at the first argument, which can be optional (a '[').
+            // To find the first non-optional argument, it's more robust to do it
+            // character by character.
+            _command_name_regex = new Regex ("^(?P<name>[a-z]+\\*?)\\s*(\\[|{)",
+                RegexCompileFlags.OPTIMIZE);
+        }
+        catch (RegexError e)
+        {
+            stderr.printf ("Structure: %s\n", e.message);
         }
     }
 
diff --git a/src/latex_post_processor.vala b/src/latex_post_processor.vala
index 5872347..6894544 100644
--- a/src/latex_post_processor.vala
+++ b/src/latex_post_processor.vala
@@ -89,40 +89,40 @@ private class LatexPostProcessor : GLib.Object, PostProcessor
 
     public LatexPostProcessor ()
     {
-        if (reg_badbox == null)
+        reset_msg ();
+
+        if (reg_badbox != null)
+            return;
+
+        try
         {
-            try
-            {
-                reg_badbox = new Regex ("^(Over|Under)full \\\\[hv]box");
-                reg_badbox_lines = new Regex ("(.*) at lines ([0-9]+)--([0-9]+)");
-                reg_badbox_line = new Regex ("(.*) at line ([0-9]+)");
-                reg_badbox_output =
-                    new Regex ("(.*)has occurred while \\output is active");
-
-                reg_warning =
-                    new Regex ("^(((! )?(La|pdf)TeX)|Package|Class) .*Warning[^:]*:[[:space:]]*(.*)",
-                    RegexCompileFlags.CASELESS);
-                reg_warning_no_file = new Regex ("(No file .*)");
-                reg_warning_line = new Regex ("(.*) on input line ([0-9]+)\\.$");
-                reg_warning_international_line = new Regex ("(.*)([0-9]+)\\.$");
-
-                reg_latex_error = new Regex ("^! LaTeX Error: (.*)$");
-                reg_pdflatex_error = new Regex ("^Error: pdflatex (.*)$");
-                reg_tex_error = new Regex ("^! (.*)\\.$");
-                reg_error_line = new Regex ("^l\\.([0-9]+)(.*)");
-
-                reg_file_pop = new Regex ("(\\) )?:<-$");
-                reg_other_bytes = new Regex ("([0-9]+) bytes");
-
-                reg_spaces = new Regex ("[[:space:]]{2,}");
-            }
-            catch (RegexError e)
-            {
-                stderr.printf ("LatexPostProcessor: %s\n", e.message);
-            }
+            reg_badbox = new Regex ("^(Over|Under)full \\\\[hv]box");
+            reg_badbox_lines = new Regex ("(.*) at lines (\\d+)--(\\d+)");
+            reg_badbox_line = new Regex ("(.*) at line (\\d+)");
+            reg_badbox_output =
+                new Regex ("(.*)has occurred while \\output is active");
+
+            reg_warning = new Regex (
+                "^(((! )?(La|pdf)TeX)|Package|Class) .*Warning[^:]*:\\s*(.*)",
+                RegexCompileFlags.CASELESS);
+            reg_warning_no_file = new Regex ("(No file .*)");
+            reg_warning_line = new Regex ("(.*) on input line (\\d+)\\.$");
+            reg_warning_international_line = new Regex ("(.*)(\\d+)\\.$");
+
+            reg_latex_error = new Regex ("^! LaTeX Error: (.*)$");
+            reg_pdflatex_error = new Regex ("^Error: pdflatex (.*)$");
+            reg_tex_error = new Regex ("^! (.*)\\.$");
+            reg_error_line = new Regex ("^l\\.(\\d+)(.*)");
+
+            reg_file_pop = new Regex ("(\\) )?:<-$");
+            reg_other_bytes = new Regex ("(\\d+) bytes");
+
+            reg_spaces = new Regex ("\\s{2,}");
+        }
+        catch (RegexError e)
+        {
+            stderr.printf ("LatexPostProcessor: %s\n", e.message);
         }
-
-        reset_msg ();
     }
 
     public void process (File file, string output, int status)
diff --git a/src/post_processors.vala b/src/post_processors.vala
index cfbf9eb..af36bd4 100644
--- a/src/post_processors.vala
+++ b/src/post_processors.vala
@@ -99,18 +99,18 @@ private class RubberPostProcessor : GLib.Object, PostProcessor
 
     public RubberPostProcessor ()
     {
-        if (pattern == null)
+        if (pattern != null)
+            return;
+
+        try
         {
-            try
-            {
-                pattern = new Regex (
-                    "(?P<file>[^:\n]+)(:(?P<line>[0-9\\-]+))?:(?P<text>.+)$",
-                    RegexCompileFlags.MULTILINE);
-            }
-            catch (RegexError e)
-            {
-                stderr.printf ("RubberPostProcessor: %s\n", e.message);
-            }
+            pattern = new Regex (
+                "(?P<file>[^:\n]+)(:(?P<line>[\\d\\-]+))?:(?P<text>.+)$",
+                RegexCompileFlags.MULTILINE | RegexCompileFlags.OPTIMIZE);
+        }
+        catch (RegexError e)
+        {
+            stderr.printf ("RubberPostProcessor: %s\n", e.message);
         }
     }
 
@@ -186,33 +186,33 @@ private class LatexmkPostProcessor : GLib.Object, PostProcessor
     {
         this.show_all = show_all;
 
-        if (reg_rule == null)
+        if (reg_rule != null)
+            return;
+
+        try
         {
-            try
-            {
-                string reg_rule_str = "^-{12}\n";
-                reg_rule_str += "(?P<line>Run number [0-9]+ of rule '(?P<rule>.*)')\n";
-                reg_rule_str += "(-{12}\n){2}";
-                reg_rule_str += "Running '(?P<cmd>.*)'\n";
-                reg_rule_str += "-{12}\n";
-                reg_rule_str += "Latexmk: applying rule .*\n";
-                reg_rule_str += "(For rule '.*', running .*\n)?";
-                reg_rule_str += "(?P<output>(?U)(.*\n)*)"; // ungreedy
-                reg_rule_str += "(Latexmk:|Rule '.*':)";
-
-                reg_rule = new Regex (reg_rule_str,
-                    RegexCompileFlags.MULTILINE | RegexCompileFlags.OPTIMIZE);
-
-                string reg_no_rule_str = "^(Latexmk: This is Latexmk.*\n)?";
-                reg_no_rule_str += "(\\*{4} Report bugs.*\n)?";
-                reg_no_rule_str += "(?P<output>(.*\n)*)";
-
-                reg_no_rule = new Regex (reg_no_rule_str, RegexCompileFlags.MULTILINE);
-            }
-            catch (RegexError e)
-            {
-                stderr.printf ("LatexmkPostProcessor: %s\n", e.message);
-            }
+            string reg_rule_str = "^-{12}\\R";
+            reg_rule_str += "(?P<line>Run number \\d+ of rule '(?P<rule>.*)')\\R";
+            reg_rule_str += "(-{12}\\R){2}";
+            reg_rule_str += "Running '(?P<cmd>.*)'\\R";
+            reg_rule_str += "-{12}\\R";
+            reg_rule_str += "Latexmk: applying rule .*\\R";
+            reg_rule_str += "(For rule '.*', running .*\\R)?";
+            reg_rule_str += "(?P<output>(?U)(.*\\R)*)"; // ungreedy
+            reg_rule_str += "(Latexmk:|Rule '.*':)";
+
+            reg_rule = new Regex (reg_rule_str,
+                RegexCompileFlags.MULTILINE | RegexCompileFlags.OPTIMIZE);
+
+            string reg_no_rule_str = "^(Latexmk: This is Latexmk.*\\R)?";
+            reg_no_rule_str += "(\\*{4} Report bugs.*\\R)?";
+            reg_no_rule_str += "(?P<output>(.*\\R)*)";
+
+            reg_no_rule = new Regex (reg_no_rule_str, RegexCompileFlags.MULTILINE);
+        }
+        catch (RegexError e)
+        {
+            stderr.printf ("LatexmkPostProcessor: %s\n", e.message);
         }
     }
 



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