[pypy-svn] r15797 - in pypy/dist/pypy: rpython rpython/module rpython/module/test translator/c translator/c/src translator/c/test

pedronis at codespeak.net pedronis at codespeak.net
Tue Aug 9 01:24:26 CEST 2005


Author: pedronis
Date: Tue Aug  9 01:24:23 2005
New Revision: 15797

Modified:
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/module/ll_strtod.py
   pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
   pypy/dist/pypy/rpython/rarithmetic.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/src/ll_strtod.h
   pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
low-level float->string helper formatd.



Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Tue Aug  9 01:24:23 2005
@@ -125,3 +125,5 @@
 # string->float helper
 from pypy.rpython import rarithmetic
 declare(rarithmetic.parts_to_float, float, 'll_strtod/parts_to_float')
+# float->string helper
+declare(rarithmetic.formatd, str, 'll_strtod/formatd')

Modified: pypy/dist/pypy/rpython/module/ll_strtod.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_strtod.py	(original)
+++ pypy/dist/pypy/rpython/module/ll_strtod.py	Tue Aug  9 01:24:23 2005
@@ -8,5 +8,8 @@
                                       from_rstr(beforept),
                                       from_rstr(afterpt),
                                       from_rstr(exponent))
-
 ll_strtod_parts_to_float.suggested_primitive = True
+
+def ll_strtod_formatd(fmt, x):
+    return to_rstr(rarithmetic.formatd(from_rstr(fmt), x))
+ll_strtod_formatd.suggested_primitive = True

Modified: pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_strtod.py	(original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_strtod.py	Tue Aug  9 01:24:23 2005
@@ -1,9 +1,9 @@
 
-from pypy.rpython.module.ll_strtod import ll_strtod_parts_to_float
-from pypy.rpython.module.support import to_rstr
+from pypy.rpython.module.ll_strtod import ll_strtod_parts_to_float, ll_strtod_formatd
+from pypy.rpython.module.support import to_rstr, from_rstr
 
 
-def test_it():
+def test_parts_to_float():
     data = [
     (("","1","","")     , 1.0),
     (("-","1","","")    , -1.0),
@@ -16,3 +16,7 @@
     for parts, val in data:
         assert ll_strtod_parts_to_float(*map(to_rstr, parts)) == val
     
+
+def test_formatd():
+    res = ll_strtod_formatd(to_rstr("%.2f"), 1.5)
+    assert from_rstr(res) == "1.50"

Modified: pypy/dist/pypy/rpython/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rarithmetic.py	Tue Aug  9 01:24:23 2005
@@ -378,3 +378,6 @@
     return float("%s%s.%se%s" % (sign, beforept, afterpt, exponent))
 
 # float -> string
+
+def formatd(fmt, x):
+    return fmt % (x,)

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Tue Aug  9 01:24:23 2005
@@ -32,6 +32,8 @@
     ll_math.ll_math_hypot: 'LL_math_hypot',
     ll_strtod.ll_strtod_parts_to_float:
         'LL_strtod_parts_to_float',
+    ll_strtod.ll_strtod_formatd:
+        'LL_strtod_formatd',
     }
 
 #______________________________________________________

Modified: pypy/dist/pypy/translator/c/src/ll_strtod.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll_strtod.h	(original)
+++ pypy/dist/pypy/translator/c/src/ll_strtod.h	Tue Aug  9 01:24:23 2005
@@ -1,4 +1,5 @@
 #include <locale.h>
+#include <ctype.h>
 
 double LL_strtod_parts_to_float(
 	RPyString *sign, 
@@ -54,3 +55,51 @@
         }
         return x;
 }
+
+
+RPyString *LL_strtod_formatd(RPyString *fmt, double x) {
+	char buffer[120]; /* this should be enough, from PyString_Format code */
+	int buflen = 120;
+	int res;
+	res = snprintf(buffer, buflen, RPyString_AsString(fmt), x);
+	if (res <= 0 || res >= buflen) {
+		strcpy(buffer, "??.?"); /* should not occur */
+	} else {
+		struct lconv *locale_data;
+		const char *decimal_point;
+		int decimal_point_len;
+		char *p;
+
+		locale_data = localeconv();
+		decimal_point = locale_data->decimal_point;
+		decimal_point_len = strlen(decimal_point);
+
+		if (decimal_point[0] != '.' || 
+		    decimal_point[1] != 0)
+		{
+			p = buffer;
+
+			if (*p == '+' || *p == '-')
+				p++;
+
+			while (isdigit((unsigned char)*p))
+				p++;
+
+			if (strncmp(p, decimal_point, decimal_point_len) == 0)
+			{
+				*p = '.';
+				p++;
+				if (decimal_point_len > 1) {
+					int rest_len;
+					rest_len = strlen(p + (decimal_point_len - 1));
+					memmove(p, p + (decimal_point_len - 1), 
+						rest_len);
+					p[rest_len] = 0;
+				}
+			}
+		}
+		
+	}
+
+	return RPyString_FromString(buffer);
+}

Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py	Tue Aug  9 01:24:23 2005
@@ -234,3 +234,14 @@
 
     for parts, val in data:
         assert f(*parts) == val
+
+def test_rarith_formatd():
+    from pypy.rpython.rarithmetic import formatd
+    def fn(x):
+        return formatd("%.2f", x)
+
+    f = compile(fn, [float])
+
+    assert f(0.0) == "0.00"
+    assert f(1.5) == "1.50"
+    assert f(2.0) == "2.00"



More information about the Pypy-commit mailing list