# High-level abstraction for all text representations
abstract class Text
+ super Comparable
+
+ redef type OTHER: Text
# Gets a view on the chars of the Text object
fun chars: StringCharView is abstract
return res.to_s
end
+ redef fun ==(o)
+ do
+ if o == null then return false
+ if not o isa Text then return false
+ if self.is_same_instance(o) then return true
+ if self.length != o.length then return false
+ return self.chars == o.chars
+ end
+
+ redef fun <(o)
+ do
+ return self.chars < o.chars
+ end
+
end
# All kinds of array-based text representations.
# views on String and Buffer objects
abstract class StringCharView
super SequenceRead[Char]
+ super Comparable
type SELFTYPE: Text
+ redef type OTHER: StringCharView
+
private var target: SELFTYPE
private init(tgt: SELFTYPE)
return false
end
+ redef fun ==(other)
+ do
+ if other == null then return false
+ if not other isa StringCharView then return false
+ var other_chars = other.iterator
+ for i in self do
+ if i != other_chars.item then return false
+ other_chars.next
+ end
+ return true
+ end
+
+ redef fun <(other)
+ do
+ var self_chars = self.iterator
+ var other_chars = other.iterator
+
+ while self_chars.is_ok and other_chars.is_ok do
+ if self_chars.item < other_chars.item then return true
+ if self_chars.item > other_chars.item then return false
+ self_chars.next
+ other_chars.next
+ end
+
+ if self_chars.is_ok then
+ return false
+ else
+ return true
+ end
+ end
end
# View on Buffer objects, extends Sequence
# Immutable strings of characters.
class String
- super Comparable
super FlatText
super StringCapable
- redef type OTHER: String
-
# Index in _items of the start of the string
private var index_from: Int
redef fun ==(other)
do
- if not other isa String then return false
+ if not other isa String then return super
if self.object_id == other.object_id then return true
# assert ("aa" < "b") == true
redef fun <(other)
do
+ if not other isa String then return super
+
if self.object_id == other.object_id then return false
var my_curr_char : Char
# Mutable strings of characters.
class FlatBuffer
super FlatText
- super Comparable
super StringCapable
super Buffer
- redef type OTHER: String
-
redef var chars: FlatBufferCharView = new FlatBufferCharView(self)
var capacity: Int
return a.to_s_with_length(length)
end
- redef fun <(s)
- do
- var i = 0
- var l1 = length
- var l2 = s.length
- while i < l1 and i < l2 do
- var c1 = self.chars[i].ascii
- var c2 = s.chars[i].ascii
- if c1 < c2 then
- return true
- else if c2 < c1 then
- return false
- end
- i += 1
- end
- if l1 < l2 then
- return true
- else
- return false
- end
- end
-
# Create a new empty string.
init
do
length += sl
end
- redef fun ==(o)
- do
- if not o isa FlatBuffer then return false
- var l = length
- if o.length != l then return false
- var i = 0
- var it = items
- var oit = o.items
- while i < l do
- if it[i] != oit[i] then return false
- i += 1
- end
- return true
- end
-
# Copies the content of self in `dest`
fun copy(start: Int, len: Int, dest: Buffer, new_start: Int)
do