[vala] glib-2.0: Add parse and try_parse methods to boolean and numeric types



commit 1aa1b0de647a6c0eada08e690750144942bdfef6
Author: Jürg Billeter <j bitron ch>
Date:   Sat Jan 29 00:05:09 2011 +0100

    glib-2.0: Add parse and try_parse methods to boolean and numeric types
    
    This deprecates string.to_bool and string.to_int*.

 codegen/valaccodearraymodule.vala |    2 +-
 vala/valaattribute.vala           |    6 +-
 vala/valaelementaccess.vala       |    2 +-
 vala/valagenieparser.vala         |    4 +-
 vala/valagirparser.vala           |    8 ++--
 vala/valaintegerliteral.vala      |    2 +-
 vala/valaintegertype.vala         |    4 +-
 vala/valaparser.vala              |    2 +-
 vapi/glib-2.0.vapi                |   81 +++++++++++++++++++++++++++++++++++++
 vapigen/valagidlparser.vala       |   17 +++-----
 10 files changed, 102 insertions(+), 26 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 06b370a..3bfb3b1 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -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");
diff --git a/vala/valaattribute.vala b/vala/valaattribute.vala
index 9fdc28a..68d7539 100644
--- a/vala/valaattribute.vala
+++ b/vala/valaattribute.vala
@@ -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));
 	}
 }
diff --git a/vala/valaelementaccess.vala b/vala/valaelementaccess.vala
index b7e7def..d8c7699 100644
--- a/vala/valaelementaccess.vala
+++ b/vala/valaelementaccess.vala
@@ -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");
diff --git a/vala/valagenieparser.vala b/vala/valagenieparser.vala
index 5d04204..41c66b6 100644
--- a/vala/valagenieparser.vala
+++ b/vala/valagenieparser.vala
@@ -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);
 
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index d79ce1b..e2ef05d 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -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 ();
diff --git a/vala/valaintegerliteral.vala b/vala/valaintegerliteral.vala
index 1f85a21..e16c457 100644
--- a/vala/valaintegerliteral.vala
+++ b/vala/valaintegerliteral.vala
@@ -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;
diff --git a/vala/valaintegertype.vala b/vala/valaintegertype.vala
index 2c04491..5384545 100644
--- a/vala/valaintegertype.vala
+++ b/vala/valaintegertype.vala
@@ -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;
 			}
 		}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index b062638..136bf01 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -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);
 
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 71846b1..c41e1c5 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -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;
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index fdcfdad..bc45b3e 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -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 ("\"")) {



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