[glib/gsettings] replace sam's "ridiculous" float printing code



commit 1cdbdc7bd604fff33a5acbc4d52de12252bb4db5
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Sep 2 23:26:04 2009 -0400

    replace sam's "ridiculous" float printing code
    
    turns out that g_ascii_dtostr() is exactly what we want here

 glib/gvariant-printer.c |   34 +++++++++++++---------------------
 1 files changed, 13 insertions(+), 21 deletions(-)
---
diff --git a/glib/gvariant-printer.c b/glib/gvariant-printer.c
index 1f2877c..8c149bc 100644
--- a/glib/gvariant-printer.c
+++ b/glib/gvariant-printer.c
@@ -248,32 +248,24 @@ g_variant_print_string (GVariant *value,
 
     case G_VARIANT_TYPE_CLASS_DOUBLE:
       {
+        gchar buffer[100];
         gint i;
-        gchar *repr = NULL;
 
-        /* No need to type-annotate here, because we always use at least 1dp - and
-         * any number with a decimal point is clearly a double.
-         */
+        g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
 
-        /* print float at the minimum precision to maintain its value.
-         * FIXME: this is a ridiculous algorithm.
-         */
-        for (i=1; i<255; i++)
+        for (i = 0; buffer[i]; i++)
+          if (buffer[i] == '.' || buffer[i] == 'e')
+            break;
+
+        /* if there is no '.' or 'e' in the float then add one */
+        if (buffer[i] == '\0')
           {
-            GVariant *variant; gdouble value_repr;
-            GError *error = NULL;
-            g_free (repr);
-            repr = g_strdup_printf ("%.*f", i, g_variant_get_double (value));
-            variant = g_variant_parse (repr, -1, G_VARIANT_TYPE_DOUBLE, &error);
-            g_assert_no_error (error);
-            g_return_val_if_fail (variant != NULL, string);
-            value_repr = g_variant_get_double (variant);
-            g_variant_unref (variant);
-            if (value_repr == g_variant_get_double (value))
-                break;
+            buffer[i++] = '.';
+            buffer[i++] = '0';
+            buffer[i++] = '\0';
           }
-        g_string_append_printf (string, "%s", repr);
-        g_free (repr);
+
+        g_string_append (string, buffer);
         break;
       }
 



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