]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Add parse and try_parse methods to boolean and numeric types
authorJürg Billeter <j@bitron.ch>
Fri, 28 Jan 2011 23:05:09 +0000 (00:05 +0100)
committerJürg Billeter <j@bitron.ch>
Fri, 28 Jan 2011 23:09:02 +0000 (00:09 +0100)
This deprecates string.to_bool and string.to_int*.

codegen/valaccodearraymodule.vala
vala/valaattribute.vala
vala/valaelementaccess.vala
vala/valagenieparser.vala
vala/valagirparser.vala
vala/valaintegerliteral.vala
vala/valaintegertype.vala
vala/valaparser.vala
vapi/glib-2.0.vapi
vapigen/valagidlparser.vala

index 06b370ab58e7e0500d97028614360b951e16e457..3bfb3b1fa0fbbf70d6d6f88896fb76183688035f 100644 (file)
@@ -178,7 +178,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
                        var lit = indices[0] as IntegerLiteral;
                        var memberaccess = expr.container as MemberAccess;
                        if (lit != null && memberaccess != null) {
-                               int dim = lit.value.to_int ();
+                               int dim = int.parse (lit.value);
                                set_cvalue (expr, get_array_length_cexpression (memberaccess.inner, dim + 1));
                        } else {
                                Report.error (expr.source_reference, "only integer literals supported as index");
index 9fdc28a428ab86570e77c616689f1b6aacab1a22..68d753910f800959a0ff08c294b6fa9025f9c055 100644 (file)
@@ -99,7 +99,7 @@ public class Vala.Attribute : CodeNode {
                        return 0;
                }
 
-               return value.to_int ();
+               return int.parse (value);
        }
 
        /**
@@ -115,7 +115,7 @@ public class Vala.Attribute : CodeNode {
                        return 0;
                }
 
-               return value.to_double ();
+               return double.parse (value);
        }
 
        /**
@@ -125,6 +125,6 @@ public class Vala.Attribute : CodeNode {
         * @return     boolean value
         */
        public bool get_bool (string name) {
-               return (args.get (name) == "true");
+               return bool.parse (args.get (name));
        }
 }
index b7e7deff70f73f8f2825cc93bdcc4a4d0d97452d..d8c769997f6f64e7d81925428a38a6c3bb95517a 100644 (file)
@@ -156,7 +156,7 @@ public class Vala.ElementAccess : Expression {
                                Report.error (source_reference, "Element access with non-literal index is not supported for tuples");
                                return false;
                        }
-                       int i = index.value.to_int ();
+                       int i = int.parse (index.value);
                        if (container.value_type.get_type_arguments ().size == 0) {
                                error = true;
                                Report.error (source_reference, "Element access is not supported for untyped tuples");
index 5d042043b16aa5801992a20313c4e840e80f5631..41c66b647523dab4628aff13e1c3874eaff48966 100644 (file)
@@ -392,7 +392,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                                if (id == "indent") {
                                        expect (TokenType.ASSIGN);
                                        expect (TokenType.INTEGER_LITERAL);
-                                       scanner.indent_spaces = get_last_string().to_int();
+                                       scanner.indent_spaces = int.parse (get_last_string());
                                        expect (TokenType.CLOSE_BRACKET);
                                        expect (TokenType.EOL);
                                } else {
@@ -599,7 +599,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                                }
 
                                var length_literal = (IntegerLiteral) parse_literal ();
-                               array_length = length_literal.value.to_int ();
+                               array_length = int.parse (length_literal.value);
                        }
                        expect (TokenType.CLOSE_BRACKET);
 
index d79ce1bbdf973c02444ed04b4d5ec519f021ece2..e2ef05dc2ecc9c167e22cef9b5c3ab03c756464d 100644 (file)
@@ -202,7 +202,7 @@ public class Vala.GirParser : CodeVisitor {
                public int get_integer (ArgumentType arg) {
                        var lit = get_expression (arg) as IntegerLiteral;
                        if (lit != null) {
-                               return lit.value.to_int ();
+                               return int.parse (lit.value);
                        }
 
                        return 0;
@@ -1630,10 +1630,10 @@ public class Vala.GirParser : CodeVisitor {
                string closure = reader.get_attribute ("closure");
                string destroy = reader.get_attribute ("destroy");
                if (closure != null && &closure_idx != null) {
-                       closure_idx = closure.to_int ();
+                       closure_idx = int.parse (closure);
                }
                if (destroy != null && &destroy_idx != null) {
-                       destroy_idx = destroy.to_int ();
+                       destroy_idx = int.parse (destroy);
                }
 
                next ();
@@ -1690,7 +1690,7 @@ public class Vala.GirParser : CodeVisitor {
                        if (!(type_name == "GLib.Array" || type_name == "GLib.PtrArray")) {
                                if (reader.get_attribute ("length") != null
                                    && &array_length_index != null) {
-                                       array_length_index = reader.get_attribute ("length").to_int ();
+                                       array_length_index = int.parse (reader.get_attribute ("length"));
                                }
                                next ();
                                var element_type = parse_type ();
index 1f85a21c5e6f954641483ef4ecf9e5dfb00326dd..e16c4577ec9b9654e7c0dceea194ca07643ec99e 100644 (file)
@@ -78,7 +78,7 @@ public class Vala.IntegerLiteral : Literal {
                        value = value.substring (0, value.length - 1);
                }
                
-               int64 n = value.to_int64 ();
+               int64 n = int64.parse (value);
                if (!u && n > 0x7fffffff) {
                        // value doesn't fit into signed 32-bit
                        l = 2;
index 2c04491b071ff31bf02803305a96c19b2430c647..53845455a478b280e99f7546cbf2872672ed7ab2 100644 (file)
@@ -52,7 +52,7 @@ public class Vala.IntegerType : ValueType {
                        if (target_st.is_integer_type ()) {
                                var int_attr = target_st.get_attribute ("IntegerType");
                                if (int_attr != null && int_attr.has_argument ("min") && int_attr.has_argument ("max")) {
-                                       int val = literal_value.to_int ();
+                                       int val = int.parse (literal_value);
                                        return (val >= int_attr.get_integer ("min") && val <= int_attr.get_integer ("max"));
                                } else {
                                        // assume to be compatible if the target type doesn't specify limits
@@ -61,7 +61,7 @@ public class Vala.IntegerType : ValueType {
                        }
                } else if (target_type.data_type is Enum && literal_type_name == "int") {
                        // allow implicit conversion from 0 to enum and flags types
-                       if (literal_value.to_int () == 0) {
+                       if (int.parse (literal_value) == 0) {
                                return true;
                        }
                }
index b0626384cc8a431c38a2dd382f072dde32772a09..136bf01696c3779e905a19ee396de7eff3376a29 100644 (file)
@@ -511,7 +511,7 @@ public class Vala.Parser : CodeVisitor {
                                }
 
                                var length_literal = (IntegerLiteral) parse_literal ();
-                               array_length = length_literal.value.to_int ();
+                               array_length = int.parse (length_literal.value);
                        }
                        expect (TokenType.CLOSE_BRACKET);
 
index 71846b1d9bfb88112eeb8115e294e9edf4ce4181..c41e1c599990fd4325addac702dd5088893efeba 100644 (file)
@@ -40,6 +40,26 @@ public struct bool {
                        return "false";
                }
        }
+
+       public static bool parse (string str) {
+               if (str == "true") {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+       public static bool try_parse (string str, out bool result = null) {
+               if (str == "true") {
+                       result = true;
+                       return true;
+               } else if (str == "false") {
+                       result = false;
+                       return true;
+               } else {
+                       result = false;
+                       return false;
+               }
+       }
 }
 
 [SimpleType]
@@ -124,6 +144,9 @@ public struct int {
        public static int from_big_endian (int val);
        [CCode (cname = "GINT_FROM_LE")]
        public static int from_little_endian (int val);
+
+       [CCode (cname = "atoi", cheader_filename = "stdlib.h")]
+       public static int parse (string str);
 }
 
 [SimpleType]
@@ -233,6 +256,9 @@ public struct long {
        public static long from_big_endian (long val);
        [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);
 }
 
 [SimpleType]
@@ -560,6 +586,22 @@ public struct int64 {
 
        [CCode (cname = "GUINT64_SWAP_LE_BE")]
        public uint64 swap_little_endian_big_endian ();
+
+       [CCode (cname = "g_ascii_strtoll")]
+       static int64 ascii_strtoll (string nptr, out char* endptr, uint _base);
+
+       public static int64 parse (string str) {
+               return ascii_strtoll (str, null, 0);
+       }
+       public static bool try_parse (string str, out int64 result = null) {
+               char* endptr;
+               result = ascii_strtoll (str, out endptr, 0);
+               if (endptr == (char*) str + str.length) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
 }
 
 [SimpleType]
@@ -596,6 +638,22 @@ public struct uint64 {
        public static uint64 from_big_endian (uint64 val);
        [CCode (cname = "GUINT64_FROM_LE")]
        public static uint64 from_little_endian (uint64 val);
+
+       [CCode (cname = "g_ascii_strtoull")]
+       static uint64 ascii_strtoull (string nptr, out char* endptr, uint _base);
+
+       public static uint64 parse (string str) {
+               return ascii_strtoull (str, null, 0);
+       }
+       public static bool try_parse (string str, out uint64 result = null) {
+               char* endptr;
+               result = ascii_strtoull (str, out endptr, 0);
+               if (endptr == (char*) str + str.length) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
 }
 
 [SimpleType]
@@ -708,6 +766,22 @@ public struct double {
        public string to_string () {
                return this.to_str(new char[DTOSTR_BUF_SIZE]);
        }
+
+       [CCode (cname = "g_ascii_strtod")]
+       static double ascii_strtod (string nptr, out char* endptr);
+
+       public static double parse (string str) {
+               return ascii_strtod (str, null);
+       }
+       public static bool try_parse (string str, out double result = null) {
+               char* endptr;
+               result = ascii_strtod (str, out endptr);
+               if (endptr == (char*) str + str.length) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
 }
 
 [GIR (name = "glong")]
@@ -1153,19 +1227,26 @@ public class string {
        [CCode (cname = "g_str_hash")]
        public uint hash ();
        
+       [Deprecated (replacement = "int.parse")]
        [CCode (cname = "atoi")]
        public int to_int ();
+       [Deprecated (replacement = "long.parse")]
        [CCode (cname = "strtol")]
        public long to_long (out unowned string endptr = null, int _base = 0);
+       [Deprecated (replacement = "double.parse")]
        [CCode (cname = "g_ascii_strtod")]
        public double to_double (out unowned string endptr = null);
+       [Deprecated (replacement = "uint64.parse")]
        [CCode (cname = "strtoul")]
        public ulong to_ulong (out unowned string endptr = null, int _base = 0);
+       [Deprecated (replacement = "int64.parse")]
        [CCode (cname = "g_ascii_strtoll")]
        public int64 to_int64 (out unowned string endptr = null, int _base = 0);
+       [Deprecated (replacement = "uint64.parse")]
        [CCode (cname = "g_ascii_strtoull")]
        public uint64 to_uint64 (out unowned string endptr = null, int _base = 0);
 
+       [Deprecated (replacement = "bool.parse")]
        public bool to_bool () {
                if (this == "true") {
                        return true;
index fdcfdade013281a5b7e73cb6491df724890729e4..bc45b3eef3176e12a8174538efdda9fa4b3aa04f 100644 (file)
@@ -484,7 +484,7 @@ public class Vala.GIdlParser : CodeVisitor {
                                } else if (nv[0] == "type_arguments") {
                                        parse_type_arguments_from_string (return_type, eval (nv[1]));
                                } else if (nv[0] == "instance_pos") {
-                                       cb.cinstance_parameter_position = eval (nv[1]).to_double ();
+                                       cb.cinstance_parameter_position = double.parse (eval (nv[1]));
                                } else if (nv[0] == "type_parameters") {
                                        foreach (string type_param_name in eval (nv[1]).split (",")) {
                                                cb.add_type_parameter (new TypeParameter (type_param_name, current_source_reference));
@@ -629,7 +629,7 @@ public class Vala.GIdlParser : CodeVisitor {
                                                } else if (nv[0] == "base_type") {
                                                        st.base_type = parse_type_string (eval (nv[1]));
                                                } else if (nv[0] == "rank") {
-                                                       st.set_rank (eval (nv[1]).to_int ());
+                                                       st.set_rank (int.parse (eval (nv[1])));
                                                } else if (nv[0] == "simple_type") {
                                                        if (eval (nv[1]) == "1") {
                                                                st.set_simple_type (true);
@@ -2198,10 +2198,10 @@ public class Vala.GIdlParser : CodeVisitor {
                                                }
                                        } else if (nv[0] == "array_length_pos") {
                                                set_array_length_pos = true;
-                                               array_length_pos = eval (nv[1]).to_double ();
+                                               array_length_pos = double.parse (eval (nv[1]));
                                        } else if (nv[0] == "delegate_target_pos") {
                                                set_delegate_target_pos = true;
-                                               delegate_target_pos = eval (nv[1]).to_double ();
+                                               delegate_target_pos = double.parse (eval (nv[1]));
                                        } else if (nv[0] == "type_name") {
                                                p.variable_type = param_type = parse_type_from_string (eval (nv[1]), false);
                                        } else if (nv[0] == "ctype") {
@@ -2219,15 +2219,10 @@ public class Vala.GIdlParser : CodeVisitor {
                                                } else if (val == "") {
                                                        p.initializer = new StringLiteral ("\"\"", param_type.source_reference);
                                                } else {
-                                                       unowned string endptr;
-                                                       char* val_end = (char*) val + val.length;
-
-                                                       val.to_long (out endptr);
-                                                       if ((long)endptr == (long)val_end) {
+                                                       if (int64.try_parse (val)) {
                                                                p.initializer = new IntegerLiteral (val, param_type.source_reference);
                                                        } else {
-                                                               val.to_double (out endptr);
-                                                               if ((long)endptr == (long)val_end) {
+                                                               if (double.try_parse (val)) {
                                                                        p.initializer = new RealLiteral (val, param_type.source_reference);
                                                                } else {
                                                                        if (val.has_prefix ("\"") && val.has_suffix ("\"")) {