end
# Is there no item in the collection?
+ #
+ # assert [1,2,3].is_empty == false
+ # assert [1..1[.is_empty == true
fun is_empty: Bool is abstract
# Number of items in the collection.
+ #
+ # assert [10,20,30].length == 3
+ # assert [20..30[.length == 10
fun length: Int is abstract
# Is `item` in the collection ?
# Comparisons are done with ==
+ #
+ # assert [1,2,3].has(2) == true
+ # assert [1,2,3].has(9) == false
+ # assert [1..5[.has(2) == true
+ # assert [1..5[.has(9) == false
fun has(item: E): Bool is abstract
# Is the collection contain only `item`?
# Comparisons are done with ==
# Return true if the collection is empty.
+ #
+ # assert [1,1,1].has_only(1) == true
+ # assert [1,2,3].has_only(1) == false
+ # assert [1..1].has_only(1) == true
+ # assert [1..3].has_only(1) == false
+ # assert [3..3[.has_only(1) == true # empty collection
+ #
+ # ENSURE `is_empty implies (return) == true`
fun has_only(item: E): Bool is abstract
# How many occurrences of `item` are in the collection?
# Comparisons are done with ==
+ #
+ # assert [10,20,10].count(10) == 2
fun count(item: E): Int is abstract
# Return one the item of the collection
+ #
+ # assert [1,2,3].first == 1
fun first: E is abstract
end
fun -(i: Float): Float is intern
fun *(i: Float): Float is intern
fun /(i: Float): Float is intern
-
+
# The integer part of `self`.
+ #
+ # assert (0.0).to_i == 0
+ # assert (0.9).to_i == 0
+ # assert (-0.9).to_i == 0
+ # assert (9.9).to_i == 9
+ # assert (-9.9).to_i == -9
fun to_i: Int is intern
end
redef fun -(i) is intern
fun *(i: Int): Int is intern
fun /(i: Int): Int is intern
- fun %(i: Int): Int is intern
+ fun %(i: Int): Int is intern
+
+ # `i` bits shift fo the left (aka <<)
+ #
+ # assert 5.lshift(1) == 10
fun lshift(i: Int): Int is intern
- fun rshift(i: Int): Int is intern
+
+ # `i` bits shift fo the right (aka >>)
+ #
+ # assert 5.rshift(1) == 2
+ fun rshift(i: Int): Int is intern
# The float equivalent of `self`
+ #
+ # assert 5.to_f == 5.0
+ # assert 5.to_f != 5 # Float and Int are not equals
fun to_f: Float is intern
redef fun succ is intern
return -d
end
end
-
+
redef fun <=>(other)
do
if self < other then
end
# The character whose ASCII value is `self`.
+ #
+ # assert 65.ascii == 'A'
+ # assert 10.ascii == '\n'
fun ascii: Char is intern
# Number of digits of an integer in base `b` (plus one if negative)
+ #
+ # assert 123.digit_count(10) == 3
+ # assert 123.digit_count(2) == 7 # 1111011 in binary
fun digit_count(b: Int): Int
do
if b == 10 then return digit_count_base_10
# Return the corresponding digit character
# If 0 <= `self` <= 9, return the corresponding character.
+ # assert 5.to_c == '5'
# If 10 <= `self` <= 36, return the corresponding letter [a..z].
+ # assert 15.to_c == 'f'
fun to_c: Char
do
assert self >= 0 and self <= 36 # TODO plan for this
end
# The absolute value of self
+ #
+ # assert (-10).abs == 10
+ # assert 10.abs == 10
+ # assert 0.abs == 0
fun abs: Int
do
if self >= 0
end
# If `self` is a digit then return this digit else return -1.
+ #
+ # assert '5'.to_i == 5
fun to_i: Int
do
end
# the ascii value of self
+ #
+ # assert 'a'.ascii == 97
+ # assert '\n'.ascii == 10
fun ascii: Int is intern
redef fun +(i) is intern
# Return the lower case version of self.
# If self is not a letter, then return self
+ #
+ # assert 'A'.to_lower == 'a'
+ # assert 'a'.to_lower == 'a'
+ # assert '$'.to_lower == '$'
fun to_lower: Char
do
if is_upper then
# Return the upper case version of self.
# If self is not a letter, then return self
+ #
+ # assert 'a'.to_upper == 'A'
+ # assert 'A'.to_upper == 'A'
+ # assert '$'.to_upper == '$'
fun to_upper: Char
do
if is_lower then
return self
end
end
-
+
# Is self a digit? (from '0' to '9')
+ #
+ # assert '0'.is_digit == true
+ # assert '9'.is_digit == true
+ # assert 'a'.is_digit == false
fun is_digit : Bool
do
return self >= '0' and self <= '9'
end
-
+
# Is self a lower case letter? (from 'a' to 'z')
+ #
+ # assert 'a'.is_lower == true
+ # assert 'z'.is_lower == true
+ # assert 'A'.is_lower == false
+ # assert '$'.is_lower == false
fun is_lower : Bool
do
return self >= 'a' and self <= 'z'
end
-
+
# Is self a upper case letter? (from 'A' to 'Z')
+ #
+ # assert 'A'.is_upper == true
+ # assert 'A'.is_upper == true
+ # assert 'z'.is_upper == false
+ # assert '$'.is_upper == false
fun is_upper : Bool
do
return self >= 'A' and self <= 'Z'
end
-
+
# Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
+ #
+ # assert 'A'.is_letter == true
+ # assert 'A'.is_letter == true
+ # assert 'z'.is_letter == true
+ # assert '$'.is_letter == false
fun is_letter : Bool
do
return is_lower or is_upper
readable private var _items: NativeString
# Access a character at `index` in the string.
+ #
+ # assert "abcd"[2] == 'c'
redef fun [](index) do return _items[index]
# Create a substring.
fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
# If `self` contains only digits, return the corresponding integer
+ #
+ # assert "123".to_i == 123
+ # assert "-1".to_i == -1
fun to_i: Int
do
# Shortcut
end
# If `self` contains a float, return the corresponding float
+ #
+ # assert "123".to_f == 123.0
+ # assert "-1".to_f == -1.0
+ # assert "-1.2e-3".to_f == -1.2e-3
fun to_f: Float
do
# Shortcut
fun to_hex: Int do return a_to(16)
# If `self` contains only digits and letters, return the corresponding integer in a given base
+ #
+ # assert "120".a_to(3) == 15
fun a_to(base: Int) : Int
do
var i = 0
end
# Returns `true` if the string contains only Numeric values (and one "," or one "." character)
+ #
+ # assert "123".is_numeric == true
+ # assert "1.2".is_numeric == true
+ # assert "1,2".is_numeric == true
+ # assert "1..2".is_numeric == false
fun is_numeric: Bool
do
var has_point_or_comma = false
end
# A upper case version of `self`
+ #
+ # assert "Hello World!".to_upper == "HELLO WORLD!"
fun to_upper: String
do
var s = new Buffer.with_capacity(length)
end
# A lower case version of `self`
+ #
+ # assert "Hello World!".to_lower == "hello world!"
fun to_lower : String
do
var s = new Buffer.with_capacity(length)
# Trims trailing and preceding white spaces
# A whitespace is defined as any character which ascii value is less than or equal to 32
+ #
+ # assert " Hello World ! ".trim == "Hello World !"
+ # assert "\na\nb\tc\t".trim == "a\nb\tc"
fun trim: String
do
if self._length == 0 then return self.to_s
end
# The concatenation of `self` with `s`
+ #
+ # assert "hello " + "world!" == "hello world!"
fun +(s: String): String
do
var my_length = self._length
end
# `i` repetitions of `self`
+ #
+ # assert "abc"*3 == "abcabcabc"
+ # assert "abc"*1 == "abc"
+ # assert "abc"*0 == ""
fun *(i: Int): String
do
assert i >= 0
private fun native_class_name: NativeString is intern
# The class name of the object.
+ #
+ # assert 5.class_name == "Int"
fun class_name: String do return new String.from_cstring(native_class_name)
# Developer readable representation of `self`.
end
redef class Bool
+ # assert true.to_s == "true"
+ # assert false.to_s == "false"
redef fun to_s
do
if self then
private fun native_int_to_s(len: Int): NativeString is extern "native_int_to_s"
# return displayable int in base 10 and signed
+ #
+ # assert 1.to_s == "1"
+ # assert (-123).to_s == "-123"
redef fun to_s do
var len = digit_count(10)
return new String.from_cstring(native_int_to_s(len))
end
redef class Float
- # Pretty print self, print needed decimals up to a max of 6.
+ # Pretty print self, print needoed decimals up to a max of 3.
redef fun to_s do
var str = to_precision( 3 )
var len = str.length
end
redef class Char
+ # assert 'x'.to_s == "x"
redef fun to_s
do
var s = new Buffer.with_capacity(1)
end
# Concatenate and separate each elements with `sep`.
+ #
+ # assert [1, 2, 3].join(":") == "1:2:3"
+ # assert [1..3].join(":") == "1:2:3"
fun join(sep: String): String
do
if is_empty then return ""
# Concatenate couple of 'key value'.
# key and value are separated by `couple_sep`.
# each couple is separated each couple with `sep`.
+ #
+ # var m = new ArrayMap[Int, String]
+ # m[1] = "one"
+ # m[10] = "ten"
+ # assert m.join("; ", "=") == "1=one; 10=ten"
fun join(sep: String, couple_sep: String): String
do
if is_empty then return ""