rev 616 - in trunk: . pr/test src
SVN User <[email protected]> Sun, 13 Jun 2004 00:19:58 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-06-13 00:19:55 -0400 (Sun, 13 Jun 2004)
New Revision: 616
Added:
trunk/pr/test/floatstr.pr
Modified:
trunk/STATUS.txt
trunk/pr/test/test.pr
trunk/src/builtins-float.c
Log:
added float.str_(spec) and floatstr.pr
Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt 2004-06-13 02:59:52 UTC (rev 615)
+++ trunk/STATUS.txt 2004-06-13 04:19:55 UTC (rev 616)
@@ -1,7 +1,7 @@
----------------------- TO-DO (highest priority first) ------------------------
---- float.str_(spec), and vars_ default for str.fmt()
+--- add vars_ default for str.fmt()
--- support list and dictionary object params in string.fmt() ???
--- add \u0000 \U00000000 and \N{name} esc sequences
Added: trunk/pr/test/floatstr.pr
===================================================================
--- trunk/pr/test/floatstr.pr 2004-06-13 02:59:52 UTC (rev 615)
+++ trunk/pr/test/floatstr.pr 2004-06-13 04:19:55 UTC (rev 616)
@@ -0,0 +1,27 @@
+#!/usr/bin/env prothon
+
+def test(n, evalstr, exp):
+ exec 'res = ' + evalstr
+ if res != exp:
+ print "test", n, 'failed'
+ print 'evaluating:', evalstr
+ print 'expected: ', exp
+ print 'found: ', res
+ sys.exit(1)
+
+test(1, '"{0}".fmt(12.34)', '12.34')
+test(2, '"{0}".fmt(-12.34)', '-12.34')
+test(3, '"{0:3.4}".fmt(12.34)', '12.3400')
+test(4, '"{0:3.4f}".fmt(-127.6876)', '-127.6876')
+test(5, '"{0:3.2}".fmt(-127.6876)', '-127.69')
+test(6, '"{0:10.2}".fmt(-127.6876)', ' -127.69')
+test(7, '"{0:-10.2}".fmt(-127.6876)', '-127.69 ')
+test(8, '"{0:-010.2}".fmt(-127.6876)', '-127.69000')
+test(9, '"{0:010.2}".fmt(-127.6876)', '-000127.69')
+test(10, '"{0:10}".fmt(-127.6876)', '-127.687600')
+test(11, '"{0:i}".fmt(-127.6876)', '-128')
+test(12, '"{0:#x}".fmt(-127.6876)', '-0x80')
+
+print """
+all tests passed
+"""
\ No newline at end of file
Property changes on: trunk/pr/test/floatstr.pr
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr 2004-06-13 02:59:52 UTC (rev 615)
+++ trunk/pr/test/test.pr 2004-06-13 04:19:55 UTC (rev 616)
@@ -9,33 +9,18 @@
print 'found: ', res
sys.exit(1)
-test(1, '"{0}".fmt(1276876)', '1276876')
-test(2, '"{0}".fmt(-1276876)', '-1276876')
-test(3, '"{0:d}".fmt(1276876)', '1276876')
-test(4, '"{0:d}".fmt(-1276876)', '-1276876')
-test(5, '"{0:o}".fmt(1276876)', '4675714')
-test(6, '"{0:o}".fmt(-1276876)', '-4675714')
-test(7, '"{0:#o}".fmt(1276876)', '0o4675714')
-test(8, '"{0:#o}".fmt(-1276876)', '-0o4675714')
-test(9, '"{0:x}".fmt(1276876)', '137bcc')
-test(10, '"{0:x}".fmt(-1276876)', '-137bcc')
-test(11, '"{0:#x}".fmt(1276876)', '0x137bcc')
-test(12, '"{0:#x}".fmt(-1276876)', '-0x137bcc')
-test(13, '"{0:X}".fmt(1276876)', '137BCC')
-test(14, '"{0:X}".fmt(-1276876)', '-137BCC')
-test(15, '"{0:#X}".fmt(1276876)', '0x137BCC')
-test(16, '"{0:#X}".fmt(-1276876)', '-0x137BCC')
-test(17, '"{0}".fmt(12768766576576563764)', '12768766576576563764')
-test(18, '"{0}".fmt(-12768766576576563764)', '-12768766576576563764')
-test(19, '"{0:10}".fmt(12345)', ' 12345')
-test(20, '"{0:-10}".fmt(12345)', '12345 ')
-test(21, '"{0:10.7}".fmt(12345)', ' 0012345')
-test(22, '"{0:10.7}".fmt(-12345)', ' -0012345')
-test(23, '"{0:+10.7}".fmt(12345)', ' +0012345')
-test(24, '"{0:+10.7}".fmt(-12345)', ' -0012345')
-test(25, '"{0: }".fmt(12345)', ' 12345')
-test(26, '"{0: }".fmt(-12345)', '-12345')
-test(27, '"{0:-+10.7}".fmt(-12345)', '-0012345 ')
+test(1, '"{0}".fmt(12.34)', '12.34')
+test(2, '"{0}".fmt(-12.34)', '-12.34')
+test(3, '"{0:3.4}".fmt(12.34)', '12.3400')
+test(4, '"{0:3.4f}".fmt(-127.6876)', '-127.6876')
+test(5, '"{0:3.2}".fmt(-127.6876)', '-127.69')
+test(6, '"{0:10.2}".fmt(-127.6876)', ' -127.69')
+test(7, '"{0:-10.2}".fmt(-127.6876)', '-127.69 ')
+test(8, '"{0:-010.2}".fmt(-127.6876)', '-127.69000')
+test(9, '"{0:010.2}".fmt(-127.6876)', '-000127.69')
+test(10, '"{0:10}".fmt(-127.6876)', '-127.687600')
+test(11, '"{0:i}".fmt(-127.6876)', '-128')
+test(12, '"{0:#x}".fmt(-127.6876)', '-0x80')
print """
all tests passed
Modified: trunk/src/builtins-float.c
===================================================================
--- trunk/src/builtins-float.c 2004-06-13 02:59:52 UTC (rev 615)
+++ trunk/src/builtins-float.c 2004-06-13 04:19:55 UTC (rev 616)
@@ -358,10 +358,33 @@
else return OBJ(PR_FALSE);
}
-DEF(Float, str_, NULL){
- char str[1024];
+DEF(Float, str_, FPARM1(spec, NEW_STRING(""))) {
+ char str[256], *spec, spec_buf[80];
BIN_STR_CHK(Float);
- apr_snprintf(str, sizeof(str), "%g", Float_value(self));
+ STRING_PARAM(1, spec);
+ if (strlen(spec) >= sizeof(spec_buf)) {
+ raise_exception(ist, OBJ(OVERFLOW_EXC), "fmt spec too large");
+ return NULL;
+ }
+ if (strlen(spec)) {
+ char lastch = spec[strlen(spec)-1];
+ if ( lastch == 'd' || lastch == 'D' || lastch == 'i' || lastch == 'I' ||
+ lastch == 'c' || lastch == 'C' || lastch == 'o' || lastch == 'O' ||
+ lastch == 'x' || lastch == 'X' ) {
+ obj_p res_obj, int_obj;
+ apr_snprintf(str, sizeof(str), "%1.0f", Float_value(self));
+ int_obj = NEW_INT(apr_atoi64(str));
+ res_obj = call_func1(ist, int_obj, SYM(STR_), parms[1]);
+ del_unlock(int_obj);
+ return res_obj;
+ }
+ strcpy(spec_buf, "%");
+ strcat(spec_buf, spec);
+ if (lastch != 'f' && lastch != 'F' && lastch != 'e' && lastch != 'E')
+ strcat(spec_buf, "f");
+ } else
+ strcpy(spec_buf, "%g");
+ apr_snprintf(str, sizeof(str), spec_buf, Float_value(self));
return NEW_STRING(str);
}