#
# This file is free software, which comes along with NIT. This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. You can modify it is you want, provided this header
# is kept unaltered, and a notification of the changes is added.
# You are allowed to redistribute it and sell it, alone or is a part of
# Basic manipulations of strings of characters
module string
+import math
intrude import collection # FIXME should be collection::array
`{
redef fun length do return target.length
+ redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
+
+ fun iterator_from(pos: Int): IndexedIterator[Char] is abstract
+
redef fun has(c: Char): Bool
do
for i in self do
return target._items[index + target._index_from]
end
- redef fun iterator: IndexedIterator[Char] do return new FlatStringIterator.with_pos(target, 0)
+ redef fun iterator_from(start) do return new FlatStringIterator.with_pos(target, start)
end
redef type SELFTYPE: Buffer
- init(tgt: Buffer)
- do
- self.target = tgt
- end
-
redef fun [](index) do return target._items[index]
redef fun []=(index, item)
if target.capacity < s.length then enlarge(s_length + target.length)
end
- redef fun iterator: IndexedIterator[Char] do return new FlatBufferIterator.with_pos(target, 0)
+ redef fun iterator_from(pos) do return new FlatBufferIterator.with_pos(target, pos)
end
# assert true.to_s == "true"
# assert false.to_s == "false"
redef fun to_s
- do
- if self then
- return once "true"
- else
- return once "false"
+ do
+ if self then
+ return once "true"
+ else
+ return once "false"
end
- end
+ end
end
redef class Int
end
# Fill digits
var pos = digit_count(base) - 1
- while pos >= 0 and n > 0 do
+ while pos >= 0 and n > 0 do
s.chars[pos] = (n % base).to_c
n = n / base # /
pos -= 1
# Pretty print self, print needoed decimals up to a max of 3.
redef fun to_s do
var str = to_precision( 3 )
+ if is_inf != 0 or is_nan then return str
var len = str.length
for i in [0..len-1] do
var j = len-1-i
# `self` representation with `nb` digits after the '.'.
fun to_precision(nb: Int): String
do
+ if is_nan then return "nan"
+
+ var isinf = self.is_inf
+ if isinf == 1 then
+ return "inf"
+ else if isinf == -1 then
+ return "-inf"
+ end
+
if nb == 0 then return self.to_i.to_s
var f = self
for i in [0..nb[ do f = f * 10.0
fun join(sep: String): String
do
if is_empty then return ""
-
+
var s = new Buffer # Result
# Concat first item
var i = iterator
var e = i.item
if e != null then s.append(e.to_s)
-
+
# Concat other items
i.next
while i.is_ok do
fun join(sep: String, couple_sep: String): String
do
if is_empty then return ""
-
+
var s = new Buffer # Result
# Concat first item