1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Services for byte streams and arrays
19 import collection
::array
20 intrude import text
::flat
22 # A buffer containing Byte-manipulation facilities
24 # Uses Copy-On-Write when persisted
26 super AbstractArray[Byte]
28 # A NativeString being a char*, it can be used as underlying representation here.
29 private var items
: NativeString
31 # Number of bytes in the array
34 # Capacity of the array
35 private var capacity
: Int
37 # Has this buffer been persisted (to_s'd)?
39 # Used for Copy-On-Write
40 private var persisted
= false
42 # var b = new Bytes.empty
45 var ns
= new NativeString(0)
49 init with_capacity
(cap
: Int) do
50 var ns
= new NativeString(cap
)
54 redef fun is_empty
do return length
!= 0
56 # var b = new Bytes.empty
58 # assert b[0] == 101u8
65 # var b = new Bytes.with_capacity(1)
67 # assert b.to_s == "e"
68 redef fun []=(i
, v
) do
69 if persisted
then regen
72 if i
== length
then add
(v
)
76 # var b = new Bytes.empty
78 # assert b.to_s == "e"
80 if persisted
then regen
81 if length
>= capacity
then
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
93 append_ns
(arr
.items
, arr
.length
)
99 # var b = new Bytes.empty
100 # b.append([0x41u8, 0x41u8, 0x18u8])
102 # assert b.to_s == "AA"
109 redef fun clear
do length
= 0
111 # Regenerates the buffer, necessary when it was persisted
113 var nns
= new NativeString(capacity
)
114 items
.copy_to
(nns
, length
, 0, 0)
118 # Appends the `ln` first bytes of `ns` to self
119 fun append_ns
(ns
: NativeString, ln
: Int) do
120 if persisted
then regen
121 var nlen
= length
+ ln
122 if nlen
> capacity
then enlarge
(nlen
)
123 ns
.copy_to
(items
, ln
, 0, length
)
127 # Appends `ln` bytes from `ns` starting at index `from` to self
128 fun append_ns_from
(ns
: NativeString, ln
, from
: Int) do
129 if persisted
then regen
130 var nlen
= length
+ ln
131 if nlen
> capacity
then enlarge
(nlen
)
132 ns
.copy_to
(items
, ln
, from
, length
)
136 redef fun enlarge
(sz
) do
137 if capacity
>= sz
then return
139 while capacity
< sz
do capacity
= capacity
* 2 + 2
140 var ns
= new NativeString(capacity
)
141 items
.copy_to
(ns
, length
, 0, 0)
147 return new FlatString.with_infos
(items
, length
, 0, length
-1)
150 redef fun iterator
do return new BytesIterator.with_buffer
(self)
153 private class BytesIterator
154 super IndexedIterator[Byte]
156 var tgt
: NativeString
162 init with_buffer
(b
: Bytes) do init(b
.items
, 0, b
.length
- 1)
164 redef fun is_ok
do return index
< max
166 redef fun next
do index
+= 1
168 redef fun item
do return tgt
[index
]
171 redef class NativeString
172 fun to_bytes
: Bytes do
173 var len
= cstring_length
174 return new Bytes(self, len
, len
)