aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/preamble.js32
-rw-r--r--tests/runner.py3
2 files changed, 21 insertions, 14 deletions
diff --git a/src/preamble.js b/src/preamble.js
index f0f6745b..3c7286cc 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -278,15 +278,15 @@ function __formatString() {
next = IHEAP[textIndex+1];
if (curr == '%'.charCodeAt(0)) {
// Handle very very simply formatting, namely only %.X[f|d|u|etc.]
- var limit = -1;
+ var precision = -1;
if (next == '.'.charCodeAt(0)) {
textIndex++;
- limit = 0;
+ precision = 0;
while(1) {
- var limitChr = IHEAP[textIndex+1];
- if (!(limitChr >= '0'.charCodeAt(0) && limitChr <= '9'.charCodeAt(0))) break;
- limit *= 10;
- limit += limitChr - '0'.charCodeAt(0);
+ var precisionChr = IHEAP[textIndex+1];
+ if (!(precisionChr >= '0'.charCodeAt(0) && precisionChr <= '9'.charCodeAt(0))) break;
+ precision *= 10;
+ precision += precisionChr - '0'.charCodeAt(0);
textIndex++;
}
next = IHEAP[textIndex+1];
@@ -310,14 +310,20 @@ function __formatString() {
} else {
argText = String(+currArg); // +: boolean=>int
}
- if (limit >= 0) {
- var dotIndex = argText.indexOf('.');
- if (dotIndex == -1 && next == 'f'.charCodeAt(0)) {
- dotIndex = argText.length;
- argText += '.';
+ if (precision >= 0) {
+ if (isFloatArg(next)) {
+ var dotIndex = argText.indexOf('.');
+ if (dotIndex == -1 && next == 'f'.charCodeAt(0)) {
+ dotIndex = argText.length;
+ argText += '.';
+ }
+ argText += '00000000000'; // padding
+ argText = argText.substr(0, dotIndex+1+precision);
+ } else {
+ while (argText.length < precision) {
+ argText = '0' + argText;
+ }
}
- argText += '00000000000'; // padding
- argText = argText.substr(0, dotIndex+1+limit);
}
argText.split('').forEach(function(chr) {
ret.push(chr.charCodeAt(0));
diff --git a/tests/runner.py b/tests/runner.py
index 5a9ccc7a..cc1b1309 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -238,11 +238,12 @@ if 'benchmark' not in sys.argv:
printf(",");
}
printf("*\\n");
+ printf("*%.1d,%.2d*\\n", 56, 9);
printf("*%ld*%p\\n", (long)21, &hash); // The %p should not enter an infinite loop!
return 0;
}
'''
- self.do_test(src, '*5,23,10,19,121,1,37,1,0*\n0:-1,1:134217727,2:4194303,3:131071,4:4095,5:127,6:3,7:0,8:0*\n*21*')
+ self.do_test(src, '*5,23,10,19,121,1,37,1,0*\n0:-1,1:134217727,2:4194303,3:131071,4:4095,5:127,6:3,7:0,8:0*\n*56,09*\n*21*')
def test_unsigned(self):
src = '''