[vala/0.40] glib-2.0: Add float.parse/try_parse()



commit 259dfa49a4f6ae475c4513289547f72e216496e3
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Aug 16 08:16:44 2018 +0200

    glib-2.0: Add float.parse/try_parse()
    
    https://gitlab.gnome.org/GNOME/vala/issues/649

 tests/basic-types/floats.vala | 72 +++++++++++++++++++++++++++++++++++++++++++
 vapi/glib-2.0.vapi            | 19 ++++++++++++
 2 files changed, 91 insertions(+)
---
diff --git a/tests/basic-types/floats.vala b/tests/basic-types/floats.vala
index e0e8aef6b..903a4c1bf 100644
--- a/tests/basic-types/floats.vala
+++ b/tests/basic-types/floats.vala
@@ -45,6 +45,9 @@ void test_double () {
        string s = d.to_string ();
        assert (s == "42");
 
+       d = double.parse ("47.11mm");
+       assert (d == 47.11);
+
        unowned string unparsed;
        double.try_parse ("3.45mm", out d, out unparsed);
        assert (d == 3.45);
@@ -63,6 +66,75 @@ void test_double () {
        assert (d2 == 10);
 }
 
+void test_float () {
+       // declaration and initialization
+       float f = 42f;
+       assert (f == 42f);
+
+       // assignment
+       f = 23f;
+       assert (f == 23f);
+
+       // access
+       float g = f;
+       assert (g == 23f);
+
+       // +
+       f = 42f + 23f;
+       assert (f == 65f);
+
+       // -
+       f = 42f - 23f;
+       assert (f == 19f);
+
+       // *
+       f = 42f * 23f;
+       assert (f == 966f);
+
+       // /
+       f = 42f / 23f;
+       assert (f > 1.8);
+       assert (f < 1.9);
+
+       // equality and relational
+       f = 42f;
+       assert (f == 42f);
+       assert (f != 50f);
+       assert (f < 42.5f);
+       assert (!(f < 41.5f));
+       assert (f <= 42f);
+       assert (!(f <= 41.5f));
+       assert (f >= 42f);
+       assert (!(f >= 42.5f));
+       assert (f > 41.5f);
+       assert (!(f > 42.5f));
+
+       // to_string
+       string s = f.to_string ();
+       assert (s == "42");
+
+       f = float.parse ("47.11mm");
+       assert (f == 47.11f);
+
+       unowned string unparsed;
+       float.try_parse ("3.45mm", out f, out unparsed);
+       assert (f == 3.45f);
+       assert (unparsed == "mm");
+
+       // ensure that MIN and MAX are valid values
+       f = float.MIN;
+       assert (f == float.MIN);
+       assert (f < float.MAX);
+       f = float.MAX;
+       assert (f == float.MAX);
+       assert (f > float.MIN);
+
+       // nullable
+       float? f2 = 10f;
+       assert (f2 == 10f);
+}
+
 void main () {
        test_double ();
+       test_float ();
 }
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 5a75cfc80..fc322c9a0 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -856,6 +856,25 @@ public struct float {
        public float clamp (float low, float high);
        [CCode (cname = "fabsf")]
        public float abs ();
+
+       [CCode (cname = "strtof", cheader_filename = "stdlib.h")]
+       static float strtof (string nptr, out char* endptr);
+
+       public static float parse (string str) {
+               return strtof (str, null);
+       }
+
+       public static bool try_parse (string str, out float result = null, out unowned string unparsed = 
null) {
+               char* endptr;
+               result = strtof (str, out endptr);
+               if (endptr == (char*) str + str.length) {
+                       unparsed = "";
+                       return true;
+               } else {
+                       unparsed = (string) endptr;
+                       return false;
+               }
+       }
 }
 
 [SimpleType]


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