# In this case, `from += count` and `count -= from`.
fun substring(from: Int, count: Int): SELFTYPE is abstract
+ # Iterates on the substrings of self if any
+ fun substrings: Iterator[Text] is abstract
+
# Concatenates `o` to `self`
#
# assert "hello" + "world" == "helloworld"
# Real items, used as cache for to_cstring is called
private var real_items: nullable NativeString = null
- redef var length: Int
+ redef var length: Int = 0
init do end
redef fun to_s do return self
+ fun append(s: String): SELFTYPE is abstract
+
+ fun prepend(s: String): SELFTYPE is abstract
+
+ fun insert_at(s: String, pos: Int): SELFTYPE is abstract
+end
+
+private class FlatSubstringsIter
+ super Iterator[FlatText]
+
+ var tgt: nullable FlatText
+
+ init(tgt: FlatText) do self.tgt = tgt
+
+ redef fun item do
+ assert is_ok
+ return tgt.as(not null)
+ end
+
+ redef fun is_ok do return tgt != null
+
+ redef fun next do tgt = null
end
# Immutable strings of characters.
super FlatText
super String
- redef type SELFTYPE: FlatString
-
# Index in _items of the start of the string
private var index_from: Int
return hash_cache.as(not null)
end
+
+ redef fun substrings do return new FlatSubstringsIter(self)
end
private class FlatStringReverseIterator
redef var chars: Sequence[Char] = new FlatBufferCharView(self)
- private var capacity: Int
+ private var capacity: Int = 0
+
+ redef fun substrings do return new FlatSubstringsIter(self)
redef fun []=(index, item)
do
redef fun enlarge(cap)
do
- is_dirty = true
var c = capacity
if cap <= c then return
while c <= cap do c = c * 2 + 2
var a = calloc_string(c+1)
- items.copy_to(a, length, 0, 0)
+ if length > 0 then items.copy_to(a, length, 0, 0)
items = a
capacity = c
- items.copy_to(a, length, 0, 0)
end
redef fun to_s: String
if is_dirty then
var new_native = calloc_string(length + 1)
new_native[length] = '\0'
- items.copy_to(new_native, length, 0, 0)
+ if length > 0 then items.copy_to(new_native, length, 0, 0)
real_items = new_native
is_dirty = false
end
end
# Create a new empty string.
- init do with_capacity(5)
+ init do end
init from(s: Text)
do
redef fun append(s)
do
+ if s.is_empty then return
is_dirty = true
var sl = s.length
if capacity < length + sl then enlarge(length + sl)
init with_pos(tgt: FlatBuffer, pos: Int)
do
target = tgt
- target_items = tgt.items
+ if tgt.length > 0 then target_items = tgt.items
curr_pos = pos
end
init with_pos(tgt: FlatBuffer, pos: Int)
do
target = tgt
- target_items = tgt.items
+ if tgt.length > 0 then target_items = tgt.items
curr_pos = pos
end