After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 669278 - No parse(string) function for many basic types
No parse(string) function for many basic types
Status: RESOLVED DUPLICATE of bug 774124
Product: vala
Classification: Core
Component: Basic Types
0.15.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2012-02-02 20:24 UTC by Daniel Espinosa
Modified: 2017-10-18 23:18 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add parse () functions for most SympleType defined in glib-2.0.vapi (5.73 KB, patch)
2012-02-07 18:32 UTC, Daniel Espinosa
needs-work Details | Review

Description Daniel Espinosa 2012-02-02 20:24:16 UTC
No parse(string) function for the following types:

float
double
short
ushort
Comment 1 Luca Bruno 2012-02-02 20:27:47 UTC
double.parse() exists
Comment 2 Daniel Espinosa 2012-02-03 17:28:55 UTC
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.
Comment 3 Luca Bruno 2012-02-04 10:08:49 UTC
(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.
Comment 4 Daniel Espinosa 2012-02-07 17:10:17 UTC
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.
Comment 5 Daniel Espinosa 2012-02-07 17:43:21 UTC
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);
}
Comment 6 Daniel Espinosa 2012-02-07 18:26:29 UTC
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.
Comment 7 Daniel Espinosa 2012-02-07 18:32:23 UTC
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.
Comment 8 Jürg Billeter 2012-08-02 08:40:26 UTC
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.
Comment 9 Al Thomas 2017-10-18 23:18:36 UTC

*** This bug has been marked as a duplicate of bug 774124 ***