- # Is this string suffixed by 'suffix'
- #
- # "abcd".has_suffix("abc") # --> false
- # "abcd".has_suffix("bcd") # --> true
- meth has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
+# Strings are arrays of characters.
+class Buffer
+special AbstractString
+special Comparable
+special StringCapable
+special AbstractArray[Char]
+
+ redef type OTHER: String
+
+ redef meth []=(index, item)
+ do
+ if index == length then
+ add(item)
+ return
+ end
+ assert index >= 0 and index < length
+ _items[index] = item
+ end
+
+ redef meth add(c)
+ do
+ if _capacity <= length then enlarge(length + 5)
+ _items[length] = c
+ _length += 1
+ end
+
+ redef meth enlarge(cap)
+ do
+ 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)
+ _items = a
+ _capacity = c
+ end
+
+ redef meth append(s)
+ do
+ if s isa String then
+ var sl = s.length
+ if _capacity < length + sl then enlarge(length + sl)
+ s.items.copy_to(_items, sl, 0, length)
+ _length += sl
+ else
+ super
+ end
+ end
+
+ redef meth to_s: String
+ do
+ var l = length
+ var a = calloc_string(l+1)
+ _items.copy_to(a, l, 0, 0)
+
+ # Ensure the afterlast byte is '\0' to nul-terminated char *
+ a[length] = '\0'
+
+ return new String.with_native(a, length)
+ end