Commits

Alex Gaynor committed 2523e19

Added str.isalnum for strings (it already existed on chars)

Comments (0)

Files changed (3)

rpython/annotator/unaryop.py

 
 
 class __extend__(SomeString):
-    def method_isdigit(chr):
+    def method_isdigit(str):
         return s_Bool
 
-    def method_isalpha(chr):
+    def method_isalpha(str):
+        return s_Bool
+
+    def method_isalnum(str):
         return s_Bool
 
     def method_upper(str):

rpython/rtyper/rstr.py

         hop.exception_cannot_occur()
         return hop.gendirectcall(self.ll.ll_isalpha, v_str)
 
+    def rtype_method_isalnum(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_isalnum, v_str)
+
     def _list_length_items(self, hop, v_lst, LIST):
         """Return two Variables containing the length and items of a
         list. Need to be overriden because it is typesystem-specific."""
                 return False
         return True
 
+    def ll_isalnum(s):
+        from rpython.rtyper.annlowlevel import hlstr
+
+        s = hlstr(s)
+        if not s:
+            return False
+        for ch in s:
+            if not ch.isalnum():
+                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)

rpython/rtyper/test/test_rstr.py

         for i in xrange(3):
             assert self.interpret(fn, [i]) == fn(i)
 
+    def test_str_isalnum(self):
+        const = self.const
+
+        def fn(i):
+            consts = [const(''), const('abc'), const('abc123'), const('abc123!')]
+            return consts[i].isalnum()
+        for i in xrange(3):
+            assert self.interpret(fn, [i]) == fn(i)
+
     def test_char_compare(self):
         const = self.const
         res = self.interpret(lambda c1, c2: c1 == c2,  [const('a'),