diff options
-rw-r--r-- | src/library.js | 8 | ||||
-rw-r--r-- | tests/runner.py | 22 |
2 files changed, 29 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js index de42d1e0..2e0adb64 100644 --- a/src/library.js +++ b/src/library.js @@ -3156,8 +3156,14 @@ LibraryManager.library = { abs: 'Math.abs', // XXX should be integer? + atoi__deps: ['isspace', 'isdigit'], atoi: function(s) { - return Math.floor(Number(Pointer_stringify(s))); + var c; + while ((c = {{{ makeGetValue('s', 0, 'i8') }}}) && _isspace(c)) s++; + if (!c || !_isdigit(c)) return 0; + var e = s; + while ((c = {{{ makeGetValue('e', 0, 'i8') }}}) && _isdigit(c)) e++; + return Math.floor(Number(Pointer_stringify(s).substr(0, e-s))); }, exit__deps: ['_exit'], diff --git a/tests/runner.py b/tests/runner.py index 517f2bcf..292d1104 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -2349,6 +2349,28 @@ if 'benchmark' not in str(sys.argv): ''' self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected)) + def test_atoi(self): + src = r''' + #include <stdio.h> + #include <stdlib.h> + + int main () { + printf("%d\n", atoi("")); + printf("%d\n", atoi("a")); + printf("%d\n", atoi(" b")); + printf("%d\n", atoi(" c ")); + printf("%d\n", atoi("6")); + printf("%d\n", atoi(" 5")); + printf("%d\n", atoi("4 ")); + printf("%d\n", atoi("3 6")); + printf("%d\n", atoi(" 3 7")); + printf("%d\n", atoi("9 d")); + printf("%d\n", atoi(" 8 e")); + return 0; + } + ''' + self.do_run(src) + def test_sscanf(self): src = r''' #include <stdio.h> |