[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/utils/abakus/src
From:       Michael Pyne <michael.pyne () kdemail ! net>
Date:       2005-09-13 3:09:52
Message-ID: 1126580992.210962.19854.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 460128 by mpyne:

Try to support the locale's current decimal separator in abakus instead of always using
US Decimal.


 M  +1 -1      abakus.cpp  
 M  +2 -2      lexer.ll  
 M  +35 -2     numerictypes.cpp  
 M  +4 -15     numerictypes.h  
 M  +11 -0     parser.yy  


--- trunk/playground/utils/abakus/src/abakus.cpp #460127:460128
@@ -29,7 +29,7 @@
 
 #include "mainwindow.h"
 
-const char *const version = "0.90";
+const char *const version = "0.91-rc1";
 
 int main(int argc, char **argv)
 {
--- trunk/playground/utils/abakus/src/lexer.ll #460127:460128
@@ -75,7 +75,7 @@
  * digits, such as .32, -234.45, .0, etc.  Numbers are only read in the BEGIN
  * state.
  */
-{DIGITS}*(\.{DIGITS}+)(e[-+]?{DIGITS}+)? {
+{DIGITS}*([\.,]{DIGITS}+)(e[-+]?{DIGITS}+)? {
     yyCurTokenPos += yyThisTokenLength;
     yyThisTokenLength = yyleng;
     return NUM;
@@ -91,7 +91,7 @@
  /* Read numbers with at least the integral part, such as +4234, -34e8, etc.
  * Numbers are only read in the BEGIN state.
  */
-{DIGITS}+(\.{DIGITS}*)?(e[-+]?{DIGITS}+)? {
+{DIGITS}+([\.,]{DIGITS}*)?(e[-+]?{DIGITS}+)? {
     yyCurTokenPos += yyThisTokenLength;
     yyThisTokenLength = yyleng;
     return NUM;
--- trunk/playground/utils/abakus/src/numerictypes.cpp #460127:460128
@@ -21,6 +21,8 @@
 #include "hmath.h"
 
 #include <kdebug.h>
+#include <kglobal.h>
+#include <klocale.h>
 
 Abakus::TrigMode Abakus::m_trigMode = Abakus::Degrees;
 int Abakus::m_prec = -1;
@@ -36,6 +38,7 @@
     QRegExp zeroKiller ("0*$");
     mp_exp_t exp;
     int desiredPrecision = Abakus::m_prec;
+    QString decimalSymbol = KGlobal::locale()->decimalSymbol();
 
     if(desiredPrecision < 0)
         desiredPrecision = 8;
@@ -77,7 +80,7 @@
 
         r.append(QString("e%1").arg(exp - 1));
 
-        return sign + l + "." + r;
+        return sign + l + decimalSymbol + r;
     }
     else
     {
@@ -131,7 +134,7 @@
     if(r.isEmpty())
         return sign + l;
 
-    return sign + l + "." + r;
+    return sign + l + decimalSymbol + r;
 }
 
 } // namespace Abakus
@@ -164,6 +167,36 @@
 
 #else
 
+// Converts hmath number to a string.
+
+namespace Abakus
+{
+
+QString convertToString(const HNumber &num)
+{
+    QString str = HMath::formatGenString(num, m_prec);
+    QString decimalSymbol = KGlobal::locale()->decimalSymbol();
+    str.replace('.', decimalSymbol);
+
+    QStringList parts = QStringList::split("e", str);
+    QRegExp zeroKiller("(" + QRegExp::escape(decimalSymbol) +
+                       "\\d*[1-9])0*$"); // Remove trailing zeroes.
+    QRegExp zeroKiller2("(" + QRegExp::escape(decimalSymbol) + ")0*$");
+
+    str = parts[0];
+    str.replace(zeroKiller, "\\1");
+    str.replace(zeroKiller2, "\\1");
+    if(str.endsWith(decimalSymbol))
+        str.truncate(str.length() - 1); // Remove trailing period.
+
+    if(parts.count() > 1 && parts[1] != "0")
+        str += QString("e%1").arg(parts[1]);
+
+    return str;
+}
+
+} // namespace Abakus.
+
 const Abakus::number_t::value_type Abakus::number_t::PI = HMath::pi();
 const Abakus::number_t::value_type Abakus::number_t::E = HMath::exp(1);
 
--- trunk/playground/utils/abakus/src/numerictypes.h #460127:460128
@@ -506,6 +506,9 @@
 
 #else
 
+// Defined in numerictypes.cpp for ease of reimplementation.
+QString convertToString(const HNumber &num);
+
 /**
  * Specialization for internal HMath library, used if MPFR isn't usable.
  *
@@ -663,21 +666,7 @@
 
     QString toString() const
     {
-        QString str = HMath::formatGenString(m_t, m_prec);
-	QStringList parts = QStringList::split("e", str);
-	QRegExp zeroKiller("(\\.\\d*[1-9])0*$"); // Remove trailing zeroes.
-	QRegExp zeroKiller2("(\\.)0*$");
-
-	str = parts[0];
-	str.replace(zeroKiller, "\\1");
-	str.replace(zeroKiller2, "\\1");
-	if(str.endsWith("."))
-	    str.truncate(str.length() - 1); // Remove trailing period.
-
-	if(parts.count() > 1 && parts[1] != "0")
-	    str += QString("e%1").arg(parts[1]);
-
-        return str;
+        return convertToString(m_t);
     }
 
     static number<HNumber> nan()
--- trunk/playground/utils/abakus/src/parser.yy #460127:460128
@@ -21,6 +21,7 @@
 /* Add necessary includes here. */
 #include <kdebug.h>
 #include <klocale.h>
+#include <kglobal.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -314,6 +315,16 @@
 VALUE: NUMBER { $$ = $1; }
 
 NUMBER: NUM {
+    KLocale *locale = KGlobal::locale();
+    QChar decimal = locale->decimalSymbol()[0];
+
+    // Replace current decimal separator with US Decimal separator to be
+    // evil.
+    unsigned len = strlen(yytext);
+    for(unsigned i = 0; i < len; ++i)
+	if(yytext[i] == decimal)
+	    yytext[i] = '.';
+
     Abakus::number_t value(yytext);
 
     $$ = new NumericValue(value);
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic