diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-05-09 18:41:13 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-05-09 18:41:13 -0700 |
commit | bb9317e1eb68a975cb9462d0cd61c62ff6061a49 (patch) | |
tree | 24d6d9346a647aadd37e7646c53e1596e26e8eec | |
parent | fe0e6110261a2ccc742334699aa247b428348b82 (diff) |
sscanf support for negative integers
-rw-r--r-- | src/library.js | 9 | ||||
-rwxr-xr-x | tests/runner.py | 7 |
2 files changed, 12 insertions, 4 deletions
diff --git a/src/library.js b/src/library.js index f2f1a3cb..bac57b59 100644 --- a/src/library.js +++ b/src/library.js @@ -2306,13 +2306,15 @@ LibraryManager.library = { unget(); next = get(); } else { + var first = true; while ((curr < max_ || isNaN(max_)) && next > 0) { if (!(next in __scanString.whiteSpace) && // stop on whitespace - ((type == 's' || - ((type === 'd' || type == 'u') && next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0)) || + (type == 's' || + ((type === 'd' || type == 'u') && ((next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0)) || + (first && next == '-'.charCodeAt(0)))) || (type === 'x' && (next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0) || next >= 'a'.charCodeAt(0) && next <= 'f'.charCodeAt(0) || - next >= 'A'.charCodeAt(0) && next <= 'F'.charCodeAt(0))))) && + next >= 'A'.charCodeAt(0) && next <= 'F'.charCodeAt(0)))) && (formatIndex >= format.length || next !== format[formatIndex].charCodeAt(0))) { // Stop when we read something that is coming up buffer.push(String.fromCharCode(next)); next = get(); @@ -2320,6 +2322,7 @@ LibraryManager.library = { } else { break; } + first = false; } } if (buffer.length === 0) return 0; // Failure. diff --git a/tests/runner.py b/tests/runner.py index 17acdbd9..f44dc95a 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -3819,12 +3819,17 @@ at function.:blag printf("%d\n", sscanf(" vert 87 ( 0.481565 0.059481 ) 0 1\n", " vert %d ( %f %f ) %hu %hu", &index, &u, &v, &start, &count)); printf("%d,%.6f,%.6f,%hu,%hu\n", index, u, v, start, count); + int neg, neg2, neg3 = 0; + printf("%d\n", sscanf("-123 -765 -34-6", "%d %u %d", &neg, &neg2, &neg3)); + printf("%d,%u,%d\n", neg, neg2, neg3); + return 0; } ''' self.do_run(src, 'en-us : 2\nen-r : 99\nen : 3\n1.234567, 0.000000\n-3.0300\n|some|\n|something|\n|somethingmoar|\n' + '1\n1499\n' + - '5\n87,0.481565,0.059481,0,1\n') + '5\n87,0.481565,0.059481,0,1\n' + + '3\n-123,4294966531,-34\n') def test_sscanf_2(self): # doubles |