lib/standard: the single parameter of `Collection::join` is optional
[nit.git] / lib / standard / bytes.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Services for byte streams and arrays
16 module bytes
17
18 import kernel
19 import collection::array
20 intrude import text::flat
21
22 # A buffer containing Byte-manipulation facilities
23 #
24 # Uses Copy-On-Write when persisted
25 class Bytes
26 super AbstractArray[Byte]
27
28 # A NativeString being a char*, it can be used as underlying representation here.
29 private var items: NativeString
30
31 # Number of bytes in the array
32 redef var length
33
34 # Capacity of the array
35 private var capacity: Int
36
37 # Has this buffer been persisted (to_s'd)?
38 #
39 # Used for Copy-On-Write
40 private var persisted = false
41
42 # var b = new Bytes.empty
43 # assert b.to_s == ""
44 init empty do
45 var ns = new NativeString(0)
46 init(ns, 0, 0)
47 end
48
49 init with_capacity(cap: Int) do
50 var ns = new NativeString(cap)
51 init(ns, 0, cap)
52 end
53
54 redef fun is_empty do return length != 0
55
56 # var b = new Bytes.empty
57 # b.add 101u8
58 # assert b[0] == 101u8
59 redef fun [](i) do
60 assert i >= 0
61 assert i < length
62 return items[i]
63 end
64
65 # var b = new Bytes.with_capacity(1)
66 # b[0] = 101u8
67 # assert b.to_s == "e"
68 redef fun []=(i, v) do
69 if persisted then regen
70 assert i >= 0
71 assert i <= length
72 if i == length then add(v)
73 items[i] = v
74 end
75
76 # var b = new Bytes.empty
77 # b.add 101u8
78 # assert b.to_s == "e"
79 redef fun add(c) do
80 if persisted then regen
81 if length >= capacity then
82 enlarge(length)
83 end
84 items[length] = c
85 length += 1
86 end
87
88 # var b = new Bytes.empty
89 # b.append([104u8, 101u8, 108u8, 108u8, 111u8])
90 # assert b.to_s == "hello"
91 redef fun append(arr) do
92 if arr isa Bytes then
93 append_ns(arr.items, arr.length)
94 else
95 for i in arr do add i
96 end
97 end
98
99 redef fun clear do length = 0
100
101 # Regenerates the buffer, necessary when it was persisted
102 private fun regen do
103 var nns = new NativeString(capacity)
104 items.copy_to(nns, length, 0, 0)
105 persisted = false
106 end
107
108 # Appends the `ln` first bytes of `ns` to self
109 fun append_ns(ns: NativeString, ln: Int) do
110 if persisted then regen
111 var nlen = length + ln
112 if nlen > capacity then enlarge(nlen)
113 ns.copy_to(items, ln, 0, length)
114 length += ln
115 end
116
117 # Appends `ln` bytes from `ns` starting at index `from` to self
118 fun append_ns_from(ns: NativeString, ln, from: Int) do
119 if persisted then regen
120 var nlen = length + ln
121 if nlen > capacity then enlarge(nlen)
122 ns.copy_to(items, ln, from, length)
123 length += ln
124 end
125
126 redef fun enlarge(sz) do
127 if capacity >= sz then return
128 persisted = false
129 while capacity < sz do capacity = capacity * 2 + 2
130 var ns = new NativeString(capacity)
131 items.copy_to(ns, length, 0, 0)
132 items = ns
133 end
134
135 redef fun to_s do
136 persisted = true
137 return new FlatString.with_infos(items, length, 0, length -1)
138 end
139
140 redef fun iterator do return new BytesIterator.with_buffer(self)
141 end
142
143 private class BytesIterator
144 super IndexedIterator[Byte]
145
146 var tgt: NativeString
147
148 redef var index
149
150 var max: Int
151
152 init with_buffer(b: Bytes) do init(b.items, 0, b.length - 1)
153
154 redef fun is_ok do return index < max
155
156 redef fun next do index += 1
157
158 redef fun item do return tgt[index]
159 end
160
161 redef class NativeString
162 fun to_bytes: Bytes do
163 var len = cstring_length
164 return new Bytes(self, len, len)
165 end
166 end