X-Git-Url: http://nitlanguage.org diff --git a/lib/core/bytes.nit b/lib/core/bytes.nit index deddbd8..94d3d27 100644 --- a/lib/core/bytes.nit +++ b/lib/core/bytes.nit @@ -20,22 +20,30 @@ import collection::array intrude import text::flat redef class Byte + # Write self as a string into `ns` at position `pos` + private fun add_digest_at(ns: NativeString, pos: Int) do + var tmp = (0xF0u8 & self) >> 4 + ns[pos] = if tmp >= 0x0Au8 then tmp + 0x37u8 else tmp + 0x30u8 + tmp = 0x0Fu8 & self + ns[pos + 1] = if tmp >= 0x0Au8 then tmp + 0x37u8 else tmp + 0x30u8 + end + # 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 + # assert not '/'.ascii.is_valid_hexdigit + # assert '0'.ascii.is_valid_hexdigit + # assert '9'.ascii.is_valid_hexdigit + # assert not ':'.ascii.is_valid_hexdigit + # assert not '@'.ascii.is_valid_hexdigit + # assert 'A'.ascii.is_valid_hexdigit + # assert 'F'.ascii.is_valid_hexdigit + # assert not 'G'.ascii.is_valid_hexdigit + # assert not '`'.ascii.is_valid_hexdigit + # assert 'a'.ascii.is_valid_hexdigit + # assert 'f'.ascii.is_valid_hexdigit + # assert not 'g'.ascii.is_valid_hexdigit # ~~~ private fun is_valid_hexdigit: Bool do return (self >= 0x30u8 and self <= 0x39u8) or @@ -73,7 +81,7 @@ class Bytes super AbstractArray[Byte] # A NativeString being a char*, it can be used as underlying representation here. - private var items: NativeString + var items: NativeString # Number of bytes in the array redef var length @@ -110,6 +118,20 @@ class Bytes return items[i] end + # Returns self as a hexadecimal digest + fun hexdigest: String do + var elen = length * 2 + var ns = new NativeString(elen) + var i = 0 + var oi = 0 + while i < length do + self[i].add_digest_at(ns, oi) + i += 1 + oi += 2 + end + return new FlatString.full(ns, elen, 0, elen - 1, elen) + end + # var b = new Bytes.with_capacity(1) # b[0] = 101u8 # assert b.to_s == "e" @@ -193,80 +215,13 @@ class Bytes redef fun to_s do persisted = true var b = self - if not is_utf8 then - b = clean_utf8 - persisted = false - end - return new FlatString.with_infos(b.items, b.length, 0, b.length -1) + var r = b.items.to_s_with_length(length) + if r != items then persisted = false + return r end redef fun iterator do return new BytesIterator.with_buffer(self) - # Is the byte collection valid UTF-8 ? - fun is_utf8: Bool do - var charst = once [0x80u8, 0u8, 0xE0u8, 0xC0u8, 0xF0u8, 0xE0u8, 0xF8u8, 0xF0u8] - var lobounds = once [0, 0x80, 0x800, 0x10000] - var hibounds = once [0x7F, 0x7FF, 0xFFFF, 0x10FFFF] - var pos = 0 - var len = length - var mits = items - while pos < len do - var nxst = mits.length_of_char_at(pos) - var charst_index = (nxst - 1) * 2 - if mits[pos] & charst[charst_index] == charst[charst_index + 1] then - var c = mits.char_at(pos) - var cp = c.ascii - if cp <= hibounds[nxst - 1] and cp >= lobounds[nxst - 1] then - if cp >= 0xD800 and cp <= 0xDFFF or - cp == 0xFFFE or cp == 0xFFFF then return false - else - return false - end - else - return false - end - pos += nxst - end - return true - end - - # Cleans the bytes of `self` to be UTF-8 compliant - private fun clean_utf8: Bytes do - var charst = once [0x80u8, 0u8, 0xE0u8, 0xC0u8, 0xF0u8, 0xE0u8, 0xF8u8, 0xF0u8] - var badchar = once [0xEFu8, 0xBFu8, 0xBDu8] - var lobounds = once [0, 0x80, 0x800, 0x10000] - var hibounds = once [0x7F, 0x7FF, 0xFFFF, 0x10FFFF] - var pos = 0 - var len = length - var ret = new Bytes.with_capacity(len) - var mits = items - while pos < len do - var nxst = mits.length_of_char_at(pos) - var charst_index = (nxst - 1) * 2 - if mits[pos] & charst[charst_index] == charst[charst_index + 1] then - var c = mits.char_at(pos) - var cp = c.ascii - if cp <= hibounds[nxst - 1] and cp >= lobounds[nxst - 1] then - if cp >= 0xD800 and cp <= 0xDFFF or - cp == 0xFFFE or cp == 0xFFFF then - ret.append badchar - pos += 1 - else - var pend = pos + nxst - for i in [pos .. pend[ do ret.add mits[i] - pos += nxst - end - else - ret.append badchar - pos += 1 - end - else - ret.append badchar - pos += 1 - end - end - return ret - end end private class BytesIterator @@ -278,7 +233,7 @@ private class BytesIterator var max: Int - init with_buffer(b: Bytes) do init(b.items, 0, b.length - 1) + init with_buffer(b: Bytes) do init(b.items, 0, b.length) redef fun is_ok do return index < max @@ -334,6 +289,20 @@ redef class Text end return ret end + + # Gets the hexdigest of the bytes of `self` + # + # assert "<STRING/&rt;".hexdigest == "266C743B535452494E47262334373B2672743B" + fun hexdigest: String do + var ln = bytelen + var outns = new NativeString(ln * 2) + var oi = 0 + for i in [0 .. ln[ do + bytes[i].add_digest_at(outns, oi) + oi += 2 + end + return new FlatString.with_infos(outns, ln * 2, 0, ln * 2 - 1) + end end redef class FlatText