[gcalctool] Use gunichar for numeric point/thousands separator storage



commit 4353e280fe941b8a5df36e203b94222e4e5b7a43
Author: Robert Ancell <robert ancell canonical com>
Date:   Wed Oct 20 09:35:53 2010 +1100

    Use gunichar for numeric point/thousands separator storage

 src/math-buttons.c  |    6 +++++-
 src/math-equation.c |   10 +++++++---
 src/mp-serializer.c |   37 +++++++++++--------------------------
 src/mp-serializer.h |    5 ++---
 4 files changed, 25 insertions(+), 33 deletions(-)
---
diff --git a/src/math-buttons.c b/src/math-buttons.c
index 329bb83..5fb04fd 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -954,7 +954,11 @@ load_mode(MathButtons *buttons, ButtonMode mode)
     widget = GET_WIDGET(builder, "calc_numeric_point_button");
     if (widget) {
         MpSerializer *serializer = math_equation_get_serializer(buttons->priv->equation);
-        gtk_button_set_label(GTK_BUTTON(widget), mp_serializer_get_numeric_point_text(serializer));
+        gchar buffer[7];
+        gint len;
+        len = g_unichar_to_utf8(mp_serializer_get_numeric_point_text(serializer), buffer);
+        buffer[len] = '\0';
+        gtk_button_set_label(GTK_BUTTON(widget), buffer);
     }
   
     widget = GET_WIDGET(builder, "calc_superscript_button");
diff --git a/src/math-equation.c b/src/math-equation.c
index 5a8a743..3503841 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -796,7 +796,11 @@ math_equation_insert_digit(MathEquation *equation, guint digit)
 void
 math_equation_insert_numeric_point(MathEquation *equation)
 {
-    math_equation_insert(equation, mp_serializer_get_numeric_point_text(equation->priv->serializer));
+    gchar buffer[7];
+    gint len;
+    len = g_unichar_to_utf8( mp_serializer_get_numeric_point_text(equation->priv->serializer), buffer);
+    buffer[len] = '\0';
+    math_equation_insert(equation, buffer);
 }
 
 
@@ -1519,8 +1523,8 @@ pre_insert_text_cb (MathEquation  *equation,
     /* Clear result on next digit entered if cursor at end of line */
     // FIXME Cursor
     c = g_utf8_get_char(text);
-    if ((g_unichar_isdigit(c) || mp_serializer_is_numeric_point(equation->priv->serializer, text)) &&
-            math_equation_is_result(equation)) {
+    if ((g_unichar_isdigit(c) || c == mp_serializer_get_numeric_point_text(equation->priv->serializer)) &&
+         math_equation_is_result(equation)) {
         gtk_text_buffer_set_text(GTK_TEXT_BUFFER(equation), "", -1);
         clear_ans(equation, FALSE);
         gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(equation), location);
diff --git a/src/mp-serializer.c b/src/mp-serializer.c
index d1834f4..cc085b2 100644
--- a/src/mp-serializer.c
+++ b/src/mp-serializer.c
@@ -45,8 +45,8 @@ struct MpSerializerPrivate
 
     gint base;                /* Numeric base */
 
-    char *tsep;         /* Locale specific thousands separator. */
-    char *radix;        /* Locale specific radix string. */
+    gunichar tsep;            /* Locale specific thousands separator. */
+    gunichar radix;           /* Locale specific radix string. */
     gint tsep_count;          /* Number of digits between separator. */
 };
 
@@ -104,7 +104,7 @@ mp_cast_to_string_real(MpSerializer *serializer, const MPNumber *x, int base, bo
 
         i++;
         if (serializer->priv->show_tsep && i == serializer->priv->tsep_count) {
-            g_string_prepend(string, serializer->priv->tsep);
+            g_string_prepend_unichar(string, serializer->priv->tsep);
             i = 0;
         }
 
@@ -113,7 +113,7 @@ mp_cast_to_string_real(MpSerializer *serializer, const MPNumber *x, int base, bo
 
     last_non_zero = string->len;
 
-    g_string_append(string, serializer->priv->radix);
+    g_string_append_unichar(string, serializer->priv->radix);
 
     /* Write out the fractional component */
     mp_set_from_mp(&fractional_component, &temp);
@@ -301,8 +301,7 @@ mp_serializer_to_specific_string(const MPNumber *x, int base, int accuracy, bool
 {
     MpSerializer *serializer = mp_serializer_new ();
     if (!localize) {
-        g_free(serializer->priv->radix);
-        serializer->priv->radix = g_strdup(".");
+        serializer->priv->radix = '.';
         serializer->priv->show_tsep = false;
     }
     serializer->priv->base = base;
@@ -330,26 +329,20 @@ mp_serializer_get_base(MpSerializer *serializer)
     return serializer->priv->base;
 }
 
-const gchar *
+
+gunichar
 mp_serializer_get_numeric_point_text(MpSerializer *serializer)
 {
     return serializer->priv->radix;
 }
 
 
-const gchar *
+gunichar
 mp_serializer_get_thousands_separator_text(MpSerializer *serializer)
 {
     return serializer->priv->tsep;
 }
 
-gboolean
-mp_serializer_is_numeric_point(MpSerializer *serializer, const char *text)
-{
-    if (*text == '.' || strncmp(text, serializer->priv->radix, strlen(serializer->priv->radix)) == 0)
-        return true;
-    return false;
-}
 
 void
 mp_serializer_set_show_thousands_separators(MpSerializer *serializer, gboolean visible)
@@ -451,13 +444,6 @@ mp_serializer_get_property(GObject *object,
     }
 }
 
-static void
-mp_serializer_finalize (GObject *gobject)
-{
-    MpSerializer *serializer = MP_SERIALIZER(gobject);
-    g_free(serializer->priv->radix);
-    g_free(serializer->priv->tsep);
-}
 
 static void
 mp_serializer_class_init (MpSerializerClass *klass)
@@ -465,7 +451,6 @@ mp_serializer_class_init (MpSerializerClass *klass)
     GObjectClass *object_class = G_OBJECT_CLASS(klass);
     object_class->get_property = mp_serializer_get_property;
     object_class->set_property = mp_serializer_set_property;
-    object_class->finalize = mp_serializer_finalize;
 
     g_type_class_add_private (klass, sizeof(MpSerializerPrivate));
 
@@ -509,11 +494,11 @@ mp_serializer_init(MpSerializer *serializer)
     serializer->priv = G_TYPE_INSTANCE_GET_PRIVATE(serializer, mp_serializer_get_type(), MpSerializerPrivate);
 
     radix = nl_langinfo(RADIXCHAR);
-    serializer->priv->radix = *radix ? g_locale_to_utf8(radix, -1, NULL, NULL, NULL) : g_strdup(".");
+    serializer->priv->radix = radix ? g_utf8_get_char(g_locale_to_utf8(radix, -1, NULL, NULL, NULL)) : '.';
     tsep = nl_langinfo(THOUSEP);
-    serializer->priv->tsep = *tsep ? g_locale_to_utf8(tsep, -1, NULL, NULL, NULL) : g_strdup(" ");
-
+    serializer->priv->tsep = tsep ? g_utf8_get_char(g_locale_to_utf8(tsep, -1, NULL, NULL, NULL)) : ',';
     serializer->priv->tsep_count = 3;
+
     serializer->priv->base = 10;
     serializer->priv->accuracy = 9;
     serializer->priv->show_zeroes = FALSE;
diff --git a/src/mp-serializer.h b/src/mp-serializer.h
index 718ee1e..cef5c26 100644
--- a/src/mp-serializer.h
+++ b/src/mp-serializer.h
@@ -54,9 +54,8 @@ void mp_serializer_to_standard_string(MpSerializer *serializer, const MPNumber *
 void mp_serializer_to_specific_string(const MPNumber *z, int base, int accuracy, bool trim_zeroes, bool localize, char **target);
 bool mp_serializer_from_string(MpSerializer *serializer, const char *str, MPNumber *z);
 
-const gchar* mp_serializer_get_thousands_separator_text(MpSerializer *serializer);
-const gchar* mp_serializer_get_numeric_point_text(MpSerializer *serializer);
-gboolean mp_serializer_is_numeric_point(MpSerializer *serializer, const char* text);
+gunichar mp_serializer_get_thousands_separator_text(MpSerializer *serializer);
+gunichar mp_serializer_get_numeric_point_text(MpSerializer *serializer);
 void mp_serializer_set_base(MpSerializer *serializer, int base);
 int mp_serializer_get_base(MpSerializer *serializer);
 gboolean mp_serializer_get_show_trailing_zeroes(MpSerializer *serializer);



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