diff options
author | Alon Zakai <azakai@mozilla.com> | 2010-12-11 21:29:03 -0800 |
---|---|---|
committer | Alon Zakai <azakai@mozilla.com> | 2010-12-11 21:29:03 -0800 |
commit | 240ff2ff8e8d797740a6ee1dd534240afed6d6e5 (patch) | |
tree | 7dd78102dd68c888a28321b73aa7f57ac6ba00cc | |
parent | 1776ba30d4a746cd4f1cf7f69ec4f9a7a762d34a (diff) |
improve printf formatting
-rw-r--r-- | src/preamble.js | 57 | ||||
-rw-r--r-- | tests/runner.py | 4 |
2 files changed, 33 insertions, 28 deletions
diff --git a/src/preamble.js b/src/preamble.js index 7e1300fb..3494053a 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -260,44 +260,47 @@ function __formatString() { curr = IHEAP[textIndex]; next = IHEAP[textIndex+1]; if (curr == '%'.charCodeAt(0)) { + // Handle very very simply formatting, namely only %.X[f|d|u|etc.] + var limit = -1; + if (next == '.'.charCodeAt(0)) { + textIndex++; + limit = 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); + textIndex++; + } + next = IHEAP[textIndex+1]; + } if (next == 'l'.charCodeAt(0)) { textIndex++; next = IHEAP[textIndex+1]; } - if (['d', 'u', 'p', 'f', '.'].indexOf(String.fromCharCode(next)) != -1) { + if (next == 'e'.charCodeAt(0) || next == 'g'.charCodeAt(0)) { + next = 'f'.charCodeAt(0); // no support for 'e' + } + if (['d', 'u', 'p', 'f'].indexOf(String.fromCharCode(next)) != -1) { var currArg; var argText; - // Handle very very simply formatting, namely only %.Xf - if (next == '.'.charCodeAt(0)) { - var limit = 0; - while(1) { - var limitChr = IHEAP[textIndex+2]; - if (!(limitChr >= '0'.charCodeAt(0) && limitChr <= '9'.charCodeAt(0))) break; - limit *= 10; - limit += limitChr - '0'.charCodeAt(0); - textIndex++; - } - textIndex--; - next = IHEAP[textIndex+1]; - currArg = getNextArg(next); + currArg = getNextArg(next); + argText = String(+currArg); // +: boolean=>int + if (next == 'u'.charCodeAt(0)) { + argText = String(unSign(currArg, 32)); + } else if (next == 'p'.charCodeAt(0)) { + argText = '0x' + currArg.toString(16); + } else { argText = String(+currArg); // +: boolean=>int + } + if (limit >= 0) { var dotIndex = argText.indexOf('.'); - if (dotIndex == -1) { + if (dotIndex == -1 && next == 'f'.charCodeAt(0)) { dotIndex = argText.length; argText += '.'; } argText += '00000000000'; // padding argText = argText.substr(0, dotIndex+1+limit); - textIndex += 2; - } else if (next == 'u'.charCodeAt(0)) { - currArg = getNextArg(next); - argText = String(unSign(currArg, 32)); - } else if (next == 'p'.charCodeAt(0)) { - currArg = getNextArg(next); - argText = '0x' + currArg.toString(16); - } else { - currArg = getNextArg(next); - argText = String(+currArg); // +: boolean=>int } argText.split('').forEach(function(chr) { ret.push(chr.charCodeAt(0)); @@ -310,8 +313,8 @@ function __formatString() { ret = ret.concat(getNextArg(next)); textIndex += 2; } else { - ret.push(curr); - textIndex += 1; // not sure what to do with this %, so print it + ret.push(next); + textIndex += 2; // not sure what to do with this %, so print it } } else { ret.push(curr); diff --git a/tests/runner.py b/tests/runner.py index e556f3c5..2e96651b 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -1039,11 +1039,13 @@ if 'benchmark' not in sys.argv: printf("*%d,%d,%d,%d,%d,%d*\\n", values[0], values[1], values[2], values[3], values[4], values[5]); printf("*stdin==0:%d*\\n", stdin == 0); // check that external values are at least not NULL + printf("*%%*\\n"); + printf("*%.1ld*\\n", 5); return 0; } ''' - self.do_test(src, '*1,2,3,5,5,6*\n*stdin==0:0*\n*cleaned*') + self.do_test(src, '*1,2,3,5,5,6*\n*stdin==0:0*\n*%*\n*5*\n*cleaned*') def test_statics(self): src = ''' |