]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Add (u)long.parse/try_parse()
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 10 Mar 2017 13:57:27 +0000 (14:57 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 10 Aug 2018 12:46:25 +0000 (14:46 +0200)
https://gitlab.gnome.org/GNOME/vala/issues/649

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

index 4e91071b0ee08407c23179d5eb5d9464fc084057..4b097ae2e8bae3f5ede094d59314c468e363515e 100644 (file)
@@ -71,12 +71,25 @@ void test_int () {
        assert (s == "42");
 
        unowned string unparsed;
+       long l;
+       long.try_parse ("%lim".printf (long.MIN), out l, out unparsed);
+       assert (l == long.MIN);
+       assert (unparsed == "m");
+       assert (!long.try_parse ("%lum".printf (ulong.MAX), out l));
+
+       ulong ul;
+       ulong.try_parse ("%lum".printf (ulong.MAX), out ul, out unparsed);
+       assert (ul == ulong.MAX);
+       assert (unparsed == "m");
+       assert (!ulong.try_parse ("%lim".printf (long.MIN), out ul));
+
        int64 i64;
        int64.try_parse ("-4711inch", out i64, out unparsed);
        assert (i64 == -4711);
        assert (unparsed == "inch");
        int64.try_parse ("-31415km", out i64);
        assert (i64 == -31415);
+
        uint64 ui64;
        uint64.try_parse ("4711yards", out ui64, out unparsed);
        assert (ui64 == 4711);
index 147a7148dc556a5056cbecbf06acbab1c1971c02..d3e38c544a6caf3c0c925689bd4378e02aa6f75c 100644 (file)
@@ -263,8 +263,24 @@ public struct long {
        [CCode (cname = "GLONG_FROM_LE")]
        public static long from_little_endian (long val);
 
-       [CCode (cname = "atol", cheader_filename = "stdlib.h")]
-       public static long parse (string str);
+       [CCode (cname = "strtol", cheader_filename = "stdlib.h")]
+       static long strtol (string nptr, out char* endptr, uint _base);
+
+       public static long parse (string str) {
+               return strtol (str, null, 0);
+       }
+
+       public static bool try_parse (string str, out long result = null, out unowned string unparsed = null) {
+               char* endptr;
+               result = strtol (str, out endptr, 0);
+               if (endptr == (char*) str + str.length) {
+                       unparsed = "";
+                       return true;
+               } else {
+                       unparsed = (string) endptr;
+                       return false;
+               }
+       }
 }
 
 [SimpleType]
@@ -296,6 +312,25 @@ public struct ulong {
        public static ulong from_big_endian (ulong val);
        [CCode (cname = "GULONG_FROM_LE")]
        public static ulong from_little_endian (ulong val);
+
+       [CCode (cname = "strtoul", cheader_filename = "stdlib.h")]
+       static ulong strtoul (string nptr, out char* endptr, uint _base);
+
+       public static ulong parse (string str) {
+               return strtoul (str, null, 0);
+       }
+
+       public static bool try_parse (string str, out ulong result = null, out unowned string unparsed = null) {
+               char* endptr;
+               result = strtoul (str, out endptr, 0);
+               if (endptr == (char*) str + str.length) {
+                       unparsed = "";
+                       return true;
+               } else {
+                       unparsed = (string) endptr;
+                       return false;
+               }
+       }
 }
 
 [SimpleType]
@@ -747,6 +782,7 @@ public struct uint64 {
        public static uint64 parse (string str) {
                return ascii_strtoull (str, null, 0);
        }
+
        public static bool try_parse (string str, out uint64 result = null, out unowned string unparsed = null) {
                char* endptr;
                result = ascii_strtoull (str, out endptr, 0);
@@ -889,6 +925,7 @@ public struct double {
        public static double parse (string str) {
                return ascii_strtod (str, null);
        }
+
        public static bool try_parse (string str, out double result = null, out unowned string unparsed = null) {
                char* endptr;
                result = ascii_strtod (str, out endptr);
@@ -1280,7 +1317,7 @@ public class string {
        [Version (replacement = "double.parse")]
        [CCode (cname = "g_ascii_strtod")]
        public double to_double (out unowned string endptr = null);
-       [Version (replacement = "uint64.parse")]
+       [Version (replacement = "ulong.parse")]
        [CCode (cname = "strtoul")]
        public ulong to_ulong (out unowned string endptr = null, int _base = 0);
        [Version (replacement = "int64.parse")]