lib: Perfized `to_hex` and have it work anywhere in a `Text`
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 14 Dec 2015 22:31:53 +0000 (17:31 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 17 Dec 2015 20:13:27 +0000 (15:13 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/text/abstract_text.nit

index 17e4a23..1de1aa1 100644 (file)
@@ -248,7 +248,17 @@ abstract class Text
        # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
        #
        #     assert "ff".to_hex == 255
-       fun to_hex: Int do return a_to(16)
+       fun to_hex(pos, ln: nullable Int): Int do
+               var res = 0
+               if pos == null then pos = 0
+               if ln == null then ln = length - pos
+               var max = pos + ln
+               for i in [pos .. max[ do
+                       res <<= 4
+                       res += self[i].from_hex
+               end
+               return res
+       end
 
        # If `self` contains only digits <= '7', return the corresponding integer.
        #