Merge: Rename the extern class `NativeString` to `CString`
[nit.git] / lib / core / bytes.nit
index 7b25d61..9bf70c0 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
@@ -308,7 +308,7 @@ class Bytes
        # ~~~
        fun binarydigest: String do
                var elen = length * 8
-               var ns = new NativeString(elen)
+               var ns = new CString(elen)
                var i = 0
                var oi = 0
                while i < length do
@@ -430,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)
@@ -445,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)
@@ -456,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
 
@@ -466,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
@@ -572,15 +572,16 @@ class Bytes
 
        # Decode `self` from percent (or URL) encoding to a clear string
        #
-       # Replace invalid use of '%' with '?'.
+       # Invalid '%' are not decoded.
        #
        #     assert "aBc09-._~".to_bytes.from_percent_encoding == "aBc09-._~".to_bytes
        #     assert "%25%28%29%3c%20%3e".to_bytes.from_percent_encoding == "%()< >".to_bytes
        #     assert ".com%2fpost%3fe%3dasdf%26f%3d123".to_bytes.from_percent_encoding == ".com/post?e=asdf&f=123".to_bytes
        #     assert "%25%28%29%3C%20%3E".to_bytes.from_percent_encoding == "%()< >".to_bytes
-       #     assert "incomplete %".to_bytes.from_percent_encoding == "incomplete ?".to_bytes
-       #     assert "invalid % usage".to_bytes.from_percent_encoding == "invalid ? usage".to_bytes
+       #     assert "incomplete %".to_bytes.from_percent_encoding == "incomplete %".to_bytes
+       #     assert "invalid % usage".to_bytes.from_percent_encoding == "invalid % usage".to_bytes
        #     assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".to_bytes.from_percent_encoding == "éあいう".to_bytes
+       #     assert "%1 %A %C3%A9A9".to_bytes.from_percent_encoding == "%1 %A éA9".to_bytes
        fun from_percent_encoding: Bytes do
                var tmp = new Bytes.with_capacity(length)
                var pos = 0
@@ -592,14 +593,14 @@ class Bytes
                                continue
                        end
                        if length - pos < 2 then
-                               tmp.add '?'.ascii
+                               tmp.add '%'.ascii
                                pos += 1
                                continue
                        end
                        var bn = self[pos + 1]
                        var bnn = self[pos + 2]
                        if not bn.is_valid_hexdigit or not bnn.is_valid_hexdigit then
-                               tmp.add '?'.ascii
+                               tmp.add '%'.ascii
                                pos += 1
                                continue
                        end
@@ -637,7 +638,7 @@ end
 private class BytesIterator
        super IndexedIterator[Byte]
 
-       var tgt: NativeString
+       var tgt: CString
 
        redef var index
 
@@ -712,7 +713,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
@@ -730,7 +731,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
 
@@ -757,7 +758,7 @@ redef class Text
        #     assert "a b c".hexdigest_to_bytes.hexdigest == "0ABC"
        fun hexdigest_to_bytes: Bytes do
                var b = bytes
-               var max = bytelen
+               var max = byte_length
 
                var dlength = 0 # Number of hex digits
                var pos = 0
@@ -795,8 +796,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)
@@ -815,11 +816,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
@@ -886,7 +887,7 @@ redef class Text
        fun binarydigest_to_bytes: Bytes
        do
                var b = bytes
-               var max = bytelen
+               var max = byte_length
 
                # Count bits
                var bitlen = 0
@@ -930,11 +931,11 @@ 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
@@ -948,7 +949,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