[vala/staging] glib-2.0: Add (u)int.try_parse()



commit ae7280960f2d4534f3ffd5668615d1e733002b98
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Oct 21 13:16:36 2019 +0200

    glib-2.0: Add (u)int.try_parse()
    
    See https://gitlab.gnome.org/GNOME/vala/issues/649

 tests/basic-types/integers.vala | 15 ++++++++++++++
 vapi/glib-2.0.vapi              | 44 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
---
diff --git a/tests/basic-types/integers.vala b/tests/basic-types/integers.vala
index 1500ab266..08e3d0d8d 100644
--- a/tests/basic-types/integers.vala
+++ b/tests/basic-types/integers.vala
@@ -91,6 +91,13 @@ void test_int () {
        assert (s == "42");
 
        unowned string unparsed;
+       s = "%im".printf (int.MIN);
+       int.try_parse (s, out i, out unparsed);
+       assert (i == int.MIN);
+       assert (unparsed == "m");
+       s = "%lim".printf (long.MAX);
+       assert (!int.try_parse (s, out i));
+
        s = "%lim".printf (long.MIN);
        long l;
        long.try_parse (s, out l, out unparsed);
@@ -99,6 +106,14 @@ void test_int () {
        s = "%lum".printf (ulong.MAX);
        assert (!long.try_parse (s, out l));
 
+       s = "%um".printf (uint.MAX);
+       uint u;
+       uint.try_parse (s, out u, out unparsed);
+       assert (u == uint.MAX);
+       assert (unparsed == "m");
+       s = "%lum".printf (ulong.MAX);
+       assert (!uint.try_parse (s, out u));
+
        s = "%lum".printf (ulong.MAX);
        ulong ul;
        ulong.try_parse (s, out ul, out unparsed);
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 777d9fb2e..b6ed7a366 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -149,6 +149,26 @@ public struct int {
 
        [CCode (cname = "atoi", cheader_filename = "stdlib.h")]
        public static int parse (string str);
+
+       [CCode (cname = "strtol", cheader_filename = "stdlib.h")]
+       static long strtol (string nptr, out char* endptr, int _base);
+
+       public static bool try_parse (string str, out int result = null, out unowned string unparsed = null, 
uint _base = 0) {
+               char* endptr;
+               long long_result = strtol (str, out endptr, (int) _base);
+               if (endptr == (char*) str + str.length) {
+                       unparsed = "";
+               } else {
+                       unparsed = (string) endptr;
+               }
+               if (int.MIN <= long_result <= int.MAX) {
+                       result = (int) long_result;
+                       return true;
+               } else {
+                       result = int.MAX;
+                       return false;
+               }
+       }
 }
 
 [SimpleType]
@@ -185,6 +205,30 @@ public struct uint {
        public static uint from_big_endian (uint val);
        [CCode (cname = "GUINT_FROM_LE")]
        public static uint from_little_endian (uint val);
+
+       [CCode (cname = "strtoul", cheader_filename = "stdlib.h")]
+       static ulong strtoul (string nptr, out char* endptr, int _base);
+
+       public static uint parse (string str, uint _base = 0) {
+               return (uint) strtoul (str, null, (int) _base);
+       }
+
+       public static bool try_parse (string str, out uint result = null, out unowned string unparsed = null, 
uint _base = 0) {
+               char* endptr;
+               ulong ulong_result = strtoul (str, out endptr, (int) _base);
+               if (endptr == (char*) str + str.length) {
+                       unparsed = "";
+               } else {
+                       unparsed = (string) endptr;
+               }
+               if (uint.MIN <= ulong_result <= uint.MAX) {
+                       result = (uint) ulong_result;
+                       return true;
+               } else {
+                       result = uint.MAX;
+                       return false;
+               }
+       }
 }
 
 [SimpleType]


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