aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-11-02 18:21:55 -0700
committerAlon Zakai <alonzakai@gmail.com>2011-11-02 18:21:55 -0700
commit04ade3edb08382b56242725c8a13ee707281bde2 (patch)
tree9d6d144ad619558247522dc10219e9033d20e6c5
parent132bbc90e82bdeef7410ed50c9d6c513bc2d28ba (diff)
fix atoi
-rw-r--r--src/library.js8
-rw-r--r--tests/runner.py22
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>