[gnome-2048] Hey, uint64.try_parse exists!



commit 4f7f73890ea45143cadb263db355cea66635d4a9
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Feb 6 12:32:01 2019 +0100

    Hey, uint64.try_parse exists!

 src/game.vala |  6 +++---
 src/grid.vala | 32 +++++++++++++++-----------------
 2 files changed, 18 insertions(+), 20 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index 0a0fc48..1bce5c3 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -165,10 +165,10 @@ private class Game : Object
         string last_line = lines [lines.length - 1];    // not UNIX?
         if (last_line == "")
             last_line = lines [lines.length - 2];
-        int64 score_64;
-        if (!int64.try_parse (last_line, out score_64))
+        uint64 score_64;
+        if (!uint64.try_parse (last_line, out score_64))
             return false;
-        if (score_64 < 0 || score_64 > (int64) uint.MAX)
+        if (score_64 > (uint64) uint.MAX)
             return false;
         score = (uint) score_64;
 
diff --git a/src/grid.vala b/src/grid.vala
index 728b03e..44602a7 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -518,17 +518,20 @@ private class Grid : Object
         if (tokens.length != 2)
             return false;
 
-        int64 rows_64;
-        if (!int64.try_parse (tokens [0], out rows_64))
+        uint64 number_64;
+        // rows
+        if (!uint64.try_parse (tokens [0], out number_64))
             return false;
-        int64 cols_64;
-        if (!int64.try_parse (tokens [1], out cols_64))
+        if ((number_64 == 0) || (number_64 > 9))
             return false;
-
-        if ((rows_64 < 1) || (cols_64 < 1) || (rows_64 > 9) || (cols_64 > 9))
+        int rows = (int) number_64;
+        // cols
+        if (!uint64.try_parse (tokens [1], out number_64))
+            return false;
+        if ((number_64 == 0) || (number_64 > 9))
             return false;
-        int rows = (int) rows_64;
-        int cols = (int) cols_64;
+        int cols = (int) number_64;
+
         if (Application.is_disallowed_grid_size (ref rows, ref cols))
             return false;
         // number of rows + 1 for size + 1 for score; maybe an empty line at end
@@ -546,10 +549,10 @@ private class Grid : Object
 
             for (uint j = 0; j < cols; j++)
             {
-                if (!int64.try_parse (tokens [j], out cols_64))
+                if (!uint64.try_parse (tokens [j], out number_64))
                     return false;
                 uint8 number;
-                if (!_convert_tile_number (ref cols_64, out number))
+                if (!_convert_tile_number (ref number_64, out number))
                     return false;
                 grid [i, j] = number;
             }
@@ -558,21 +561,16 @@ private class Grid : Object
         return true;
     }
 
-    private static inline bool _convert_tile_number (ref int64 number_64,
+    private static inline bool _convert_tile_number (ref uint64 number_64,
                                                      out uint8 number)
     {
-        if (number_64 < 0)
-        {
-            number = 0; // garbage
-            return false;
-        }
         if (number_64 == 0)
         {
             number = 0;
             return true;
         }
         for (number = 1; number <= 81; number++)
-            if (Math.pow (2, number) == (double) number_64)
+            if (number_64 == (uint64) Math.pow (2, number))
                 return true;
 
         return false;


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