60f4196e6a8fc67adfa257725a05ebf9f61bb5c4
[nit.git] / benchmarks / strings / array_to_s_vars / array_to_s_flatstr.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Implementation of Array::to_s with FlatStrings exclusively
12 #
13 # To be used as a Mixin at compile-time for benchmarking purposes.
14 module array_to_s_flatstr
15
16 intrude import standard::text::flat
17
18 redef class FlatString
19 redef fun +(o) do
20 var mlen = length
21 var slen = o.length
22 var nns = new CString(mlen + slen)
23 items.copy_to(nns, mlen, index_from, 0)
24 if o isa FlatString then
25 o.items.copy_to(nns, slen, o.index_from, mlen)
26 else
27 var pos = mlen
28 for i in o.chars do
29 nns[pos] = i
30 pos += 1
31 end
32 end
33 return nns.to_s_with_length(mlen)
34 end
35 end
36
37 redef class Array[E]
38
39 redef fun to_s do
40 var i = 1
41 var l = length
42 if l == 0 then return ""
43 var its = _items
44 var s = its[0].to_s
45 while i < l do
46 var e = its[i]
47 if e != null then s += e.to_s
48 i += 1
49 end
50 return s
51 end
52 end