GNOME Bugzilla – Bug 669278
No parse(string) function for many basic types
Last modified: 2017-10-18 23:18:36 UTC
No parse(string) function for the following types: float double short ushort
double.parse() exists
At least not in 0.14. I think that if it is in 0.15 must be backported. Could you point me on where to find that definitions? I would like to help on it. I'm very interested in other thinks like cast to string or from string using any object type. I can propose patches for your review.
(In reply to comment #2) > At least not in 0.14. I think that if it is in 0.15 must be backported. It's there since 0.11.x. Maybe the docs were broken, try using it. > Could you point me on where to find that definitions? glib-2.0.vapi > I would like to help on > it. I'm very interested in other thinks like cast to string or from string > using any object type. *any object* is not possible.
To describe in details what I mean about "cast to" concept I filed the Bug #669586. I'll try to create patches to add missing try_parse and parse functions for SimpleTypes in glib-2.0.vapi.
I found that uint64 use g_ascii_strtoull, and documentation say this function is not for user-input, instead recomends to use strtoll(). Is this declaration on glib-2.0.vapi valid? public static uint64 parse (string str) { return ascii_strtoull (str, null, 0); }
For double, glib-2.0.vapi uses g_ascii_strtod () to parse from string, but it doesn't takes locale in account when converting, it is just for configuration but not for user-input, will be a bug if used in other libraries like GDA that gets values in locale from databases or user-input.
Created attachment 207012 [details] [review] Add parse () functions for most SympleType defined in glib-2.0.vapi This patch adds most parse() funtions to SympleType definitions in glib-2.0.vapi. Most of them use others, like uint.parse() uses internally int.parse() and then cast. Due to double and int64 uses some not locale sensitive functions, they where changed to the ones that uses LOCALE.
Review of attachment 207012 [details] [review]: Thanks for the patch. ::: vapi/glib-2.0.vapi @@ +185,3 @@ public static uint from_little_endian (uint val); + + public static uint parse (string str) { return (uint) int.parse (str); } This won't work for large uint values (> INT_MAX). @@ +300,3 @@ public static ulong from_little_endian (ulong val); + + public static ulong parse (string str) { return (ulong) long.parse (str); } This won't work for large ulong values (> LONG_MAX). @@ +331,3 @@ public size_t clamp (size_t low, size_t high); + + public static size_t parse (string str) { return (size_t) ulong.parse (str); } This won't work for large values on 64-bit Windows (where long is just 32-bit). @@ +803,2 @@ public static double parse (string str) { + return strtod (str, null); The use of g_ascii_strtod is intentional. We want the parse/to_string methods to work the same way in all locales. Locale-dependent methods should be added with a different name that clearly marks them as locale-specific.
*** This bug has been marked as a duplicate of bug 774124 ***