]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Add (u)int.try_parse()
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 21 Oct 2019 11:16:36 +0000 (13:16 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 21 Oct 2019 11:16:36 +0000 (13:16 +0200)
See https://gitlab.gnome.org/GNOME/vala/issues/649

tests/basic-types/integers.vala
vapi/glib-2.0.vapi

index 1500ab26649b8cf84e833a396c50464ad0bfa0c5..08e3d0d8d1275e75c9893c0fc0a1b154a182802f 100644 (file)
@@ -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);
index 777d9fb2e34c73af977a0cc3404461e7ec87709b..b6ed7a3668ec89f825fd28d061e668f52e0f0ded 100644 (file)
@@ -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]