[gcalctool] Last part of thousands separator code



commit df984fc20501db04fc94fba1f30f48fd19d95da3
Author: Robert Ancell <robert ancell canonical com>
Date:   Wed Oct 20 18:22:32 2010 +1100

    Last part of thousands separator code

 src/math-equation.c     |   49 +++++++++++++++++++++++++++++++++++++++-------
 src/mp-equation-lexer.l |   15 +++----------
 2 files changed, 45 insertions(+), 19 deletions(-)
---
diff --git a/src/math-equation.c b/src/math-equation.c
index 8bb71fc..c9ae034 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -731,19 +731,52 @@ math_equation_get_display(MathEquation *equation)
 gchar *
 math_equation_get_equation(MathEquation *equation)
 {
-    char *text, *t;
-    gint ans_start, ans_end;
+    gchar *text;
+    GString *eq_text;
+    gint ans_start = -1, ans_end = -1, offset;
+    const gchar *read_iter;
+    gboolean last_is_digit = FALSE;
 
     text = math_equation_get_display(equation);
+    eq_text = g_string_sized_new(strlen(text));
 
-    /* No ans to substitute */
-    if(!equation->priv->ans_start)
-        return text;
+    if (equation->priv->ans_start)
+        get_ans_offsets(equation, &ans_start, &ans_end);
 
-    get_ans_offsets(equation, &ans_start, &ans_end);
-    t = g_strdup_printf("%.*sans%s", (int)(g_utf8_offset_to_pointer(text, ans_start) - text), text, g_utf8_offset_to_pointer(text, ans_end));
+    for (read_iter = text, offset = 0; *read_iter != '\0'; read_iter = g_utf8_next_char(read_iter), offset++) {
+        gunichar c;
+        gboolean is_digit, next_is_digit;
+
+        c = g_utf8_get_char(read_iter);
+        is_digit = g_unichar_isdigit(c);
+        next_is_digit = g_unichar_isdigit(g_utf8_get_char(g_utf8_next_char(read_iter)));
+
+        /* Replace ans text with variable */
+        if (offset == ans_start) { 
+             g_string_append(eq_text, "ans");
+             read_iter = g_utf8_offset_to_pointer(read_iter, ans_end - ans_start - 1);
+             offset += ans_end - ans_start - 1;
+             is_digit = FALSE;
+             continue;
+        }
+
+        /* Ignore thousands separators */
+        if (c == mp_serializer_get_thousands_separator_text(equation->priv->serializer) && last_is_digit && next_is_digit)
+            ;
+        /* Substitute radix character */
+        else if (c == mp_serializer_get_numeric_point_text(equation->priv->serializer) && (last_is_digit || next_is_digit))
+            g_string_append_unichar(eq_text, '.');
+        else
+            g_string_append_unichar(eq_text, c);
+
+        last_is_digit = is_digit;
+    }
     g_free(text);
-    return t;
+
+    text = eq_text->str;
+    g_string_free(eq_text, FALSE);
+
+    return text;
 }
 
 
diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l
index e77e1e2..a7a7e84 100644
--- a/src/mp-equation-lexer.l
+++ b/src/mp-equation-lexer.l
@@ -45,16 +45,9 @@ SIX           "6"|"ã?¦"|"Ù¦"|"Û¶"|"ß?"|"६"|"৬"|"੬"|"૬"|"à­¬"|"௬"|"౬"
 SEVEN         "7"|"ã?§"|"Ù§"|"Û·"|"ß?"|"७"|"৭"|"à©­"|"à«­"|"à­­"|"௭"|"à±­"|"à³­"|"൭"|"à¹?"|"à»?"|"༧"|"á??"|"á??"|"á?§"|"á ?"|"á¥?"|"á§?"|"á­?"|"á®·"|"á±?"|"á±?"|"ê?§"|"ê£?"|"ê¤?"|"ê©?"|"ð??§"
 EIGHT         "8"|"ã?¨"|"Ù¨"|"Û¸"|"ß?"|"८"|"৮"|"à©®"|"à«®"|"à­®"|"௮"|"à±®"|"à³®"|"൮"|"à¹?"|"à»?"|"༨"|"á??"|"á??"|"á?¨"|"á ?"|"á¥?"|"á§?"|"á­?"|"᮸"|"á±?"|"á±?"|"ê?¨"|"ê£?"|"ê¤?"|"ê©?"|"ð??¨"
 NINE          "9"|"ã?©"|"Ù©"|"Û¹"|"ß?"|"९"|"৯"|"੯"|"૯"|"à­¯"|"௯"|"౯"|"೯"|"൯"|"à¹?"|"à»?"|"༩"|"á??"|"á??"|"á?©"|"á ?"|"á¥?"|"á§?"|"á­?"|"᮹"|"á±?"|"á±?"|"ê?©"|"ê£?"|"ê¤?"|"ê©?"|"ð??©"
-/* FIXME: DECIMAL and TSEP needs to figure out, at runtime, the correct
- * character. I can't figure out how to make flex do this.
- */
-DECIMAL       [,.]
-TSEP          [ ,]
-/* FIXME: TSEP is not a number, but if I try to handle it properly, the parser
- * gets too many NFA states.
- */
-DEC           {ZERO}|{ONE}|{TWO}|{THREE}|{FOUR}|{FIVE}|{SIX}|{SEVEN}|{EIGHT}|{NINE}|{TSEP}
-HEX           {DEC}|[A-Fa-f]
+DECIMAL	      "."|","
+DEC           {ZERO}|{ONE}|{TWO}|{THREE}|{FOUR}|{FIVE}|{SIX}|{SEVEN}|{EIGHT}|{NINE}
+HEX           {DEC}|[A-F]a-f]
 SUPER_DIGITS  "�"|"¹"|"²"|"³"|"�"|"�"|"�"|"�"|"�"|"�"
 SUPER_MINUS   "â?»"
 SUB_DIGITS    "â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"
@@ -75,7 +68,7 @@ BASE_NUM {HEX}+{SUB_NUM}|{HEX}*{DECIMAL}{HEX}+{SUB_NUM}
 ANGLE_NUM {DEC_NUM}{DEGREES}|{DEC}+{DEGREES}{DEC_NUM}{MINUTES}|{DEC}+{DEGREES}{DEC}+{MINUTES}{DEC_NUM}{SECONDS}
 
 NUMBER   {DEF_NUM}|{BASE_NUM}|{FRACTION}|{DEC}+{FRACTION}|{ANGLE_NUM}
-VARIABLE {WORD}|{WORD}{SUB_NUM}
+VARIABLE {WORD}|{WORD}{SUB_NUM}|{GREEKS}
 
 MOD  [mM][oO][dD]
 AND  "â?§"|[aA][nN][dD]



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