ec8132237333745e15ff524e5ecb1ad5df4c47ae
[nit.git] / benchmarks / strings / array_to_s_vars / array_to_s_man_buf.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 manual FlatBuffer (precalc its capacity)
12 #
13 # To be used as a Mixin at compile-time for benchmarking purposes.
14 module array_to_s_man_buf
15
16 redef class NativeArray[E]
17 new(length: Int) is intern
18 end
19
20 redef class Array[E]
21 redef fun to_s: String do
22 var l = length
23 var its = _items
24 var na = new NativeArray[String](l)
25 var i = 0
26 var sl = 0
27 while i < l do
28 var tmp = its[i].to_s
29 sl += tmp.length
30 na[i] = tmp
31 i += 1
32 end
33 var ns = new FlatBuffer.with_capacity(sl)
34 i = 0
35 while i < l do
36 ns.append na[i]
37 i += 1
38 end
39 return ns.to_s
40 end
41 end