From f375b5f7a7d29c166be41ed7edbdcfa0f2b7a209 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 18 Dec 2015 13:41:52 -0500 Subject: [PATCH] lib/core: Added an optimized `to_hex` function to `FlatText` Signed-off-by: Lucas Bajolet --- lib/core/text/flat.nit | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index c3332d0..b2336e6 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -278,6 +278,23 @@ redef class FlatText end redef fun [](index) do return _items.char_at(char_to_byte_index(index)) + + # If `self` contains only digits and alpha <= 'f', return the corresponding integer. + # + # assert "ff".to_hex == 255 + redef fun to_hex(pos, ln) do + var res = 0 + if pos == null then pos = 0 + if ln == null then ln = length - pos + pos = char_to_byte_index(pos) + var its = _items + var max = pos + ln + for i in [pos .. max[ do + res <<= 4 + res += its[i].ascii.from_hex + end + return res + end end # Immutable strings of characters. -- 1.7.9.5