stdlib: AbstractString, added function is_numeric
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 18 Mar 2013 19:04:04 +0000 (15:04 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 18 Mar 2013 19:04:04 +0000 (15:04 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/string.nit

index 01c3a89..b80cb04 100644 (file)
@@ -140,6 +140,25 @@ abstract class AbstractString
                end
        end
 
+       # Returns true if the string contains only Numeric values (and one "," or one "." character)
+       fun is_numeric: Bool
+       do
+               var has_point_or_comma = false
+               for i in self
+               do
+                       if not i.is_numeric
+                       then
+                               if (i == '.' or i == ',') and not has_point_or_comma
+                               then
+                                       has_point_or_comma = true
+                               else
+                                       return false
+                               end
+                       end
+               end
+               return true
+       end
+
        # A upper case version of `self'
        fun to_upper: String
        do
@@ -513,6 +532,17 @@ redef class Char
                s[0] = self
                return s.to_s
        end
+
+       # Returns true if the char is a numerical digit
+       fun is_numeric: Bool
+       do
+               if self >= '0' and self <= '9'
+               then
+                       return true
+               end
+               return false
+       end
+
 end
 
 redef class Collection[E]