lib/core: Removed `last_byte` attribute in `FlatString` as it is useless
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 8 Dec 2015 15:09:29 +0000 (10:09 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 28 Dec 2015 18:19:52 +0000 (13:19 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/bytes.nit
lib/core/file.nit
lib/core/stream.nit
lib/core/text/flat.nit
lib/core/text/ropes.nit

index af5b61b..1e081b8 100644 (file)
@@ -262,7 +262,7 @@ class Bytes
                        i += 1
                        oi += 2
                end
-               return new FlatString.full(ns, elen, 0, elen - 1, elen)
+               return new FlatString.full(ns, elen, 0, elen)
        end
 
        #     var b = new Bytes.with_capacity(1)
@@ -609,7 +609,7 @@ redef class Text
                        bytes[i].add_digest_at(outns, oi)
                        oi += 2
                end
-               return new FlatString.with_infos(outns, ln * 2, 0, ln * 2 - 1)
+               return new FlatString.with_infos(outns, ln * 2, 0)
        end
 
        # Return a `Bytes` instance where Nit escape sequences are transformed.
index fd848e1..f54901e 100644 (file)
@@ -1271,7 +1271,7 @@ redef class FlatString
 
        redef fun file_extension do
                var its = _items
-               var p = _last_byte
+               var p = last_byte
                var c = its[p]
                var st = _first_byte
                while p >= st and c != '.'.ascii do
@@ -1279,12 +1279,12 @@ redef class FlatString
                        c = its[p]
                end
                if p <= st then return null
-               var ls = _last_byte
-               return new FlatString.with_infos(its, ls - p, p + 1, ls)
+               var ls = last_byte
+               return new FlatString.with_infos(its, ls - p, p + 1)
        end
 
        redef fun basename(extension) do
-               var l = _last_byte
+               var l = last_byte
                var its = _items
                var min = _first_byte
                var sl = '/'.ascii
@@ -1292,7 +1292,7 @@ redef class FlatString
                if l == min then return "/"
                var ns = l
                while ns >= min and its[ns] != sl do ns -= 1
-               var bname = new FlatString.with_infos(its, l - ns, ns + 1, l)
+               var bname = new FlatString.with_infos(its, l - ns, ns + 1)
 
                return if extension != null then bname.strip_extension(extension) else bname
        end
index e8cb232..19ed842 100644 (file)
@@ -207,12 +207,12 @@ abstract class Reader
                        # if this is the best size or not
                        var chunksz = 129
                        if chunksz > remsp then
-                               rets += new FlatString.with_infos(sits, remsp, pos, pos + remsp - 1)
+                               rets += new FlatString.with_infos(sits, remsp, pos)
                                break
                        end
                        var st = sits.find_beginning_of_char_at(pos + chunksz - 1)
                        var bytelen = st - pos
-                       rets += new FlatString.with_infos(sits, bytelen, pos, st - 1)
+                       rets += new FlatString.with_infos(sits, bytelen, pos)
                        pos = st
                        remsp -= bytelen
                end
index b2336e6..e0c31f3 100644 (file)
@@ -40,7 +40,7 @@ redef class FlatText
        protected fun first_byte: Int do return 0
 
        # Last byte of the NativeString
-       protected fun last_byte: Int do return _bytelen - 1
+       protected fun last_byte: Int do return first_byte + _bytelen - 1
 
        # Cache of the latest position (char) explored in the string
        var position: Int = 0
@@ -181,7 +181,7 @@ redef class FlatText
                        end
                        pos += 1
                end
-               var s = new FlatString.with_infos(nits, nlen, 0, nlen - 1)
+               var s = new FlatString.with_infos(nits, nlen, 0)
                return s
        end
 
@@ -305,9 +305,6 @@ class FlatString
        # Index at which `self` begins in `_items`, inclusively
        redef var first_byte is noinit
 
-       # Index at which `self` ends in `_items`, inclusively
-       redef var last_byte is noinit
-
        redef var chars = new FlatStringCharView(self) is lazy
 
        redef var bytes = new FlatStringByteView(self) is lazy
@@ -406,12 +403,11 @@ class FlatString
        #
        # `_items` will be used as is, without copy, to retrieve the characters of the string.
        # Aliasing issues is the responsibility of the caller.
-       private init with_infos(items: NativeString, bytelen, from, to: Int)
+       private init with_infos(items: NativeString, bytelen, from: Int)
        do
                self._items = items
                self._bytelen = bytelen
                _first_byte = from
-               _last_byte = to
                _bytepos = from
        end
 
@@ -419,13 +415,12 @@ class FlatString
        #
        # `_items` will be used as is, without copy, to retrieve the characters of the string.
        # Aliasing issues is the responsibility of the caller.
-       private init full(items: NativeString, bytelen, from, to, length: Int)
+       private init full(items: NativeString, bytelen, from, length: Int)
        do
                self._items = items
                self.length = length
                self._bytelen = bytelen
                _first_byte = from
-               _last_byte = to
                _bytepos = from
        end
 
@@ -499,7 +494,7 @@ class FlatString
                        var ns = new NativeString(nlen + 1)
                        mits.copy_to(ns, mlen, mifrom, 0)
                        sits.copy_to(ns, slen, sifrom, mlen)
-                       return new FlatString.full(ns, nlen, 0, nlen - 1, length + o.length)
+                       return new FlatString.full(ns, nlen, 0, _length + o.length)
                else
                        abort
                end
@@ -520,7 +515,7 @@ class FlatString
                        offset += mybtlen
                        i -= 1
                end
-               return new FlatString.full(ns, new_bytelen, 0, new_bytelen - 1, newlen)
+               return new FlatString.full(ns, new_bytelen, 0, newlen)
        end
 
 
@@ -532,7 +527,7 @@ class FlatString
                        var i = _first_byte
 
                        var my_items = _items
-                       var max = _last_byte
+                       var max = last_byte
 
                        while i <= max do
                                h = (h << 5) + h + my_items[i].to_i
@@ -641,7 +636,7 @@ private class FlatStringByteIterator
                curr_pos += tgt._first_byte
        end
 
-       redef fun is_ok do return curr_pos <= target._last_byte
+       redef fun is_ok do return curr_pos <= target.last_byte
 
        redef fun item do return target_items[curr_pos]
 
@@ -658,12 +653,12 @@ private class FlatStringByteView
 
        redef fun [](index)
        do
-               # Check that the index (+ _first_byte) is not larger than _last_byte
+               # Check that the index (+ _first_byte) is not larger than last_byte
                # In other terms, if the index is valid
                assert index >= 0
                var target = self.target
                var ind = index + target._first_byte
-               assert ind <= target._last_byte
+               assert ind <= target.last_byte
                return target._items[ind]
        end
 
@@ -806,7 +801,7 @@ class FlatBuffer
                written = true
                var bln = _bytelen
                if bln == 0 then _items = new NativeString(1)
-               return new FlatString.full(_items, bln, 0, bln - 1, length)
+               return new FlatString.full(_items, bln, 0, _length)
        end
 
        redef fun to_cstring
@@ -918,7 +913,7 @@ class FlatBuffer
        redef fun times(repeats)
        do
                var bln = _bytelen
-               var x = new FlatString.full(_items, bln, 0, bln - 1, length)
+               var x = new FlatString.full(_items, bln, 0, _length)
                for i in [1 .. repeats[ do
                        append(x)
                end
@@ -1089,7 +1084,7 @@ redef class NativeString
        end
 
        redef fun to_s_full(bytelen, unilen) do
-               return new FlatString.full(self, bytelen, 0, bytelen - 1, unilen)
+               return new FlatString.full(self, bytelen, 0, unilen)
        end
 
        # Returns `self` as a new String.
@@ -1100,7 +1095,7 @@ redef class NativeString
                if r.items != self then return r
                var new_self = new NativeString(length + 1)
                copy_to(new_self, length, 0, 0)
-               var str = new FlatString.with_infos(new_self, length, 0, length - 1)
+               var str = new FlatString.with_infos(new_self, length, 0)
                new_self[length] = 0u8
                str.to_cstring = new_self
                return str
@@ -1178,7 +1173,7 @@ redef class NativeString
                        end
                        copy_to(ret, len - old_repl, old_repl, off)
                end
-               return new FlatString.full(ret, end_length, 0, end_length - 1, chr_ln)
+               return new FlatString.full(ret, end_length, 0, chr_ln)
        end
 
        # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
@@ -1236,7 +1231,7 @@ redef class Int
                var ns = new NativeString(nslen + 1)
                ns[nslen] = 0u8
                native_int_to_s(ns, nslen + 1)
-               return new FlatString.full(ns, nslen, 0, nslen - 1, nslen)
+               return new FlatString.full(ns, nslen, 0, nslen)
        end
 end
 
@@ -1286,7 +1281,7 @@ redef class Array[E]
                        end
                        i += 1
                end
-               return new FlatString.with_infos(ns, sl, 0, sl - 1)
+               return new FlatString.with_infos(ns, sl, 0)
        end
 end
 
@@ -1323,7 +1318,7 @@ redef class NativeArray[E]
                        end
                        i += 1
                end
-               return new FlatString.with_infos(ns, sl, 0, sl - 1)
+               return new FlatString.with_infos(ns, sl, 0)
        end
 end
 
index 3b4de21..64a424d 100644 (file)
@@ -154,11 +154,11 @@ private class Concat
                var lft = _left
                var llen = lft.length
                if from < llen then
-                       if from + len < llen then return lft.substring(from,len)
+                       if from + count < llen then return lft.substring(from, count)
                        var lsublen = llen - from
-                       return lft.substring_from(from) + _right.substring(0, len - lsublen)
+                       return lft.substring_from(from) + _right.substring(0, count - lsublen)
                else
-                       return _right.substring(from - llen, len)
+                       return _right.substring(from - llen, count)
                end
        end
 
@@ -479,7 +479,7 @@ class RopeBuffer
        # the final String and re-allocates a new larger Buffer.
        private fun dump_buffer do
                written = false
-               var nstr = new FlatString.with_infos(ns, rpos - dumped, dumped, rpos - 1)
+               var nstr = new FlatString.with_infos(ns, rpos - dumped, dumped)
                str += nstr
                var bs = buf_size
                bs = bs * 2
@@ -492,14 +492,14 @@ class RopeBuffer
        # Similar to dump_buffer, but does not reallocate a new NativeString
        private fun persist_buffer do
                if rpos == dumped then return
-               var nstr = new FlatString.with_infos(ns, rpos - dumped, dumped, rpos - 1)
+               var nstr = new FlatString.with_infos(ns, rpos - dumped, dumped)
                str += nstr
                dumped = rpos
        end
 
        redef fun output do
                str.output
-               new FlatString.with_infos(ns, rpos - dumped, dumped, rpos - 1).output
+               new FlatString.with_infos(ns, rpos - dumped, dumped).output
        end
 
        # Enlarge is useless here since the `Buffer`
@@ -521,7 +521,7 @@ class RopeBuffer
        redef fun reverse do
                # Flush the buffer in order to only have to reverse `str`.
                if rpos > 0 and dumped != rpos then
-                       str += new FlatString.with_infos(ns, rpos - dumped, dumped, rpos - 1)
+                       str += new FlatString.with_infos(ns, rpos - dumped, dumped)
                        dumped = rpos
                end
                str = str.reversed
@@ -564,7 +564,7 @@ redef class FlatString
                        var ns = new NativeString(nlen + 1)
                        mits.copy_to(ns, mlen, mifrom, 0)
                        sits.copy_to(ns, slen, sifrom, mlen)
-                       return new FlatString.full(ns, nlen, 0, nlen - 1, length + s.length)
+                       return new FlatString.full(ns, nlen, 0, length + s.length)
                else if s isa Concat then
                        var sl = s._left
                        var sllen = sl.bytelen
@@ -625,7 +625,7 @@ private class RopeByteReverseIterator
                if not subs.is_ok then return
                var s = subs.item
                ns = s._items
-               pns = s._last_byte
+               pns = s.last_byte
        end
 end
 
@@ -834,7 +834,7 @@ private class RopeBufSubstringIterator
 
        init from(str: RopeBuffer) do
                iter = str.str.substrings
-               nsstr = new FlatString.with_infos(str.ns, str.rpos - str.dumped, str.dumped, str.rpos - 1)
+               nsstr = new FlatString.with_infos(str.ns, str.rpos - str.dumped, str.dumped)
                if str.length == 0 then nsstr_done = true
        end