Skip to content

Commit

Permalink
Use both GMP and MPFR libraries to correctly parse numeric parts
Browse files Browse the repository at this point in the history
I know this looks stupid, but MPFR doesn't handle things like 1٫5 (valid number
in the ps_AF.UTF-8 locale using a unicode radix character) and on the other
hand, GMP thinks 0.1*1000 = 99. So until MPFR gets things right (because in case
of GMP it's not a bug but a feature) we need to use both of them to parse a
single numeric string or do our own complex magic.
  • Loading branch information
vpodzime committed Oct 19, 2015
1 parent db1439a commit f87fb9f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/bs_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ BSSize* bs_size_new_from_str (const gchar *size_str, GError **error) {
gchar *num_str = NULL;
const gchar *radix_char = NULL;
gchar *loc_size_str = NULL;
mpf_t parsed_size;
mpfr_t size;
gint status = 0;
gchar *unit_str = NULL;
Expand Down Expand Up @@ -292,16 +293,22 @@ BSSize* bs_size_new_from_str (const gchar *size_str, GError **error) {
return NULL;
}

mpfr_init2 (size, BS_FLOAT_PREC_BITS);
status = mpfr_set_str (size, *num_str == '+' ? num_str+1 : num_str, 10, MPFR_RNDN);
/* parse the number using GMP because it knows how to handle localization
much better than MPFR */
mpf_init2 (parsed_size, BS_FLOAT_PREC_BITS);
status = mpf_set_str (parsed_size, *num_str == '+' ? num_str+1 : num_str, 10);
if (status != 0) {
g_set_error (error, BS_SIZE_ERROR, BS_SIZE_ERROR_INVALID_SPEC,
"Failed to parse size spec: %s", size_str);
g_match_info_free (match_info);
g_free (loc_size_str);
mpfr_clear (size);
mpf_clear (parsed_size);
return NULL;
}
/* but use MPFR from now on because GMP thinks 0.1*1000 = 99 */
mpfr_init2 (size, BS_FLOAT_PREC_BITS);
mpfr_set_f (size, parsed_size, MPFR_RNDN);
mpf_clear (parsed_size);

unit_str = g_match_info_fetch_named (match_info, "rest");
if (unit_str && g_strcmp0 (unit_str, "") != 0) {
Expand Down

0 comments on commit f87fb9f

Please sign in to comment.