lib/core: Added hexdigest_to_bytes method from String to Bytes
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 15 Sep 2015 14:26:00 +0000 (10:26 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 15 Sep 2015 14:51:17 +0000 (10:51 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/bytes.nit

index ad1f6ef..deddbd8 100644 (file)
@@ -42,6 +42,28 @@ redef class Byte
                       (self >= 0x41u8 and self <= 0x46u8) or
                       (self >= 0x61u8 and self <= 0x66u8)
        end
+
+       # `self` as a hexdigit to its byte value
+       #
+       # ~~~nit
+       # intrude import core::bytes
+       # assert 0x39u8.hexdigit_to_byteval == 0x09u8
+       # assert 0x43u8.hexdigit_to_byteval == 0x0Cu8
+       # ~~~
+       #
+       # REQUIRE: `self.is_valid_hexdigit`
+       private fun hexdigit_to_byteval: Byte do
+               if self >= 0x30u8 and self <= 0x39u8 then
+                       return self - 0x30u8
+               else if self >= 0x41u8 and self <= 0x46u8 then
+                       return self - 0x37u8
+               else if self >= 0x61u8 and self <= 0x66u8 then
+                       return self - 0x57u8
+               end
+               # Happens only if the requirement is not met.
+               # i.e. this abort is here to please the compiler
+               abort
+       end
 end
 
 # A buffer containing Byte-manipulation facilities
@@ -294,6 +316,24 @@ redef class Text
                        b.append_ns_from(s.items, s.bytelen, from)
                end
        end
+
+       # Returns a new `Bytes` instance with the digest as content
+       #
+       #     assert "0B1F4D".hexdigest_to_bytes == [0x0Bu8, 0x1Fu8, 0x4Du8]
+       #
+       # REQUIRE: `self` is a valid hexdigest and hexdigest.length % 2 == 0
+       fun hexdigest_to_bytes: Bytes do
+               var b = bytes
+               var pos = 0
+               var max = bytelen
+               var ret = new Bytes.with_capacity(max / 2)
+               while pos < max do
+                       ret.add((b[pos].hexdigit_to_byteval << 4) |
+                       b[pos + 1].hexdigit_to_byteval)
+                       pos += 2
+               end
+               return ret
+       end
 end
 
 redef class FlatText