[gcalctool/gcalctool-newui2] Support SI units
- From: Robert Ancell <rancell src gnome org>
- To: svn-commits-list gnome org
- Subject: [gcalctool/gcalctool-newui2] Support SI units
- Date: Sat, 4 Jul 2009 15:13:42 +0000 (UTC)
commit ac417b6af9f2f408c50df66f19b7efa3b807804a
Author: Robert Ancell <robert ancell gmail com>
Date: Thu Jul 2 14:40:02 2009 +1000
Support SI units
src/mp-convert.c | 40 ++++++++++++++++++++++++++++++++--------
src/mp-equation-lexer.l | 3 ++-
2 files changed, 34 insertions(+), 9 deletions(-)
---
diff --git a/src/mp-convert.c b/src/mp-convert.c
index e693445..1505f5c 100644
--- a/src/mp-convert.c
+++ b/src/mp-convert.c
@@ -594,13 +594,17 @@ char_val(char chr, int base)
void
mp_set_from_string(const char *str, MPNumber *z)
{
- int i, base, negate = 0;
+ int i, base, negate = 0, multiplier = 0;
const char *c, *end;
+ gboolean has_fraction = FALSE;
+
const char *base_suffixes[] = {"â??", "â??", "â??â??", NULL};
- int base_values[] = {2, 8, 16, 10};
- const char *fractions[] = {"½", "â??", "â??", "¼", "¾", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", NULL};
- int numerators[] = { 1, 1, 2, 1, 3, 1, 2, 3, 4, 1, 5, 1, 3, 5, 7};
- int denominators[] = { 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 8, 8, 8, 8};
+ int base_values[] = {2, 8, 16, 10};
+ const char *fractions[] = {"½", "â??", "â??", "¼", "¾", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", NULL};
+ int numerators[] = { 1, 1, 2, 1, 3, 1, 2, 3, 4, 1, 5, 1, 3, 5, 7};
+ int denominators[] = { 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 8, 8, 8, 8};
+ const char *si_suffixes[] = {"T", "G", "M", "k", "d", "c", "m", "u", "µ", "n", "p", "f", NULL};
+ int si_multipliers[] = { 12, 9, 6, 3, -1, -2, -3, -6, -6, -9, -12, -15};
/* Find the base */
end = str;
@@ -644,12 +648,25 @@ mp_set_from_string(const char *str, MPNumber *z)
mp_set_from_fraction(numerators[i], denominators[i], &fraction);
mp_add(z, &fraction, z);
}
+
+ if (*c == '.') {
+ has_fraction = TRUE;
+ c++;
+ } else {
+ for (i = 0; si_suffixes[i] != NULL; i++) {
+ if (strncmp(c, si_suffixes[i], strlen(si_suffixes[i])) == 0)
+ break;
+ }
+ if (si_suffixes[i] != NULL) {
+ has_fraction = TRUE;
+ multiplier = si_multipliers[i];
+ c += strlen(si_suffixes[i]);
+ }
+ }
/* Convert fractional part */
- if (*c == '.') {
+ if (has_fraction) {
MPNumber numerator, denominator;
-
- c++;
mp_set_from_integer(0, &numerator);
mp_set_from_integer(1, &denominator);
@@ -666,6 +683,13 @@ mp_set_from_string(const char *str, MPNumber *z)
if (c != end) {
// FIXME: Error decoding
}
+
+ if (multiplier != 0) {
+ MPNumber t;
+ mp_set_from_integer(10, &t);
+ mp_pwr_integer(&t, multiplier, &t);
+ mp_multiply(z, &t, z);
+ }
if (negate == 1)
mp_invert_sign(z, z);
diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l
index 9a7be18..2855cac 100644
--- a/src/mp-equation-lexer.l
+++ b/src/mp-equation-lexer.l
@@ -77,12 +77,13 @@ OCT_SUFFIX "â??"
DEC [0-9]
HEX [0-9]|[A-F]|[a-f]
HEX_SUFFIX "â??â??"
+SI_SUFFIX "T"|"G"|"M"|"k"|"d"|"c"|"m"|"u"|"µ"|"n"|"p"|"f"
SUPER_DIGITS "�"|"¹"|"²"|"³"|"�"|"�"|"�"|"�"|"�"|"�"
SUB_DIGITS "â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"
FRACTION "½"|"â??"|"â??"|"¼"|"¾"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"
HEX_NUM {HEX}+{HEX_SUFFIX}|{HEX}*{DECIMAL}{HEX}+{HEX_SUFFIX}
-DEC_NUM {DEC}+|{DEC}*{DECIMAL}{DEC}+|{FRACTION}|{DEC}{FRACTION}
+DEC_NUM {DEC}+|{DEC}+{SI_SUFFIX}|{DEC}*{DECIMAL}{DEC}+|{DEC}*{SI_SUFFIX}{DEC}+|{FRACTION}|{DEC}{FRACTION}
OCT_NUM {OCT}+{OCT_SUFFIX}|{OCT}*{DECIMAL}{OCT}+{OCT_SUFFIX}
BIN_NUM {BIN}+{BIN_SUFFIX}|{BIN}*{DECIMAL}{BIN}+{BIN_SUFFIX}
SUP_NUM {SUPER_DIGITS}+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]