assert "abcd".substring(1, 2) == "bc"
assert "abcd".substring(-1, 2) == "a"
assert "abcd".substring(1, 0) == ""
assert "abcd".substring(2, 5) == "cd"
assert "あいうえお".substring(1,3) == "いうえ"
A from
index < 0 will be replaced by 0.
Unless a count
value is > 0 at the same time.
In this case, from += count
and count -= from
.
# Create a substring.
#
# ~~~
# assert "abcd".substring(1, 2) == "bc"
# assert "abcd".substring(-1, 2) == "a"
# assert "abcd".substring(1, 0) == ""
# assert "abcd".substring(2, 5) == "cd"
# assert "あいうえお".substring(1,3) == "いうえ"
# ~~~
#
# A `from` index < 0 will be replaced by 0.
# Unless a `count` value is > 0 at the same time.
# In this case, `from += count` and `count -= from`.
fun substring(from: Int, count: Int): SELFTYPE is abstract
lib/core/text/abstract_text.nit:66,2--79,59
redef fun substring(from, count) do
if from < 0 then
count += from
if count < 0 then return ""
from = 0
end
var ln = _length
if (count + from) > ln then count = ln - from
if count <= 0 then return ""
var end_index = from + count - 1
var flps = _flat_last_pos_start
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
var llen = lft.length
if from < llen then
if from + count < llen then return lft.substring(from, count)
var lsublen = llen - from
return lft.substring_from(from) + _right.substring(0, count - lsublen)
else
return _right.substring(from - llen, count)
end
end
lib/core/text/ropes.nit:168,2--197,4
redef fun substring(from, count)
do
if count <= 0 then return ""
if from < 0 then
count += from
if count <= 0 then return ""
from = 0
end
var ln = _length
if (count + from) > ln then count = ln - from
if count <= 0 then return ""
var end_index = from + count - 1
return substring_impl(from, count, end_index)
end
lib/core/text/flat.nit:442,2--457,4
redef fun substring(from, count)
do
assert count >= 0
if from < 0 then from = 0
if (from + count) > _length then count = _length - from
if count <= 0 then return new Buffer
var its = _items
var bytefrom = its.char_to_byte_index(from)
var byteto = its.char_to_byte_index(count + from - 1)
byteto += its.char_at(byteto).u8char_len - 1
var byte_length = byteto - bytefrom + 1
var r_items = new CString(byte_length)
its.copy_to(r_items, byte_length, bytefrom, 0)
return new FlatBuffer.with_infos(r_items, byte_length, byte_length, count)
end
lib/core/text/flat.nit:1090,2--1104,4
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
lib/core/text/flat.nit:702,2--713,4