# Instances of this class offers an iterator method.
#
# Collections instances can use the "for" structure:
-# var x: Collection[U]
-# ...
+# var x: Collection[U]
+# # ...
# for u in x do
# # u is a U
-# ...
+# # ...
# end
# that is equivalent with
# var x: Collection[U]
-# ...
+# # ...
# var i = x.iterator
# while i.is_ok do
# var u = i.item # u is a U
-# ...
+# # ...
# i.next
# end
#
end
end
- # Is there no item in the collection ?
+ # Is there no item in the collection?
fun is_empty: Bool is abstract
# Number of items in the collection.
fun length: Int is abstract
- # Is `item' in the collection ?
+ # Is `item` in the collection ?
# Comparisons are done with ==
fun has(item: E): Bool is abstract
- # Is the collection contain only `item' ?
+ # Is the collection contain only `item`?
# Comparisons are done with ==
# Return true if the collection is empty.
fun has_only(item: E): Bool is abstract
- # How many occurrences of `item' are in the collection ?
+ # How many occurrences of `item` are in the collection?
# Comparisons are done with ==
fun count(item: E): Int is abstract
# They are mainly used with collections.
interface Iterator[E]
# The current item.
- # Require `is_ok'.
+ # Require `is_ok`.
fun item: E is abstract
# Jump to the next item.
- # Require `is_ok'.
+ # Require `is_ok`.
fun next is abstract
# Is there a current item ?
# Remove all items
fun clear is abstract
- # Remove an occucence of `item'
+ # Remove an occucence of `item`
fun remove(item: E) is abstract
- # Remove all occurences of `item'
+ # Remove all occurences of `item`
fun remove_all(item: E) do while has(item) do remove(item)
end
# Abstract sets.
#
# Set contains contains only one element with the same value (according to ==).
-# var s: Set[E]
-# var a = "Hello"
-# var b = "Hel" + "lo"
-# ...
-# s.add(a)
-# s.has(b) # --> true
+# var s: Set[E]
+# var a = "Hello"
+# var b = "Hel" + "lo"
+# # ...
+# s.add(a)
+# s.has(b) # --> true
interface Set[E: Object]
super SimpleCollection[E]
redef fun remove_all(item) do remove(item)
end
-# MapRead are abstract associative collections: `key' -> `item'.
+# MapRead are abstract associative collections: `key` -> `item`.
interface MapRead[K: Object, E]
- # Get the item at `key'.
+ # Get the item at `key`.
fun [](key: K): E is abstract
- # Depreciated alias for `keys.has'
+ # Depreciated alias for `keys.has`
fun has_key(key: K): Bool do return self.keys.has(key)
# Get a new iterator on the map.
end
# Return the point of view of self on the values only.
- # Note that `self' and `values' are views on the same data;
+ # Note that `self` and `values` are views on the same data;
# therefore any modification of one is visible on the other.
fun values: Collection[E] is abstract
# Return the point of view of self on the keys only.
- # Note that `self' and `keys' are views on the same data;
+ # Note that `self` and `keys` are views on the same data;
# therefore any modification of one is visible on the other.
fun keys: Collection[K] is abstract
fun length: Int is abstract
end
-# Maps are associative collections: `key' -> `item'.
+# Maps are associative collections: `key` -> `item`.
#
# The main operator over maps is [].
#
# var map: Map[U, V]
-# ...
+# # ...
# map[u1] = v1 # Associate 'v1' to 'u1'
# map[u2] = v2 # Associate 'v2' to 'u2'
-# map[u1] # -> v1
-# map[u2] # -> v2
+# print map[u1] # -> v1
+# print map[u2] # -> v2
#
# Instances of maps can be used with the for structure
#
-# for key, value in map do ..
+# for key, value in map do # things with `key` and `value`
#
-# The keys and values in the map can also be manipulated directly with the `keys' and `values' methods.
+# The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
#
# map.keys.has(u1) # -> true
# map.keys.has(u3) # -> false
-# map.values.has(v1) # -> true
-# map.values.has(v3) # -> false
+# map.values.has(v1) # -> true
+# map.values.has(v3) # -> false
#
interface Map[K: Object, E]
super MapRead[K, E]
- # Set the`item' at `key'.
+ # Set the`item` at `key`.
fun []=(key: K, item: E) is abstract
- # Add each (key,value) of `map' into `self'.
- # If a same key exists in `map' and `self', then the value in self is discarded.
+ # Add each (key,value) of `map` into `self`.
+ # If a same key exists in `map` and `self`, then the value in self is discarded.
fun recover_with(map: Map[K, E])
do
var i = map.iterator
# Iterators for Map.
interface MapIterator[K: Object, E]
# The current item.
- # Require `is_ok'.
+ # Require `is_ok`.
fun item: E is abstract
# The key of the current item.
- # Require `is_ok'.
+ # Require `is_ok`.
fun key: K is abstract
# Jump to the next item.
- # Require `is_ok'.
+ # Require `is_ok`.
fun next is abstract
# Is there a current item ?
fun is_ok: Bool is abstract
- # Set a new `item' at `key'.
+ # Set a new `item` at `key`.
#fun item=(item: E) is abstract
end
end
# Sequences are indexed collections.
-# The first item is 0. The last is `length'-1.
+# The first item is 0. The last is `length-1`.
interface SequenceRead[E]
super Collection[E]
# Get the first item.
- # Is equivalent with `self'[0].
+ # Is equivalent with `self[0]`.
redef fun first
do
assert not_empty: not is_empty
end
# Return the index=th element of the sequence.
- # The first element is 0 and the last if `length-1'
+ # The first element is 0 and the last if `length-1`
# If index is invalid, the program aborts
fun [](index: Int): E is abstract
# Get the last item.
- # Is equivalent with `self'[`length'-1].
+ # Is equivalent with `self[length-1]`.
fun last: E
do
assert not_empty: not is_empty
return self[length-1]
end
- # Return the index of the first occurrence of `item'.
- # Return -1 if `item' is not found
+ # Return the index of the first occurrence of `item`.
+ # Return -1 if `item` is not found
# Comparison is done with ==
fun index_of(item: E): Int
do
end
# Sequence are indexed collection.
-# The first item is 0. The last is `length'-1.
+# The first item is 0. The last is `length-1`.
interface Sequence[E]
super SequenceRead[E]
super SimpleCollection[E]
# Set the first item.
- # Is equivalent with `self'[0] = `item'.
+ # Is equivalent with `self[0] = item`.
fun first=(item: E)
do self[0] = item end
# Set the last item.
- # Is equivalent with `self'[length-1] = `item'.
+ # Is equivalent with `self[length-1] = item`.
fun last=(item: E)
do
var l = length
end
end
- # A synonym of `push'
+ # A synonym of `push`
redef fun add(e) do push(e)
# Add an item after the last.
# The second item become the first.
fun shift: E is abstract
- # Set the`item' at `index'.
+ # Set the `item` at `index`.
fun []=(index: Int, item: E) is abstract
- # Remove the item at `index' and shift all following elements
+ # Remove the item at `index` and shift all following elements
fun remove_at(index: Int) is abstract
end
return -1
end
- # Return a new array that is the reverse of `self'
+ # Return a new array that is the reverse of `self`
#
# [1,2,3].reversed # -> [3, 2, 1]
fun reversed: Array[E]
return result
end
- # Copy a portion of `self' to an other array.
+ # Copy a portion of `self` to an other array.
#
# var a = [1, 2, 3, 4]
# var b = [10, 20, 30, 40, 50]
super AbstractArrayRead[E]
super Sequence[E]
- # Force the capacity to be at least `cap'.
+ # Force the capacity to be at least `cap`.
# The capacity of the array is an internal information.
# However, this method can be used to prepare a large amount of add
fun enlarge(cap: Int) is abstract
self.add_all(items)
end
- # Create an array with some `items'.
+ # Create an array with some `objects`.
init with_items(objects: E...)
do
_items = objects._items
_length = 0
end
- # Create an array of `count' elements
+ # Create an array of `count` elements
init filled_with(value: E, count: Int)
do
assert positive: count >= 0
# FIXME: Remove it once modules can intrude non local modules
fun intern_items: NativeArray[E] do return _items.as(not null)
- # The size of `_items'.
+ # The size of `_items`.
var _capacity: Int = 0
# Sort the array using the !cmp function.
sub_sort(0, length-1) !cmp(x,y) = cmp(x, y)
end
- # Sort `array' between `from' and `to' indices
+ # Sort `array` between `from` and `to` indices
private fun sub_sort(from: Int, to: Int)
!cmp(e1,e2: E): Int
do
end
end
-# An `Iterator' on `AbstractArray'
+# An `Iterator` on `AbstractArray`
class ArrayIterator[E]
super IndexedIterator[E]
redef fun iterator do return new ArraySetIterator[E](_array.iterator)
- # Assume the capacity is at least `cap'.
+ # Assume the capacity is at least `cap`.
fun enlarge(cap: Int) do _array.enlarge(cap)
private fun remove_at(i: Int)
redef fun clear do _items.clear
- # Assume the capacity to be at least `cap'.
+ # Assume the capacity to be at least `cap`.
fun enlarge(cap: Int) do _items.enlarge(cap)
redef fun couple_at(key)
# The last positive result given by a index(1) call
var _last_index: Int = 0
- # Where is the `key' in `_item'?
+ # Where is the `key` in `_item`?
# return -1 if not found
private fun index(key: K): Int
do
# Others tools ################################################################
redef class Iterator[E]
- # Interate on `self' and build an array
+ # Interate on `self` and build an array
fun to_a: Array[E]
do
var res = new Array[E]
# Subclasses of this class can create native arrays
interface ArrayCapable[E]
- # Get a new array of `size' elements.
+ # Get a new array of `size` elements.
protected fun calloc_array(size: Int): NativeArray[E] is intern
end
# Return the node assosiated with the key
fun node_at(k: K): nullable N
do
- # cache: `is' is used instead of `==' because it is a faster filter (even if not exact)
+ # cache: `is` is used instead of `==` because it is a faster filter (even if not exact)
if k is _last_accessed_key then return _last_accessed_node
var res = node_at_idx(index_at(k), k)
var c = _array[i]
while c != null do
var ck = c._key
- if ck is k or ck == k then # prefilter with `is' because the compiler is not smart enought yet
+ if ck is k or ck == k then # prefilter with `is` because the compiler is not smart enought yet
break
end
c = c._next_in_bucklet
end
# A map implemented with a hash table.
-# Keys of such a map cannot be null and require a working `hash' method
+# Keys of such a map cannot be null and require a working `hash` method
class HashMap[K: Object, V]
super Map[K, V]
super HashCollection[K, HashMapNode[K, V]]
end
end
-# A `Set' implemented with a hash table.
-# Keys of such a map cannot be null and require a working `hash' method
+# A `Set` implemented with a hash table.
+# Keys of such a map cannot be null and require a working `hash` method
class HashSet[E: Object]
super Set[E]
super HashCollection[E, HashSetNode[E]]
enlarge(0)
end
- # Build a list filled with the items of `coll'.
+ # Build a list filled with the items of `coll`.
init from(coll: Collection[E]) do
init
add_all(coll)
_head = node
end
- # Append `l' to `self' but clear `l'.
+ # Append `l` to `self` but clear `l`.
##
# O(1)
fun link(l: List[E])
# Build an empty list.
init do end
- # Build a list filled with the items of `coll'.
+ # Build a list filled with the items of `coll`.
init from(coll: Collection[E]) do append(coll)
# The first node of the list
# The last node of the list
var _tail: nullable ListNode[E]
- # Get the `i'th node. get `null' otherwise.
+ # Get the `i`th node. get `null` otherwise.
private fun get_node(i: Int): nullable ListNode[E]
do
var n = _head
return n
end
- # get the first node that contains e after 'after', null otherwise
+ # get the first node that contains `e` after 'after', null otherwise
private fun search_node_after(e: E, after: nullable ListNode[E]): nullable ListNode[E]
do
var n = after
_index += 1
end
- # Build a new iterator from `node'.
+ # Build a new iterator for `list`.
private init(list: List[E])
do
_list = list
redef fun is_empty do return _first >= _after
- # Create a range [`from', `to'].
- # The syntax [`from'..`to'[ is equivalent.
+ # Create a range [`from`, `to`].
+ # The syntax `[from..to[` is equivalent.
init(from: E, to: E)
do
_first = from
_after = to.succ
end
- # Create a range [`from', `to'[.
- # The syntax [`from'..`to'[ is equivalent.
+ # Create a range [`from`, `to`[.
+ # The syntax `[from..to[` is equivalent.
init without_last(from: E, to: E)
do
_first = from
# another product.
# This module contains classes used to sorts arrays.
-# In order to provide your own sort class you should define a subclass of AbstractSorter with
-# a custom `compare' function.
+# In order to provide your own sort class you should define a subclass of `AbstractSorter` with
+# a custom `AbstractSorter::compare` function.
package sorter
import array
# This abstract class generalizes ways to sort an array
-# TODO: rename *Sorter to *Comparator
+# TODO: rename `AbstractSorter` to `Comparator`
interface AbstractSorter[E]
- # Compare `a' and `b'.
+ # Compare `a` and `b`.
# Returns:
# -1 if a < b
# 0 if a = b
# 1 if a > b
fun compare(a: E, b: E): Int is abstract
- # Sort `array' using the `compare' function.
+ # Sort `array` using the `compare` function.
fun sort(array: Array[E]) do sub_sort(array, 0, array.length-1)
- # Sort `array' between `from' and `to' indices
+ # Sort `array` between `from` and `to` indices
private fun sub_sort(array: Array[E], from: Int, to: Int)
do
if from >= to then
end
end
- # Quick-sort `array' between `from' and `to' indices
+ # Quick-sort `array` between `from` and `to` indices
private fun quick_sort(array: Array[E], from: Int, to: Int)
do
var pivot = array[from]
sub_sort(array, i, to)
end
- # Bubble-sort `array' between `from' and `to' indices
+ # Bubble-sort `array` between `from` and `to` indices
private fun bubble_sort(array: Array[E], from: Int, to: Int)
do
var i = from
end
# This class uses the operator <=> to sort arrays.
-# You can also use the `sort' method of the `Array' class.
+# You can also use the `sort` method of the `Array` class.
class ComparableSorter[E: Comparable]
super AbstractSorter[E]
# Return a <=> b
redef class Object
# Simple I/O
- # Print `objects' on the standard output (`stdout').
+ # Print `objects` on the standard output (`stdout`).
protected fun printn(objects: Object...)
do
stdout.write(objects.to_s)
end
- # Print an `object' on the standard output (`stdout') and add a newline.
+ # Print an `object` on the standard output (`stdout`) and add a newline.
protected fun print(object: Object)
do
stdout.write(object.to_s)
stdout.write("\n")
end
- # Read a character from the standard input (`stdin').
+ # Read a character from the standard input (`stdin`).
protected fun getc: Char
do
return stdin.read_char.ascii
end
- # Read a line from the standard input (`stdin').
+ # Read a line from the standard input (`stdin`).
protected fun gets: String
do
return stdin.read_line
# End of file?
redef readable var _end_reached: Bool = false
- # Open the file at `path' for reading.
+ # Open the file at `path` for reading.
init open(path: String)
do
_path = path
# Is the file open in write mode
var _writable: Bool
- # Write `len' bytes from `native'.
+ # Write `len` bytes from `native`.
private fun write_native(native: NativeString, len: Int)
do
assert _writable
end
end
- # Open the file at `path' for writing.
+ # Open the file at `path` for writing.
init open(path: String)
do
_file = new NativeFile.io_open_write(path.to_cstring)
# * the validity of the path is not checked
#
# "some/./complex/../../path/from/../to/a////file//".simplify_path # -> "path/to/a/file"
- # "../dir/file" # -> "../dir/file"
- # "dir/../../" # -> ".."
- # "//absolute//path/" # -> "/absolute/path"
+ # "../dir/file".simplify_path # -> "../dir/file"
+ # "dir/../../".simplify_path # -> ".."
+ # "//absolute//path/".simplify_path # -> "/absolute/path"
fun simplify_path: String
do
var a = self.split_with("/")
# Correctly join two path using the directory separator.
#
- # Using a standard "{self}/{path}" does not work when `self' is the empty string.
+ # Using a standard "{self}/{path}" does not work when `self` is the empty string.
# This method ensure that the join is valid.
#
# "hello".join_path("world") # -> "hello/world"
# "".join_path("world") # -> "world"
# "/hello".join_path("/world") # -> "/world"
#
- # Note: you may want to use `simplify_path' on the result
+ # Note: you may want to use `simplify_path` on the result
#
# Note: I you want to join a great number of path, you can write
#
# The hash code of the object.
# Assuming that a == b -> a.hash == b.hash
##
- # Without redefinition, it is based on the `object_id' of the instance.
+ # Without redefinition, it is based on the `object_id` of the instance.
fun hash: Int do return object_id / 8
end
# The identifier is used internally to provide a hash value.
fun object_id: Int is intern
- # Return true if `self' and `other' have the same dynamic type.
+ # Return true if `self` and `other` have the same dynamic type.
# Unless specific code, you should not use this method.
fun is_same_type(other: Object): Bool is intern
- # Have `self' and `other' the same value?
+ # Have `self` and `other` the same value?
##
# The exact meaning of "same value" is let to the subclasses.
- # Implicitly, the default implementation, is `is'
+ # Implicitly, the default implementation, is `is`
fun ==(other: nullable Object): Bool do return self is other
- # Have `self' and `other' different values?
+ # Have `self` and `other` different values?
##
# != is equivalent with "not ==".
fun !=(other: nullable Object): Bool do return not (self == other)
# Quit the program with a specific return code
protected fun exit(exit_value: Int) is intern
- # Return the global sys object, the only instance of the `Sys' class.
+ # Return the global sys object, the only instance of the `Sys` class.
protected fun sys: Sys is intern
end
# The main class of the program.
-# `Sys' is a singleton class, its only instance is `sys' defined in `Object'.
-# `sys' is used to invoke methods on the program on the system.
+# `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
+# `sys` is used to invoke methods on the program on the system.
class Sys
# Instructions outside classes implicitly redefine this method.
fun main do end
# The ancestor of class where objects are in a total order.
# In order to work, the method '<' has to be redefined.
interface Comparable
- # What `self' can be compared to?
+ # What `self` can be compared to?
type OTHER: Comparable
- # Is `self' lesser than `other'?
+ # Is `self` lesser than `other`?
fun <(other: OTHER): Bool is abstract
- # not `other' < `self'
- # Note, the implementation must ensure that: (x<=y) == (x<y or x==y)
+ # not `other` < `self`
+ # Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
fun <=(other: OTHER): Bool do return not other < self
- # not `self' < `other'
- # Note, the implementation must ensure that: (x>=y) == (x>y or x==y)
+ # not `self` < `other`
+ # Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
fun >=(other: OTHER): Bool do return not self < other
- # `other' < `self'
+ # `other` < `self`
fun >(other: OTHER): Bool do return other < self
# -1 if <, +1 if > and 0 otherwise
return c <= self and self <= d
end
- # The maximum between `self' and `other' (prefers `self' if equals).
+ # The maximum between `self` and `other` (prefers `self` if equals).
fun max(other: OTHER): OTHER
do
if self < other then
end
end
- # The minimum between `self' and `c' (prefer `self' if equals)
+ # The minimum between `self` and `c` (prefer `self` if equals)
fun min(c: OTHER): OTHER
do
if c < self then
# The previous element.
fun prec: OTHER do return self - 1
- # The `i'-th successor element.
+ # The `i`-th successor element.
fun +(i: Int): OTHER is abstract
- # The `i'-th previous element.
+ # The `i`-th previous element.
fun -(i: Int): OTHER is abstract
# The distance between self and d.
- # 10.distance(15) # --> 5
- # 'Z'.distance('A') # --> 25
+ #
+ # 10.distance(15) # --> 5
+ # 'Z'.distance('A') # --> 25
fun distance(d: OTHER): Int
do
var cursor: OTHER
###############################################################################
# Native Booleans.
-# `true' and `false' are the only instances.
+# `true` and `false` are the only instances.
# Boolean are manipulated trough three special operators:
-# `and', `or', `not'.
+# `and`, `or`, `not`.
# Booleans are mainly used by conditional statement and loops.
universal Bool
redef fun object_id is intern
fun *(i: Float): Float is intern
fun /(i: Float): Float is intern
- # The integer part of `self'.
+ # The integer part of `self`.
fun to_i: Int is intern
end
fun lshift(i: Int): Int is intern
fun rshift(i: Int): Int is intern
- # The float equivalent of `self'
+ # The float equivalent of `self`
fun to_f: Float is intern
redef fun succ is intern
end
end
- # The character whose ASCII value is `self'.
+ # The character whose ASCII value is `self`.
fun ascii: Char is intern
- # Number of digits of an integer in base `b' (plus one if negative)
+ # Number of digits of an integer in base `b` (plus one if negative)
fun digit_count(b: Int): Int
do
if b == 10 then return digit_count_base_10
end
# Return the corresponding digit character
- # If 0 <= `self' <= 9, return the corresponding character.
- # If 10 <= `self' <= 36, return the corresponding letter [a..z].
+ # If 0 <= `self` <= 9, return the corresponding character.
+ # If 10 <= `self` <= 36, return the corresponding letter [a..z].
fun to_c: Char
do
assert self >= 0 and self <= 36 # TODO plan for this
# Native characters.
# Characters are denoted with simple quote.
-# eg. 'a' or '\n'.
+# eg. `'a'` or `'\n'`.
universal Char
super Discrete
redef type OTHER: Char
end
end
- # If `self' is a digit then return this digit else return -1.
+ # If `self` is a digit then return this digit else return -1.
fun to_i: Int
do
return s.to_s
end
- # Read a string until the end of the line and append it to `s'.
+ # Read a string until the end of the line and append it to `s`.
fun append_line_to(s: Buffer)
do
loop
# Is the last fill_buffer reach the end
protected fun end_reached: Bool is abstract
- # Allocate a `_buffer' for a given `capacity'.
+ # Allocate a `_buffer` for a given `capacity`.
protected fun prepare_buffer(capacity: Int)
do
_buffer = new Buffer.with_capacity(capacity)
# Create a substring.
#
- # "abcd".substring(1, 2) # --> "bc"
- # "abcd".substring(-1, 2) # --> "a"
- # "abcd".substring(1, 0) # --> ""
- # "abcd".substring(2, 5) # --> "cd"
+ # "abcd".substring(1, 2) # --> "bc"
+ # "abcd".substring(-1, 2) # --> "a"
+ # "abcd".substring(1, 0) # --> ""
+ # "abcd".substring(2, 5) # --> "cd"
fun substring(from: Int, count: Int): String
do
assert count >= 0
end
end
- # Create a substring from `self' beginning at the 'from' position
+ # Create a substring from `self` beginning at the `from` position
#
- # "abcd".substring(1) # --> "bcd"
- # "abcd".substring(-1) # --> "abcd"
- # "abcd".substring(2) # --> "cd"
+ # "abcd".substring_from(1) # --> "bcd"
+ # "abcd".substring_from(-1) # --> "abcd"
+ # "abcd".substring_from(2) # --> "cd"
fun substring_from(from: Int): String
do
assert from < length
return substring(from, length - from)
end
- # Does self have a substring 'str' starting from position 'pos
+ # Does self have a substring `str` starting from position `pos`?
#
- # "abcd".has_substring("bc",1) # --> true
- # "abcd".has_substring("bc",2) # --> false
+ # "abcd".has_substring("bc",1) # --> true
+ # "abcd".has_substring("bc",2) # --> false
fun has_substring(str: String, pos: Int): Bool
do
var itsindex = str.length - 1
return true
end
- # Is this string prefixed by 'prefix'
+ # Is this string prefixed by `prefix`?
#
- # "abc".is_prefix("abcd") # --> true
- # "bc".is_prefix("abcd") # --> false
+ # "abc".has_prefix("abcd") # --> true
+ # "bc".has_prefix("abcd") # --> false
fun has_prefix(prefix: String): Bool do return has_substring(prefix,0)
- # Is this string suffixed by 'suffix'
+ # Is this string suffixed by `suffix`?
#
- # "abcd".has_suffix("abc") # --> false
- # "abcd".has_suffix("bcd") # --> true
+ # "abcd".has_suffix("abc") # --> false
+ # "abcd".has_suffix("bcd") # --> true
fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
- # If `self' contains only digits, return the corresponding integer
+ # If `self` contains only digits, return the corresponding integer
fun to_i: Int
do
# Shortcut
return to_s.to_cstring.atoi
end
- # If `self' contains a float, return the corresponding float
+ # If `self` contains a float, return the corresponding float
fun to_f: Float
do
# Shortcut
return to_s.to_cstring.atof
end
- # If `self' contains only digits and alpha <= 'f', return the corresponding integer.
+ # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
fun to_hex: Int do return a_to(16)
- # If `self' contains only digits and letters, return the corresponding integer in a given base
+ # If `self` contains only digits and letters, return the corresponding integer in a given base
fun a_to(base: Int) : Int
do
var i = 0
end
end
- # Returns true if the string contains only Numeric values (and one "," or one "." character)
+ # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
fun is_numeric: Bool
do
var has_point_or_comma = false
return true
end
- # A upper case version of `self'
+ # A upper case version of `self`
fun to_upper: String
do
var s = new Buffer.with_capacity(length)
return s.to_s
end
- # A lower case version of `self'
+ # A lower case version of `self`
fun to_lower : String
do
var s = new Buffer.with_capacity(length)
# Create a substring.
#
- # "abcd".substring(1, 2) # --> "bc"
- # "abcd".substring(-1, 2) # --> "a"
- # "abcd".substring(1, 0) # --> ""
- # "abcd".substring(2, 5) # --> "cd"
+ # "abcd".substring(1, 2) # --> "bc"
+ # "abcd".substring(-1, 2) # --> "a"
+ # "abcd".substring(1, 0) # --> ""
+ # "abcd".substring(2, 5) # --> "cd"
#
- # A "from" index < 0 will be replaced by 0
- # Unless a count value is > 0 at the same time
- # In this case, from += count and count -= from
+ # A `from` index < 0 will be replaced by 0
+ # Unless a `count` value is > 0 at the same time
+ # In this case, `from += count` and `count -= from`
#
redef fun substring(from: Int, count: Int): String
do
return new String.from_substring(realFrom, realFrom + count - 1, _items)
end
- # Create a substring from `self' beginning at the 'from' position
+ # Create a substring from `self` beginning at the `from` position
#
- # "abcd".substring_from(1) # --> "bcd"
- # "abcd".substring_from(-1) # --> "abcd"
- # "abcd".substring_from(2) # --> "cd"
+ # "abcd".substring_from(1) # --> "bcd"
+ # "abcd".substring_from(-1) # --> "abcd"
+ # "abcd".substring_from(2) # --> "cd"
#
# As with substring, a "from" index < 0 will be replaced by 0
#
return substring(from, _length)
end
- # Does self have a substring 'str' starting from position 'pos
+ # Does self have a substring `str` starting from position 'pos
#
- # "abcd".has_substring("bc",1) # --> true
- # "abcd".has_substring("bc",2) # --> false
+ # "abcd".has_substring("bc",1) # --> true
+ # "abcd".has_substring("bc",2) # --> false
redef fun has_substring(str: String, pos: Int): Bool
do
var itsindex = str._length - 1
return true
end
- # A upper case version of `self'
+ # A upper case version of `self`
redef fun to_upper: String
do
var outstr = calloc_string(self._length + 1)
return new String.with_native(outstr, self._length)
end
- # A lower case version of `self'
+ # A lower case version of `self`
redef fun to_lower : String
do
var outstr = calloc_string(self._length + 1)
end
# The comparison between two strings is done on a lexicographical basis
- # Eg : "aa" < "b" => true
+ #
+ # "aa" < "b" # => true
redef fun <(other)
do
if self.object_id == other.object_id then return false
return my_length < its_length
end
- # The concatenation of `self' with `r'
+ # The concatenation of `self` with `s`
fun +(s: String): String
do
var my_length = self._length
return new String.with_native(target_string, my_length + its_length)
end
- # i repetitions of self
+ # `i` repetitions of `self`
fun *(i: Int): String
do
assert i >= 0
return true
end
- readable private var _capacity: Int
+ readable private var _capacity: Int
end
###############################################################################
###############################################################################
redef class Object
- # User readable representation of `self'.
+ # User readable representation of `self`.
fun to_s: String do return inspect
# The class name of the object in NativeString format.
private fun native_class_name: NativeString is intern
# The class name of the object.
- # FIXME: real type information is not available at runtime.
- # Therefore, for instance, an instance of List[Bool] has just
- # "List" for class_name
fun class_name: String do return new String.from_cstring(native_class_name)
- # Developer readable representation of `self'.
+ # Developer readable representation of `self`.
# Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
fun inspect: String
do
end
redef class Int
- fun fill_buffer(s: Buffer, base: Int, signed: Bool)
- # Fill `s' with the digits in base 'base' of `self' (and with the '-' sign if 'signed' and negative).
+ # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
# assume < to_c max const of char
+ fun fill_buffer(s: Buffer, base: Int, signed: Bool)
do
var n: Int
# Sign
return str
end
- # `self' representation with `nb' digits after the '.'.
+ # `self` representation with `nb` digits after the '.'.
fun to_precision(nb: Int): String
do
if nb == 0 then return self.to_i.to_s
return s.to_s
end
- # Concatenate and separate each elements with `sep'.
+ # Concatenate and separate each elements with `sep`.
fun join(sep: String): String
do
if is_empty then return ""
redef class Map[K,V]
# Concatenate couple of 'key value'.
- # key and value are separated by 'couple_sep'.
- # each couple is separated each couple with `sep'.
+ # key and value are separated by `couple_sep`.
+ # each couple is separated each couple with `sep`.
fun join(sep: String, couple_sep: String): String
do
if is_empty then return ""
return new String.from_cstring(native_argv(0))
end
- # Initialize `args' with the contents of `native_argc' and `native_argv'.
+ # Initialize `args` with the contents of `native_argc` and `native_argv`.
private fun init_args
do
var argc = native_argc
import string
-# Patterns are abstract string motifs (include `String' and `Char').
+# Patterns are abstract string motifs (include `String` and `Char`).
interface Pattern
- # Search `self' into `s' from a certain position.
+ # Search `self` into `s` from a certain position.
# Return the position of the first character of the matching section.
# Return -1 if not found.
fun search_index_in(s: String, from: Int): Int is abstract
- # Search `self' into `s' from a certain position.
+ # Search `self` into `s` from a certain position.
# Return null if not found.
fun search_in(s: String, from: Int): nullable Match is abstract
- # Search all `self' occurrences into `s'.
+ # Search all `self` occurrences into `s`.
fun search_all_in(s: String): Array[Match]
do
var res = new Array[Match] # Result
return res
end
- # Split `s' using `self' is separator.
+ # Split `s` using `self` is separator.
fun split_in(s: String): Array[Match]
do
var res = new Array[Match] # Result
redef fun to_s do return _motif
- # boyer-moore search gives the position of the first occurrence of a pattern starting at position `from'
+ # boyer-moore search gives the position of the first occurrence of a pattern starting at position `from`
redef fun search_index_in(s, from)
do
assert from >= 0
return -1 # found nothing
end
- # boyer-moore search. Return null if not found
+ # boyer-moore search. Return null if not found
redef fun search_in(s, from)
do
var to = search_index_in(s, from)
end
end
- # Compile a new motif
+ # Compile a new motif
init(motif: String)
do
_motif = motif
# The contents of the matching part
redef fun to_s do return _string.substring(_from, _length)
- # Matches `len' characters of `s' from `f'.
+ # Matches `len` characters of `s` from `f`.
init(s: String, f: Int, len: Int)
do
assert positive_length: len >= 0
end
end
- # Like `search_from' but from the first character.
+ # Like `search_from` but from the first character.
fun search(p: Pattern): nullable Match do return p.search_in(self, 0)
# Search the given pattern into self from a.
- # The search starts at `from'.
+ # The search starts at `from`.
# Return null if not found.
fun search_from(p: Pattern, from: Int): nullable Match do return p.search_in(self, from)
# Search all occurrences of p into self.
#
- # var a = new Array[Int]
- # for i in "hello world".searches('o') do
- # a.add(i.from)
- # end
- # a # -> [4, 7]
+ # var a = new Array[Int]
+ # for i in "hello world".search_all('o') do
+ # a.add(i.from)
+ # end
+ # a # -> [4, 7]
fun search_all(p: Pattern): Array[Match] do return p.search_all_in(self)
# Split `self` using `p` as separator.
- # "hello world".split('o') # -> ["hell", " w", "rld"]
+ #
+ # "hello world".split('o') # -> ["hell", " w", "rld"]
fun split(p: Pattern): Array[String]
do
var matches = p.split_in(self)
# Replace all occurences of a pattern with a string
#
- # "hlelo".replace("le", "el") # -> "hello"
- # "hello".replace('l', "") # -> "heo"
+ # "hlelo".replace("le", "el") # -> "hello"
+ # "hello".replace('l', "") # -> "heo"
fun replace(p: Pattern, string: String): String
do
return self.split_with(p).join(string)
# Escape the four characters < > & and " with their html counterpart
#
- # "a&b->\"x\"".html_escape # -> "a&b->"x""
+ # "a&b->\"x\"".html_escape # -> "a&b->"x""
fun html_escape: String
do
var ret = self