Author: Antonio Cuni <[email protected]>
Branch: fastjson
Changeset: r64791:65996c45a142
Date: 2013-06-05 12:32 +0200
http://bitbucket.org/pypy/pypy/changeset/65996c45a142/

Log:    add parsing of integers

diff --git a/pypy/module/_fastjson/interp_decoder.py 
b/pypy/module/_fastjson/interp_decoder.py
--- a/pypy/module/_fastjson/interp_decoder.py
+++ b/pypy/module/_fastjson/interp_decoder.py
@@ -51,14 +51,36 @@
 
     def decode_any(self):
         self.skip_whitespace()
-        ch = self.next()
+        ch = self.peek()
         if ch == '"':
+            self.next()
             return self.decode_string()
+        elif ch.isdigit() or ch == '-':
+            return self.decode_numeric(ch)
         elif ch == '{':
+            self.next()
             return self.decode_object()
         else:
             assert False, 'Unkown char: %s' % ch
 
+    def decode_numeric(self, ch):
+        intval = 0
+        sign = 1
+        if ch == '-':
+            sign = -1
+            self.next()
+
+        while not self.eof():
+            ch = self.peek()
+            if ch.isdigit():
+                intval = intval*10 + ord(ch)-ord('0')
+                self.next()
+            else:
+                break
+        #
+        intval = intval*sign
+        return self.space.wrap(intval)
+
     def decode_object(self):
         start = self.i
         w_dict = self.space.newdict()
diff --git a/pypy/module/_fastjson/test/test__fastjson.py 
b/pypy/module/_fastjson/test/test__fastjson.py
--- a/pypy/module/_fastjson/test/test__fastjson.py
+++ b/pypy/module/_fastjson/test/test__fastjson.py
@@ -69,6 +69,12 @@
         s = r'"\u1234"'
         assert _fastjson.loads(s) == u'\u1234'
 
+    def test_decode_numeric(self):
+        import _fastjson
+        assert _fastjson.loads('42') == 42
+        assert _fastjson.loads('-42') == -42
+        raises(ValueError, "_fastjson.loads('42 abc')")
+
     def test_decode_object(self):
         import _fastjson
         assert _fastjson.loads('{}') == {}
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to