redef type SELFTYPE: Buffer is fixed
- # Specific implementations MUST set this to `true` in order to invalidate caches
- protected var is_dirty = true
-
# Copy-On-Write flag
#
# If the `Buffer` was to_s'd, the next in-place altering
end
end
- redef fun hash
- do
- if is_dirty then hash_cache = null
- return super
- end
-
# In Buffers, the internal sequence of character is mutable
# Thus, `chars` can be used to modify the buffer.
redef fun chars: Sequence[Char] is abstract
# Index at which `self` begins in `_items`, inclusively
redef var first_byte is noinit
- redef var chars = new FlatStringCharView(self) is lazy
+ redef fun chars do return new FlatStringCharView(self)
- redef var bytes = new FlatStringByteView(self) is lazy
+ redef fun bytes do return new FlatStringByteView(self)
redef fun to_cstring do
var blen = _byte_length
super FlatText
super Buffer
- redef var chars: Sequence[Char] = new FlatBufferCharView(self) is lazy
+ redef fun chars do return new FlatBufferCharView(self)
- redef var bytes = new FlatBufferByteView(self) is lazy
-
- private var char_cache: Int = -1
-
- private var byte_cache: Int = -1
+ redef fun bytes do return new FlatBufferByteView(self)
private var capacity = 0
do
assert index >= 0 and index <= _length
if written then reset
- is_dirty = true
if index == _length then
add item
return
redef fun add(c)
do
if written then reset
- is_dirty = true
var clen = c.u8char_len
var bt = _byte_length
enlarge(bt + clen)
end
redef fun clear do
- is_dirty = true
_byte_length = 0
_length = 0
if written then
redef fun append(s)
do
if s.is_empty then return
- is_dirty = true
var sl = s.byte_length
var nln = _byte_length + sl
enlarge(nln)
super Rope
super String
- redef var chars is lazy do return new RopeChars(self)
+ redef fun chars do return new RopeChars(self)
- redef var bytes is lazy do return new RopeBytes(self)
+ redef fun bytes do return new RopeBytes(self)
redef var length is noinit
var flat_cache: FlatString is noinit
# Position of the beginning of `flat_cache` in `self`
- var flat_last_pos_start: Int = -1
+ var flat_last_pos_start: Int is noinit
- var flat_last_pos_end: Int = -1
-
- redef var to_cstring is lazy do
+ redef fun to_cstring do
var len = _byte_length
var ns = new NativeString(len + 1)
ns[len] = 0u8
init do
var l = _left
var r = _right
- length = l.length + r.length
+ _length = l.length + r.length
_byte_length = l.byte_length + r.byte_length
+ _flat_last_pos_start = _length
end
redef fun is_empty do return _byte_length == 0
end
redef fun [](i) do
- assert i >= 0 and i <= _length
+ assert i >= 0 and i < _length
var flps = _flat_last_pos_start
- if flps != -1 and i >= flps and i <= _flat_last_pos_end then
- return _flat_cache.fetch_char_at(i - flps)
+ if i >= flps then
+ var fc = _flat_cache
+ if i < flps + fc._length then return fc.fetch_char_at(i - flps)
end
var lf = get_leaf_at(i)
return lf.fetch_char_at(i - _flat_last_pos_start)
fun get_leaf_at(pos: Int): FlatString do
var flps = _flat_last_pos_start
- if flps != -1 and pos >= flps and pos <= _flat_last_pos_end then
- return _flat_cache
+ if pos >= flps then
+ var fc = _flat_cache
+ if pos < flps + fc._length then return fc
end
var s: String = self
var st = pos
end
end
_flat_last_pos_start = st - pos
- _flat_last_pos_end = st - pos + s.length - 1
_flat_cache = s
return s
end
var end_index = from + count - 1
var flps = _flat_last_pos_start
- if flps != -1 and from >= flps and end_index <= _flat_last_pos_end then
- return _flat_cache.substring_impl(from - flps, count, end_index - flps)
+ if from >= flps then
+ var fc = _flat_cache
+ if end_index < flps + fc._length then
+ return fc.substring_impl(from - flps, count, end_index - flps)
+ end
end
var lft = _left
super Rope
super Buffer
- redef var chars: Sequence[Char] is lazy do return new RopeBufferChars(self)
+ redef fun chars do return new RopeBufferChars(self)
- redef var bytes is lazy do return new RopeBufferBytes(self)
+ redef fun bytes do return new RopeBufferBytes(self)
# The final string being built on the fly
private var str: String = ""