From: Lucas Bajolet Date: Mon, 24 Mar 2014 15:53:58 +0000 (-0400) Subject: stdlib/strings: Moved, factorized and generalized comparison operators to Text. X-Git-Tag: v0.6.5~7^2~3^2~17 X-Git-Url: http://nitlanguage.org stdlib/strings: Moved, factorized and generalized comparison operators to Text. Signed-off-by: Lucas Bajolet --- diff --git a/lib/standard/string.nit b/lib/standard/string.nit index 1dcbb31..703db45 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -27,6 +27,9 @@ intrude import collection # FIXME should be collection::array # 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 @@ -421,6 +424,20 @@ abstract class Text 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. @@ -447,9 +464,12 @@ end # 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) @@ -507,6 +527,36 @@ abstract class StringCharView 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 @@ -521,12 +571,9 @@ end # 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 @@ -664,7 +711,7 @@ class String 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 @@ -694,6 +741,8 @@ class String # 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 @@ -897,12 +946,9 @@ end # 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 @@ -950,28 +996,6 @@ class FlatBuffer 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 @@ -1004,21 +1028,6 @@ class FlatBuffer 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