lib: Added `from_hex` service to transform an hexadecimal digit to an `Int`
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 14 Dec 2015 22:30:58 +0000 (17:30 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 17 Dec 2015 20:13:24 +0000 (15:13 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/text/abstract_text.nit

index cacee99..32628cd 100644 (file)
@@ -1732,6 +1732,19 @@ redef class Char
        do
                return self.is_numeric or self.is_alpha
        end
+
+       # Returns `self` to its int value
+       #
+       # REQUIRE: `is_hexdigit`
+       fun from_hex: Int do
+               if self >= '0' and self <= '9' then return code_point - 0x30
+               if self >= 'A' and self <= 'F' then return code_point - 0x37
+               if self >= 'a' and self <= 'f' then return code_point - 0x57
+               # Happens if self is not a hexdigit
+               assert self.is_hexdigit
+               # To make flow analysis happy
+               abort
+       end
 end
 
 redef class Collection[E]