core :: ASCIIFlatString :: defaultinit
# Special cases of String where all the characters are ASCII-based
#
# Optimizes access operations to O(1) complexity.
private class ASCIIFlatString
super FlatString
init full_data(items: CString, byte_length, from, length: Int) do
self._items = items
self._length = length
self._byte_length = byte_length
_first_byte = from
_bytepos = from
end
redef fun [](idx) do
assert idx < _byte_length and idx >= 0
return _items[idx + _first_byte].code_point
end
redef fun substring(from, count) do
var ln = _length
if count <= 0 then return ""
if (count + from) > ln then count = ln - from
if count <= 0 then return ""
if from < 0 then
count += from
if count <= 0 then return ""
from = 0
end
return new ASCIIFlatString.full_data(_items, count, from + _first_byte, count)
end
redef fun reversed do
var b = new FlatBuffer.with_capacity(_byte_length + 1)
var i = _length - 1
while i >= 0 do
b.add self[i]
i -= 1
end
var s = b.to_s.as(FlatString)
return s
end
redef fun char_to_byte_index(index) do return index + _first_byte
redef fun substring_impl(from, count, end_index) do
return new ASCIIFlatString.full_data(_items, count, from + _first_byte, count)
end
redef fun fetch_char_at(i) do return _items[i + _first_byte].code_point
end
lib/core/text/flat.nit:683,1--733,3