[pypy-commit] pypy default: isdigit on strings

alex_gaynor noreply at buildbot.pypy.org
Sat Dec 15 05:29:44 CET 2012


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r59436:e35b7884dcc9
Date: 2012-12-14 20:29 -0800
http://bitbucket.org/pypy/pypy/changeset/e35b7884dcc9/

Log:	isdigit on strings

diff --git a/pypy/annotation/unaryop.py b/pypy/annotation/unaryop.py
--- a/pypy/annotation/unaryop.py
+++ b/pypy/annotation/unaryop.py
@@ -528,6 +528,9 @@
 
 
 class __extend__(SomeString):
+    def method_isdigit(chr):
+        return s_Bool
+
     def method_isalpha(chr):
         return s_Bool
 
@@ -566,12 +569,6 @@
     def method_isspace(chr):
         return s_Bool
 
-    def method_isdigit(chr):
-        return s_Bool
-
-    def method_isalpha(chr):
-        return s_Bool
-
     def method_isalnum(chr):
         return s_Bool
 
diff --git a/pypy/rpython/rstr.py b/pypy/rpython/rstr.py
--- a/pypy/rpython/rstr.py
+++ b/pypy/rpython/rstr.py
@@ -249,6 +249,12 @@
         hop.exception_cannot_occur()
         return hop.gendirectcall(self.ll.ll_lower, v_str)
 
+    def rtype_method_isdigit(self, hop):
+        string_repr = hop.args_r[0].repr
+        [v_str] = hop.inputargs(string_repr)
+        hop.exception_cannot_occur()
+        return hop.gendirectcall(self.ll.ll_isdigit, v_str)
+
     def rtype_method_isalpha(self, hop):
         string_repr = hop.args_r[0].repr
         [v_str] = hop.inputargs(string_repr)
@@ -744,13 +750,16 @@
 class AbstractLLHelpers:
     __metaclass__ = StaticMethods
 
-    def ll_char_isspace(ch):
-        c = ord(ch)
-        return c == 32 or (9 <= c <= 13)   # c in (9, 10, 11, 12, 13, 32)
+    def ll_isdigit(s):
+        from pypy.rpython.annlowlevel import hlstr
 
-    def ll_char_isdigit(ch):
-        c = ord(ch)
-        return c <= 57 and c >= 48
+        s = hlstr(s)
+        if not s:
+            return False
+        for ch in s:
+            if not ch.isdigit():
+                return False
+        return True
 
     def ll_isalpha(s):
         from pypy.rpython.annlowlevel import hlstr
@@ -763,6 +772,14 @@
                 return False
         return True
 
+    def ll_char_isspace(ch):
+        c = ord(ch)
+        return c == 32 or (9 <= c <= 13)   # c in (9, 10, 11, 12, 13, 32)
+
+    def ll_char_isdigit(ch):
+        c = ord(ch)
+        return c <= 57 and c >= 48
+
     def ll_char_isalpha(ch):
         c = ord(ch)
         if c >= 97:
diff --git a/pypy/rpython/test/test_rstr.py b/pypy/rpython/test/test_rstr.py
--- a/pypy/rpython/test/test_rstr.py
+++ b/pypy/rpython/test/test_rstr.py
@@ -138,6 +138,15 @@
             res = self.interpret(fn, [ch])
             assert res == fn(ch)
 
+    def test_isdigit(self):
+        const = self.const
+
+        def fn(i):
+            consts = [const(''), const('anc'), const('abc123'), const('123')]
+            return consts[i].isdigit()
+        for i in xrange(3):
+            assert self.interpret(fn, [i]) == fn(i)
+
     def test_str_isalpha(self):
         const = self.const
 


More information about the pypy-commit mailing list