rename `NativeString` to `CString`
[nit.git] / lib / core / bytes.nit
index 4465ffd..fed8173 100644 (file)
@@ -53,7 +53,7 @@ redef class Byte
        super BytePattern
 
        # Write self as a string into `ns` at position `pos`
-       private fun add_digest_at(ns: NativeString, pos: Int) do
+       private fun add_digest_at(ns: CString, pos: Int) do
                var tmp = (0xF0u8 & self) >> 4
                ns[pos] = if tmp >= 0x0Au8 then tmp + 0x37u8 else tmp + 0x30u8
                tmp = 0x0Fu8 & self
@@ -146,8 +146,8 @@ class Bytes
        super AbstractArray[Byte]
        super BytePattern
 
-       # A NativeString being a char*, it can be used as underlying representation here.
-       var items: NativeString
+       # A CString being a char*, it can be used as underlying representation here.
+       var items: CString
 
        # Number of bytes in the array
        redef var length
@@ -163,13 +163,13 @@ class Bytes
        #     var b = new Bytes.empty
        #     assert b.to_s == ""
        init empty do
-               var ns = new NativeString(0)
+               var ns = new CString(0)
                init(ns, 0, 0)
        end
 
        # Init a `Bytes` with capacity `cap`
        init with_capacity(cap: Int) do
-               var ns = new NativeString(cap)
+               var ns = new CString(cap)
                init(ns, 0, cap)
        end
 
@@ -262,7 +262,7 @@ class Bytes
        # ~~~
        fun hexdigest: String do
                var elen = length * 2
-               var ns = new NativeString(elen)
+               var ns = new CString(elen)
                var i = 0
                var oi = 0
                while i < length do
@@ -285,7 +285,7 @@ class Bytes
        # ~~~
        fun chexdigest: String do
                var elen = length * 4
-               var ns = new NativeString(elen)
+               var ns = new CString(elen)
                var i = 0
                var oi = 0
                while i < length do
@@ -298,6 +298,75 @@ class Bytes
                return new FlatString.full(ns, elen, 0, elen)
        end
 
+
+       # Returns self as a stream of bits (0 and 1)
+       #
+       # ~~~
+       # var b = "abcd".to_bytes
+       # assert b.binarydigest == "01100001011000100110001101100100"
+       # assert b.binarydigest.binarydigest_to_bytes == b
+       # ~~~
+       fun binarydigest: String do
+               var elen = length * 8
+               var ns = new CString(elen)
+               var i = 0
+               var oi = 0
+               while i < length do
+                       var c = self[i]
+                       var b = 128u8
+                       while b > 0u8 do
+                               if c & b == 0u8 then
+                                       ns[oi] = 0x30u8 # b'0'
+                               else
+                                       ns[oi] = 0x31u8 # b'1'
+                               end
+                               oi += 1
+                               b = b >> 1
+                       end
+                       i += 1
+               end
+               return new FlatString.full(ns, elen, 0, elen)
+       end
+
+       # Interprets `self` as a big-endian positive integer.
+       #
+       # ~~~
+       # var b = "0102".hexdigest_to_bytes
+       # assert b.to_i == 258
+       # ~~~
+       #
+       # Nul bytes on the left are trimmed.
+       # 0 is returned for an empty Bytes object.
+       #
+       # ~~~
+       # assert "01".hexdigest_to_bytes.to_i == 1
+       # assert "0001".hexdigest_to_bytes.to_i == 1
+       #
+       # assert "0000".hexdigest_to_bytes.to_i == 0
+       # assert "00".hexdigest_to_bytes.to_i == 0
+       # assert "".hexdigest_to_bytes.to_i == 0
+       # ~~~
+       #
+       # `Int::to_bytes` is a loosely reverse method.
+       #
+       # ~~~
+       # assert b.to_i.to_bytes == b
+       # assert (b.to_i + 1).to_bytes.hexdigest == "0103"
+       # assert "0001".hexdigest_to_bytes.to_i.to_bytes.hexdigest == "01"
+       # ~~~
+       #
+       # Warning: `Int` might overflow for bytes with more than 60 bits.
+       fun to_i: Int do
+               var res = 0
+               var i = 0
+               while i < length do
+                       res *= 256
+                       res += self[i].to_i
+                       i += 1
+               end
+               return res
+       end
+
        #     var b = new Bytes.with_capacity(1)
        #     b[0] = 101u8
        #     assert b.to_s == "e"
@@ -361,13 +430,13 @@ class Bytes
 
        # Regenerates the buffer, necessary when it was persisted
        private fun regen do
-               var nns = new NativeString(capacity)
+               var nns = new CString(capacity)
                items.copy_to(nns, length, 0, 0)
                persisted = false
        end
 
        # Appends the `ln` first bytes of `ns` to self
-       fun append_ns(ns: NativeString, ln: Int) do
+       fun append_ns(ns: CString, ln: Int) do
                if persisted then regen
                var nlen = length + ln
                if nlen > capacity then enlarge(nlen)
@@ -376,7 +445,7 @@ class Bytes
        end
 
        # Appends `ln` bytes from `ns` starting at index `from` to self
-       fun append_ns_from(ns: NativeString, ln, from: Int) do
+       fun append_ns_from(ns: CString, ln, from: Int) do
                if persisted then regen
                var nlen = length + ln
                if nlen > capacity then enlarge(nlen)
@@ -387,7 +456,7 @@ class Bytes
        # Appends the bytes of `s` to `selftextextt`
        fun append_text(s: Text) do
                for i in s.substrings do
-                       append_ns(i.fast_cstring, i.bytelen)
+                       append_ns(i.fast_cstring, i.byte_length)
                end
        end
 
@@ -397,7 +466,7 @@ class Bytes
                if capacity >= sz then return
                persisted = false
                while capacity < sz do capacity = capacity * 2 + 2
-               var ns = new NativeString(capacity)
+               var ns = new CString(capacity)
                items.copy_to(ns, length, 0, 0)
                items = ns
        end
@@ -568,7 +637,7 @@ end
 private class BytesIterator
        super IndexedIterator[Byte]
 
-       var tgt: NativeString
+       var tgt: CString
 
        redef var index
 
@@ -583,6 +652,58 @@ private class BytesIterator
        redef fun item do return tgt[index]
 end
 
+redef class Int
+       # A big-endian representation of self.
+       #
+       # ~~~
+       # assert     1.to_bytes.hexdigest ==     "01"
+       # assert   255.to_bytes.hexdigest ==     "FF"
+       # assert   256.to_bytes.hexdigest ==   "0100"
+       # assert 65535.to_bytes.hexdigest ==   "FFFF"
+       # assert 65536.to_bytes.hexdigest == "010000"
+       # ~~~
+       #
+       # For 0, a Bytes object with single nul byte is returned (instead of an empty Bytes object).
+       #
+       # ~~~
+       # assert 0.to_bytes.hexdigest == "00"
+       # ~~~
+       #
+       # `Bytes::to_i` can be used to do the reverse operation.
+       #
+       # ~~~
+       # assert 1234.to_bytes.to_i == 1234
+       # ~~~
+       #
+       # Require self >= 0
+       fun to_bytes: Bytes do
+               if self == 0 then return "\0".to_bytes
+               assert self > 0
+
+               # Compute the len (log256)
+               var len = 1
+               var max = 256
+               while self >= max do
+                       len += 1
+                       max *= 256
+               end
+
+               # Allocate the buffer
+               var res = new Bytes.with_capacity(len)
+               for i in [0..len[ do res[i] = 0u8
+
+               # Fill it starting with the end
+               var i = len
+               var sum = self
+               while i > 0 do
+                       i -= 1
+                       res[i] = (sum % 256).to_b
+                       sum /= 256
+               end
+               return res
+       end
+end
+
 redef class Text
        # Returns a mutable copy of `self`'s bytes
        #
@@ -591,7 +712,7 @@ redef class Text
        # assert "String".to_bytes == [83u8, 116u8, 114u8, 105u8, 110u8, 103u8]
        # ~~~
        fun to_bytes: Bytes do
-               var b = new Bytes.with_capacity(bytelen)
+               var b = new Bytes.with_capacity(byte_length)
                append_to_bytes b
                return b
        end
@@ -609,7 +730,7 @@ redef class Text
        fun append_to_bytes(b: Bytes) do
                for s in substrings do
                        var from = if s isa FlatString then s.first_byte else 0
-                       b.append_ns_from(s.items, s.bytelen, from)
+                       b.append_ns_from(s.items, s.byte_length, from)
                end
        end
 
@@ -618,16 +739,54 @@ redef class Text
        #     assert "0B1F4D".hexdigest_to_bytes == [0x0Bu8, 0x1Fu8, 0x4Du8]
        #     assert "0B1F4D".hexdigest_to_bytes.hexdigest == "0B1F4D"
        #
-       # REQUIRE: `self` is a valid hexdigest and hexdigest.length % 2 == 0
+       # Characters that are not hexadecimal digits are ignored.
+       #
+       #     assert "z0B1 F4\nD".hexdigest_to_bytes.hexdigest == "0B1F4D"
+       #     assert "\\x0b1 \\xf4d".hexdigest_to_bytes.hexdigest == "0B1F4D"
+       #
+       # When the number of hexadecimal digit is not even, then a leading 0 is
+       # implicitly considered to fill the left byte (the most significant one).
+       #
+       #     assert "1".hexdigest_to_bytes.hexdigest == "01"
+       #     assert "FFF".hexdigest_to_bytes.hexdigest == "0FFF"
+       #
+       # `Bytes::hexdigest` is a loosely reverse method since its
+       # results contain only pairs of uppercase hexadecimal digits.
+       #
+       #     assert "ABCD".hexdigest_to_bytes.hexdigest == "ABCD"
+       #     assert "a b c".hexdigest_to_bytes.hexdigest == "0ABC"
        fun hexdigest_to_bytes: Bytes do
                var b = bytes
+               var max = byte_length
+
+               var dlength = 0 # Number of hex digits
                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
+                       var c = b[pos]
+                       if c.is_valid_hexdigit then dlength += 1
+                       pos += 1
+               end
+
+               # Allocate the result buffer
+               var ret = new Bytes.with_capacity((dlength+1) / 2)
+
+               var i = (dlength+1) % 2 # current hex digit (1=high, 0=low)
+               var byte = 0u8 # current accumulated byte value
+
+               pos = 0
+               while pos < max do
+                       var c = b[pos]
+                       if c.is_valid_hexdigit then
+                               byte = byte << 4 | c.hexdigit_to_byteval
+                               i -= 1
+                               if i < 0 then
+                                       # Last digit known: store and restart
+                                       ret.add byte
+                                       i = 1
+                                       byte = 0u8
+                               end
+                       end
+                       pos += 1
                end
                return ret
        end
@@ -636,8 +795,8 @@ redef class Text
        #
        #     assert "&lt;STRING&#47;&rt;".hexdigest == "266C743B535452494E47262334373B2672743B"
        fun hexdigest: String do
-               var ln = bytelen
-               var outns = new NativeString(ln * 2)
+               var ln = byte_length
+               var outns = new CString(ln * 2)
                var oi = 0
                for i in [0 .. ln[ do
                        bytes[i].add_digest_at(outns, oi)
@@ -656,11 +815,11 @@ redef class Text
        #     assert "\\x41\\x42\\x43".unescape_to_bytes.chexdigest == "\\x41\\x42\\x43"
        #     assert "B\\n\\x41\\u0103D3".unescape_to_bytes.chexdigest == "\\x42\\x0A\\x41\\xF0\\x90\\x8F\\x93"
        fun unescape_to_bytes: Bytes do
-               var res = new Bytes.with_capacity(self.bytelen)
+               var res = new Bytes.with_capacity(self.byte_length)
                var was_slash = false
                var i = 0
                while i < length do
-                       var c = chars[i]
+                       var c = self[i]
                        if not was_slash then
                                if c == '\\' then
                                        was_slash = true
@@ -702,16 +861,80 @@ redef class Text
                end
                return res
        end
+
+       # Return a `Bytes` by reading 0 and 1.
+       #
+       #     assert "1010101100001101".binarydigest_to_bytes.hexdigest == "AB0D"
+       #
+       # Note that characters that are neither 0 or 1 are just ignored.
+       #
+       #     assert "a1B01 010\n1100あ001101".binarydigest_to_bytes.hexdigest == "AB0D"
+       #     assert "hello".binarydigest_to_bytes.is_empty
+       #
+       # When the number of bits is not divisible by 8, then leading 0 are
+       # implicitly considered to fill the left byte (the most significant one).
+       #
+       #     assert "1".binarydigest_to_bytes.hexdigest == "01"
+       #     assert "1111111".binarydigest_to_bytes.hexdigest == "7F"
+       #     assert "1000110100".binarydigest_to_bytes.hexdigest == "0234"
+       #
+       # `Bytes::binarydigest` is a loosely reverse method since its
+       # results contain only 1 and 0 by blocks of 8.
+       #
+       #     assert "1010101100001101".binarydigest_to_bytes.binarydigest == "1010101100001101"
+       #     assert "1".binarydigest_to_bytes.binarydigest == "00000001"
+       fun binarydigest_to_bytes: Bytes
+       do
+               var b = bytes
+               var max = byte_length
+
+               # Count bits
+               var bitlen = 0
+               var pos = 0
+               while pos < max do
+                       var c = b[pos]
+                       pos += 1
+                       if c == 0x30u8 or c == 0x31u8 then bitlen += 1 # b'0' or b'1'
+               end
+
+               # Allocate (and take care of the padding)
+               var ret = new Bytes.with_capacity((bitlen+7) / 8)
+
+               var i = (bitlen+7) % 8 # current bit (7th=128, 0th=1)
+               var byte = 0u8 # current accumulated byte value
+
+               pos = 0
+               while pos < max do
+                       var c = b[pos]
+                       pos += 1
+                       if c == 0x30u8 then # b'0'
+                               byte = byte << 1
+                       else if c == 0x31u8 then # b'1'
+                               byte = byte << 1 | 1u8
+                       else
+                               continue
+                       end
+
+                       i -= 1
+                       if i < 0 then
+                               # Last bit known: store and restart
+                               ret.add byte
+                               i = 7
+                               byte = 0u8
+                       end
+               end
+               return ret
+       end
 end
 
 redef class FlatText
        redef fun append_to_bytes(b) do
                var from = if self isa FlatString then first_byte else 0
-               b.append_ns_from(items, bytelen, from)
+               b.append_ns_from(items, byte_length, from)
        end
 end
 
-redef class NativeString
+redef class CString
        # Creates a new `Bytes` object from `self` with `len` as length
        #
        # If `len` is null, strlen will determine the length of the Bytes
@@ -725,7 +948,7 @@ redef class NativeString
        # If `len` is null, strlen will determine the length of the Bytes
        fun to_bytes_with_copy(len: nullable Int): Bytes do
                if len == null then len = cstring_length
-               var nns = new NativeString(len)
+               var nns = new CString(len)
                copy_to(nns, len, 0, 0)
                return new Bytes(nns, len, len)
        end