diff options
author | Aleksander Guryanov <caiiiycuk@gmail.com> | 2012-10-28 19:29:00 +0700 |
---|---|---|
committer | Aleksander Guryanov <caiiiycuk@gmail.com> | 2012-10-31 21:47:42 +0700 |
commit | 192e25b88589bb137fb960de31b3e08de2562991 (patch) | |
tree | a04c14cc0363ae2b55a8e0477af58de53d838222 | |
parent | 4b94e181cb4ff08b26264c34ed6818f5a3fd2225 (diff) |
Fix sscanf whitespace bug
Add test for sscanf
-rw-r--r-- | src/library.js | 17 | ||||
-rwxr-xr-x | tests/runner.py | 26 |
2 files changed, 36 insertions, 7 deletions
diff --git a/src/library.js b/src/library.js index 1cfe309a..4ad6e06a 100644 --- a/src/library.js +++ b/src/library.js @@ -2456,19 +2456,22 @@ LibraryManager.library = { var fields = 0; var argIndex = 0; var next; - // remove initial whitespace - while (1) { - next = get(); - if (next == 0) return 0; - if (!(next in __scanString.whiteSpace)) break; - } - unget(next); + next = 1; mainLoop: for (var formatIndex = 0; formatIndex < format.length; formatIndex++) { + // remove whitespace + while (1) { + next = get(); + if (next == 0) return fields; + if (!(next in __scanString.whiteSpace)) break; + } + unget(next); + if (next <= 0) return fields; var next = get(); if (next <= 0) return fields; // End of input. + if (format[formatIndex] === '%') { formatIndex++; var maxSpecifierStart = formatIndex; diff --git a/tests/runner.py b/tests/runner.py index a24f2d91..8af631a8 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -4378,6 +4378,32 @@ Pass: 0.000012 0.000012''') ''' self.do_run(src, '''[DEBUG] word 1: version, l: 7\n1,one,4''') + def test_sscanf_whitespace(self): + src = r''' + #include<stdio.h> + + int main() { + short int x; + short int y; + + const char* buffer[] = { + "173,16", + " 16,173", + "183, 173", + " 17, 287", + " 98, 123, " + }; + + for (int i=0; i<5; ++i) { + sscanf(buffer[i], "%hd,%hd", &x, &y); + printf("%d:%d,%d ", i, x, y); + } + + return 0; + } + ''' + self.do_run(src, '''0:173,16 1:16,173 2:183,173 3:17,287 4:98,123''') + def test_langinfo(self): src = open(path_from_root('tests', 'langinfo', 'test.c'), 'r').read() expected = open(path_from_root('tests', 'langinfo', 'output.txt'), 'r').read() |