try_parse
Description:
Converts a string to a uint64 value.
This function behaves like the standard strtoull
function does in the C locale. It does this without actually changing the current
locale, since that would not be thread-safe.
This function is typically used when reading configuration files or other non-user input that should be locale independent. To handle input
from the user you should normally use the locale-sensitive system strtoull
function.
If the correct value would cause overflow, int64.MAX is returned, and ERANGE is stored in GLib.errno. If the base is outside the valid range, zero is returned, and EINVAL is stored in errno. If the string conversion fails, zero is returned.
Example: String to uint64, with error detection:
public static int main (string[] args) {
uint64 val;
bool res;
// Output: ``true => 34``
res = uint64.try_parse ("34", out val);
print ("%s => %" + uint64.FORMAT + "\n", res.to_string (), val);
// Output: ``false => 0``
res = uint64.try_parse ("d34", out val);
print ("%s => %" + uint64.FORMAT + "\n", res.to_string (), val);
// Output: ``false => 34``
res = uint64.try_parse ("34d", out val);
print ("%s => %" + uint64.FORMAT + "\n", res.to_string (), val);
return 0;
}
valac --pkg glib-2.0 uint64.try_parse.vala
Parameters:
str |
the string to convert to a numeric value. |
result |
the location to store the value |
Returns:
true on success, or false. |