From 2a7d302812b8e01a6c9c79bd640ef8d8d64e66cb Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 15 Sep 2015 10:25:25 -0400 Subject: [PATCH] lib/core: Added validation method for hexdigest Signed-off-by: Lucas Bajolet --- lib/core/bytes.nit | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/core/bytes.nit b/lib/core/bytes.nit index 59c4c5f..ad1f6ef 100644 --- a/lib/core/bytes.nit +++ b/lib/core/bytes.nit @@ -19,6 +19,31 @@ import kernel import collection::array intrude import text::flat +redef class Byte + # Is `self` a valid hexadecimal digit (in ASCII) + # + # ~~~nit + # intrude import core::bytes + # assert not '/'.ascii.to_b.is_valid_hexdigit + # assert '0'.ascii.to_b.is_valid_hexdigit + # assert '9'.ascii.to_b.is_valid_hexdigit + # assert not ':'.ascii.to_b.is_valid_hexdigit + # assert not '@'.ascii.to_b.is_valid_hexdigit + # assert 'A'.ascii.to_b.is_valid_hexdigit + # assert 'F'.ascii.to_b.is_valid_hexdigit + # assert not 'G'.ascii.to_b.is_valid_hexdigit + # assert not '`'.ascii.to_b.is_valid_hexdigit + # assert 'a'.ascii.to_b.is_valid_hexdigit + # assert 'f'.ascii.to_b.is_valid_hexdigit + # assert not 'g'.ascii.to_b.is_valid_hexdigit + # ~~~ + private fun is_valid_hexdigit: Bool do + return (self >= 0x30u8 and self <= 0x39u8) or + (self >= 0x41u8 and self <= 0x46u8) or + (self >= 0x61u8 and self <= 0x66u8) + end +end + # A buffer containing Byte-manipulation facilities # # Uses Copy-On-Write when persisted @@ -253,6 +278,15 @@ redef class Text return b end + # Is `self` a valid hexdigest ? + # + # assert "0B1d3F".is_valid_hexdigest + # assert not "5G".is_valid_hexdigest + fun is_valid_hexdigest: Bool do + for i in bytes do if not i.is_valid_hexdigit then return false + return true + end + # Appends `self.bytes` to `b` fun append_to_bytes(b: Bytes) do for s in substrings do -- 1.7.9.5