index
in the string.assert "abcd"[2] == 'c'
redef fun [](index) do
var len = _length
# Statistically:
# * ~70% want the next char
# * ~23% want the previous
# * ~7% want the same char
#
# So it makes sense to shortcut early. And early is here.
var dpos = index - _position
var b = _bytepos
if dpos == 1 and index < len - 1 then
var its = _items
var c = its[b]
if c & 0x80 == 0x00 then
# We want the next, and current is easy.
# So next is easy to find!
b += 1
_position = index
_bytepos = b
# The rest will be done by `dpos==0` bellow.
dpos = 0
end
else if dpos == -1 and index > 1 then
var its = _items
var c = its[b-1]
if c & 0x80 == 0x00 then
# We want the previous, and it is easy.
b -= 1
dpos = 0
_position = index
_bytepos = b
return c.code_point
end
end
if dpos == 0 then
# We know what we want (+0 or +1) just get it now!
var its = _items
var c = its[b]
if c & 0x80 == 0x00 then return c.code_point
return items.char_at(b)
end
assert index >= 0 and index < len
return fetch_char_at(index)
end
lib/core/text/flat.nit:328,2--373,4
redef fun [](i) do
sys.index_call.inc "FlatBuffer"
return super
end
lib/text_stat/text_stat.nit:251,2--254,4
redef fun [](i) do
assert i >= 0 and i < _length
var flps = _flat_last_pos_start
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)
end
lib/core/text/ropes.nit:132,2--141,4
redef fun [](i) do
sys.index_call.inc "Concat"
if flat_last_pos_start != -1 then
var fsp = i - flat_last_pos_start
if fsp >= 0 and fsp < flat_cache.length then return flat_cache[fsp]
end
sys.concat_cache_miss += 1
var s: String = self
var st = i
loop
if s isa FlatString then break
s = s.as(Concat)
var lft = s.left
var llen = lft.length
if i >= llen then
s = s.right
i -= llen
else
s = s.left
end
end
flat_last_pos_start = st - i
flat_cache = s
return s[i]
end
lib/text_stat/text_stat.nit:143,2--167,4
redef fun [](idx) do
assert idx < _byte_length and idx >= 0
return _items[idx + _first_byte].code_point
end
lib/core/text/flat.nit:697,2--700,4