+ # Encode `self` to percent (or URL) encoding
+ #
+ # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
+ # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
+ # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
+ fun to_percent_encoding: String
+ do
+ var buf = new FlatBuffer
+
+ for i in [0..length[ do
+ var c = chars[i]
+ if (c >= '0' and c <= '9') or
+ (c >= 'a' and c <= 'z') or
+ (c >= 'A' and c <= 'Z') or
+ c == '-' or c == '.' or
+ c == '_' or c == '~'
+ then
+ buf.add c
+ else buf.append "%{c.ascii.to_hex}"
+ end
+
+ return buf.to_s
+ end
+
+ # Decode `self` from percent (or URL) encoding to a clear string
+ #
+ # Replace invalid use of '%' with '?'.
+ #
+ # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
+ # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
+ # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
+ # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
+ # assert "incomplete %".from_percent_encoding == "incomplete ?"
+ # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
+ fun from_percent_encoding: String
+ do
+ var buf = new FlatBuffer
+
+ var i = 0
+ while i < length do
+ var c = chars[i]
+ if c == '%' then
+ if i + 2 >= length then
+ # What follows % has been cut off
+ buf.add '?'
+ else
+ i += 1
+ var hex_s = substring(i, 2)
+ if hex_s.is_hex then
+ var hex_i = hex_s.to_hex
+ buf.add hex_i.ascii
+ i += 1
+ else
+ # What follows a % is not Hex
+ buf.add '?'
+ i -= 1
+ end
+ end
+ else buf.add c
+
+ i += 1
+ end
+
+ return buf.to_s
+ end
+
+ # Escape the four characters `<`, `>`, `&`, and `"` with their html counterpart
+ #
+ # assert "a&b->\"x\"".html_escape == "a&b->"x""
+ fun html_escape: SELFTYPE
+ do
+ var buf = new FlatBuffer
+
+ for i in [0..length[ do
+ var c = chars[i]
+ if c == '&' then
+ buf.append "&"
+ else if c == '<' then
+ buf.append "<"
+ else if c == '>' then
+ buf.append ">"
+ else if c == '"' then
+ buf.append """
+ else buf.add c
+ end
+
+ return buf.to_s
+ end
+
+ # Equality of text
+ # Two pieces of text are equals if thez have the same characters in the same order.
+ #
+ # assert "hello" == "hello"
+ # assert "hello" != "HELLO"
+ # assert "hello" == "hel"+"lo"
+ #
+ # Things that are not Text are not equal.
+ #
+ # assert "9" != '9'
+ # assert "9" != ['9']
+ # assert "9" != 9
+ #
+ # assert "9".chars.first == '9' # equality of Char
+ # assert "9".chars == ['9'] # equality of Sequence
+ # assert "9".to_i == 9 # equality of Int
+ 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
+
+ # Lexicographical comparaison
+ #
+ # assert "abc" < "xy"
+ # assert "ABC" < "abc"
+ redef fun <(other)
+ do
+ var self_chars = self.chars.iterator
+ var other_chars = other.chars.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
+
+ # Escape string used in labels for graphviz
+ #
+ # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
+ fun escape_to_dot: String
+ do
+ return escape_more_to_c("|\{\}<>")
+ end
+
+ # Flat representation of self
+ fun flatten: FlatText is abstract
+
+ private var hash_cache: nullable Int = null
+
+ redef fun hash
+ do
+ if hash_cache == null then
+ # djb2 hash algorithm
+ var h = 5381
+
+ for i in [0..length[ do
+ var char = chars[i]
+ h = h.lshift(5) + h + char.ascii
+ end
+
+ hash_cache = h
+ end
+ return hash_cache.as(not null)
+ end
+