[gnome-calculator] Do not allow adding variables with predefined constant names



commit 34dccbce1a4c294fe912c1a9325dc6677d5d81e0
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Jun 5 17:09:05 2020 +0300

    Do not allow adding variables with predefined constant names

 lib/equation-parser.vala       | 12 ++++++++++++
 lib/equation.vala              | 12 ------------
 src/math-variable-popover.vala |  2 +-
 3 files changed, 13 insertions(+), 13 deletions(-)
---
diff --git a/lib/equation-parser.vala b/lib/equation-parser.vala
index ea92d6e4..07b94e70 100644
--- a/lib/equation-parser.vala
+++ b/lib/equation-parser.vala
@@ -982,6 +982,18 @@ public class Parser
     private int error_token_end;
     private uint representation_base;
 
+    public static HashTable<string, Number> CONSTANTS;
+
+    static construct {
+        CONSTANTS = new HashTable<string, Number> (str_hash, str_equal);
+        CONSTANTS.insert ("e", new Number.eulers ());
+        CONSTANTS.insert ("pi", new Number.pi ());
+        CONSTANTS.insert ("tau", new Number.tau ());
+        CONSTANTS.insert ("π", new Number.pi ());
+        CONSTANTS.insert ("τ", new Number.tau ());
+        CONSTANTS.insert ("i", new Number.i ());
+    }
+
     public Parser (string input, int number_base, int wordlen, AngleUnit angle_units)
     {
         this.input = input;
diff --git a/lib/equation.vala b/lib/equation.vala
index 98cbd8b8..e6e3a31b 100644
--- a/lib/equation.vala
+++ b/lib/equation.vala
@@ -157,18 +157,6 @@ private class EquationParser : Parser
 {
     private Equation equation;
 
-    private static HashTable<string, Number> CONSTANTS;
-
-    static construct {
-        CONSTANTS = new HashTable<string, Number> (str_hash, str_equal);
-        CONSTANTS.insert ("e", new Number.eulers ());
-        CONSTANTS.insert ("pi", new Number.pi ());
-        CONSTANTS.insert ("tau", new Number.tau ());
-        CONSTANTS.insert ("π", new Number.pi ());
-        CONSTANTS.insert ("τ", new Number.tau ());
-        CONSTANTS.insert ("i", new Number.i ());
-    }
-
     public EquationParser (Equation equation, string expression)
     {
         base (expression, equation.base, equation.wordlen, equation.angle_units);
diff --git a/src/math-variable-popover.vala b/src/math-variable-popover.vala
index 1c387865..9677f108 100644
--- a/src/math-variable-popover.vala
+++ b/src/math-variable-popover.vala
@@ -98,7 +98,7 @@ public class MathVariablePopover : Gtk.Popover
     private void store_variable_cb (Gtk.Widget widget)
     {
         var name = variable_name_entry.get_text ();
-        if (name == "" || name in RESERVED_VARIABLE_NAMES)
+        if (name == "" || name in RESERVED_VARIABLE_NAMES || name in Parser.CONSTANTS)
             return;
 
         var z = equation.number;


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