[vala/wip/housekeeping] Don't create constant Regex on demand and use static field where possible



commit e65e41140c2ff685c4a9a943282ca20fd8c06d0d
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Nov 1 10:29:45 2016 +0100

    Don't create constant Regex on demand and use static field where possible

 ccode/valaccodewriter.vala |    7 +++++--
 vala/valacodewriter.vala   |    6 ++++--
 vala/valareport.vala       |    5 +++--
 3 files changed, 12 insertions(+), 6 deletions(-)
---
diff --git a/ccode/valaccodewriter.vala b/ccode/valaccodewriter.vala
index b6a3238..281ff6e 100644
--- a/ccode/valaccodewriter.vala
+++ b/ccode/valaccodewriter.vala
@@ -48,6 +48,8 @@ public class Vala.CCodeWriter {
                get { return _bol; }
        }
 
+       static GLib.Regex fix_indent_regex;
+
        private string temp_filename;
        private bool file_exists;
 
@@ -220,7 +222,8 @@ public class Vala.CCodeWriter {
                        bool first = true;
 
                        // discard tabs at beginning of line
-                       var regex = new GLib.Regex ("^\t+");
+                       if (fix_indent_regex == null)
+                               fix_indent_regex = new GLib.Regex ("^\t+");;
 
                        foreach (unowned string line in text.split ("\n")) {
                                if (!first) {
@@ -229,7 +232,7 @@ public class Vala.CCodeWriter {
                                        first = false;
                                }
 
-                               var lineparts = regex.replace_literal (line, -1, 0, "").split ("*/");
+                               var lineparts = fix_indent_regex.replace_literal (line, -1, 0, "").split 
("*/");
 
                                for (int i = 0; lineparts[i] != null; i++) {
                                        stream.puts (lineparts[i]);
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index 99a9725..8c0e688 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -28,6 +28,8 @@
  * Code visitor generating Vala API file for the public interface.
  */
 public class Vala.CodeWriter : CodeVisitor {
+       static GLib.Regex fix_indent_regex;
+
        private CodeContext context;
        
        FileStream stream;
@@ -1502,9 +1504,9 @@ public class Vala.CodeWriter : CodeVisitor {
        }
 
        private void write_comment (Comment comment) {
-               Regex fix_indent_regex;
                try {
-                       fix_indent_regex = new Regex ("\\n[\\t ]*");
+                       if (fix_indent_regex == null)
+                               fix_indent_regex = new Regex ("\\n[\\t ]*");
                } catch (Error e) {
                        assert_not_reached ();
                }
diff --git a/vala/valareport.vala b/vala/valareport.vala
index 67b3a80..f977dce 100644
--- a/vala/valareport.vala
+++ b/vala/valareport.vala
@@ -100,6 +100,7 @@ public class Vala.Report : Object {
 
        public bool enable_warnings { get; set; default = true; }
 
+       static GLib.Regex val_regex;
 
        /**
         * Set all colors by string
@@ -109,9 +110,9 @@ public class Vala.Report : Object {
         * }}}
         */
        public bool set_colors (string str) {
-               Regex val_regex;
                try {
-                       val_regex = new Regex ("^\\s*[0-9]+(;[0-9]*)*\\s*$");
+                       if (val_regex == null)
+                               val_regex = new Regex ("^\\s*[0-9]+(;[0-9]*)*\\s*$");
                } catch (RegexError e) {
                        assert_not_reached ();
                }


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