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 redef fun clear
do length
= 0
101 # Regenerates the buffer, necessary when it was persisted
103 var nns
= new NativeString(capacity
)
104 items
.copy_to
(nns
, length
, 0, 0)
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
)
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
)
126 redef fun enlarge
(sz
) do
127 if capacity
>= sz
then return
129 while capacity
< sz
do capacity
= capacity
* 2 + 2
130 var ns
= new NativeString(capacity
)
131 items
.copy_to
(ns
, length
, 0, 0)
137 return new FlatString.with_infos
(items
, length
, 0, length
-1)
140 redef fun iterator
do return new BytesIterator.with_buffer
(self)
143 private class BytesIterator
144 super IndexedIterator[Byte]
146 var tgt
: NativeString
152 init with_buffer
(b
: Bytes) do init(b
.items
, 0, b
.length
- 1)
154 redef fun is_ok
do return index
< max
156 redef fun next
do index
+= 1
158 redef fun item
do return tgt
[index
]
161 redef class NativeString
162 fun to_bytes
: Bytes do
163 var len
= cstring_length
164 return new Bytes(self, len
, len
)