lib: intro `Text::is_hex`
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 24 Jul 2014 14:38:11 +0000 (10:38 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 30 Jul 2014 13:10:31 +0000 (09:10 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/string.nit

index eb1e455..45e5712 100644 (file)
@@ -293,6 +293,21 @@ abstract class Text
                return true
        end
 
+       # Returns `true` if the string contains only Hex chars
+       #
+       #     assert "048bf".is_hex  == true
+       #     assert "ABCDEF".is_hex  == true
+       #     assert "0G".is_hex == false
+       fun is_hex: Bool
+       do
+               for c in self.chars do
+                       if not (c >= 'a' and c <= 'f') and
+                          not (c >= 'A' and c <= 'F') and
+                          not (c >= '0' and c <= '9') then return false
+               end
+               return true
+       end
+
        # Are all letters in `self` upper-case ?
        #
        #     assert "HELLO WORLD".is_upper == true