fun char_to_byte_index(index: Int): Int do
var dpos = index - _position
var b = _bytepos
+ var its = _items
- if dpos == 0 then return b
if dpos == 1 then
- b += _items.length_of_char_at(b)
+ if its[b] & 0x80u8 == 0x00u8 then
+ b += 1
+ else
+ b += its.length_of_char_at(b)
+ end
_bytepos = b
_position = index
return b
end
if dpos == -1 then
- b = _items.find_beginning_of_char_at(b - 1)
+ b = its.find_beginning_of_char_at(b - 1)
_bytepos = b
_position = index
return b
end
+ if dpos == 0 then return b
var ln = _length
var pos = _position
var delta_end = (ln - 1) - index
var delta_cache = (pos - index).abs
var min = delta_begin
- var its = _items
if delta_cache < min then min = delta_cache
if delta_end < min then min = delta_end
end
redef fun [](index) do
- assert index >= 0 and index < _length
+ 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 & 0x80u8 == 0x00u8 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 & 0x80u8 == 0x00u8 then
+ # We want the previous, and it is easy.
+ b -= 1
+ dpos = 0
+ _position = index
+ _bytepos = b
+ return c.ascii
+ 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 & 0x80u8 == 0x00u8 then return c.ascii
+ return items.char_at(b)
+ end
+
+ assert index >= 0 and index < len
return fetch_char_at(index)
end
return to_s_with_length(cstring_length)
end
- # Returns `self` as a String of `length`.
- redef fun to_s_with_length(length): FlatString
+ redef fun to_s_with_length(length)
do
assert length >= 0
return clean_utf8(length)
return new FlatString.with_infos(self, len, 0)
end
- # Returns `self` as a new String.
- redef fun to_s_with_copy: FlatString
+ redef fun to_s_with_copy do return to_s_with_copy_and_length(cstring_length)
+
+ # Get a `String` from `length` bytes at `self` copied into Nit memory
+ fun to_s_with_copy_and_length(length: Int): String
do
- var length = cstring_length
var r = clean_utf8(length)
if r.items != self then return r
var new_self = new NativeString(length + 1)
end
redef class Int
- redef fun to_base(base, signed)
- do
- var l = digit_count(base)
- var s = new FlatBuffer.from(" " * l)
- fill_buffer(s, base, signed)
- return s.to_s
- end
-
# return displayable int in base 10 and signed
#
# assert 1.to_s == "1"