[vala/staging] glib-2.0: Add optional 'unparsed' parameter to *.try_parse ()



commit 1eb5044ff3be0546fa6fee64ae250323204ded5c
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Fri Mar 10 08:05:48 2017 +0100

    glib-2.0: Add optional 'unparsed' parameter to *.try_parse ()
    
    This reintroduces a dropped feature of string.to_*() while returning the
    maybe available unparsed part of the string.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=774124

 tests/basic-types/floats.vala   |    5 +++++
 tests/basic-types/integers.vala |   14 ++++++++++++++
 vapi/glib-2.0.vapi              |   12 +++++++++---
 3 files changed, 28 insertions(+), 3 deletions(-)
---
diff --git a/tests/basic-types/floats.vala b/tests/basic-types/floats.vala
index 3f6eaa1..e0e8aef 100644
--- a/tests/basic-types/floats.vala
+++ b/tests/basic-types/floats.vala
@@ -45,6 +45,11 @@ void test_double () {
        string s = d.to_string ();
        assert (s == "42");
 
+       unowned string unparsed;
+       double.try_parse ("3.45mm", out d, out unparsed);
+       assert (d == 3.45);
+       assert (unparsed == "mm");
+
        // ensure that MIN and MAX are valid values
        d = double.MIN;
        assert (d == double.MIN);
diff --git a/tests/basic-types/integers.vala b/tests/basic-types/integers.vala
index 40eebfa..4e91071 100644
--- a/tests/basic-types/integers.vala
+++ b/tests/basic-types/integers.vala
@@ -70,6 +70,20 @@ void test_int () {
        string s = i.to_string ();
        assert (s == "42");
 
+       unowned string unparsed;
+       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);
+       assert (unparsed == "yards");
+       uint64.try_parse ("31415yards", out ui64);
+       assert (ui64 == 31415);
+
        // ensure that MIN and MAX are valid values
        i = int.MIN;
        assert (i == int.MIN);
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index e3ba412..8414293 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -689,12 +689,14 @@ public struct int64 {
        }
 
        [Version (since = "2.12")]
-       public static bool try_parse (string str, out int64 result = null) {
+       public static bool try_parse (string str, out int64 result = null, out unowned string unparsed = 
null) {
                char* endptr;
                result = ascii_strtoll (str, out endptr, 0);
                if (endptr == (char*) str + str.length) {
+                       unparsed = "";
                        return true;
                } else {
+                       unparsed = (string) endptr;
                        return false;
                }
        }
@@ -745,12 +747,14 @@ 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) {
+       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);
                if (endptr == (char*) str + str.length) {
+                       unparsed = "";
                        return true;
                } else {
+                       unparsed = (string) endptr;
                        return false;
                }
        }
@@ -885,12 +889,14 @@ 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) {
+       public static bool try_parse (string str, out double result = null, out unowned string unparsed = 
null) {
                char* endptr;
                result = ascii_strtod (str, out endptr);
                if (endptr == (char*) str + str.length) {
+                       unparsed = "";
                        return true;
                } else {
+                       unparsed = (string) endptr;
                        return false;
                }
        }


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