332efc2f3264ec24a6379631a69ea3a7ff7f1581
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 a `Bytes` with capacity `cap`
50 init with_capacity
(cap
: Int) do
51 var ns
= new NativeString(cap
)
55 redef fun is_empty
do return length
!= 0
57 # var b = new Bytes.empty
59 # assert b[0] == 101u8
66 # var b = new Bytes.with_capacity(1)
68 # assert b.to_s == "e"
69 redef fun []=(i
, v
) do
70 if persisted
then regen
73 if i
== length
then add
(v
)
77 # var b = new Bytes.empty
79 # assert b.to_s == "e"
81 if persisted
then regen
82 if length
>= capacity
then
89 # var b = new Bytes.empty
90 # b.append([104u8, 101u8, 108u8, 108u8, 111u8])
91 # assert b.to_s == "hello"
92 redef fun append
(arr
) do
94 append_ns
(arr
.items
, arr
.length
)
100 # var b = new Bytes.empty
101 # b.append([0x41u8, 0x41u8, 0x18u8])
103 # assert b.to_s == "AA"
110 redef fun clear
do length
= 0
112 # Regenerates the buffer, necessary when it was persisted
114 var nns
= new NativeString(capacity
)
115 items
.copy_to
(nns
, length
, 0, 0)
119 # Appends the `ln` first bytes of `ns` to self
120 fun append_ns
(ns
: NativeString, ln
: Int) do
121 if persisted
then regen
122 var nlen
= length
+ ln
123 if nlen
> capacity
then enlarge
(nlen
)
124 ns
.copy_to
(items
, ln
, 0, length
)
128 # Appends `ln` bytes from `ns` starting at index `from` to self
129 fun append_ns_from
(ns
: NativeString, ln
, from
: Int) do
130 if persisted
then regen
131 var nlen
= length
+ ln
132 if nlen
> capacity
then enlarge
(nlen
)
133 ns
.copy_to
(items
, ln
, from
, length
)
137 redef fun enlarge
(sz
) do
138 if capacity
>= sz
then return
140 while capacity
< sz
do capacity
= capacity
* 2 + 2
141 var ns
= new NativeString(capacity
)
142 items
.copy_to
(ns
, length
, 0, 0)
149 var r
= b
.items
.to_s_with_length
(length
)
150 if r
!= items
then persisted
= false
154 redef fun iterator
do return new BytesIterator.with_buffer
(self)
158 private class BytesIterator
159 super IndexedIterator[Byte]
161 var tgt
: NativeString
167 init with_buffer
(b
: Bytes) do init(b
.items
, 0, b
.length
- 1)
169 redef fun is_ok
do return index
< max
171 redef fun next
do index
+= 1
173 redef fun item
do return tgt
[index
]
177 # Returns a mutable copy of `self`'s bytes
180 # assert "String".to_bytes isa Bytes
181 # assert "String".to_bytes == [83u8, 116u8, 114u8, 105u8, 110u8, 103u8]
183 fun to_bytes
: Bytes do
184 var b
= new Bytes.with_capacity
(bytelen
)
189 # Appends `self.bytes` to `b`
190 fun append_to_bytes
(b
: Bytes) do
191 for s
in substrings
do
192 var from
= if s
isa FlatString then s
.first_byte
else 0
193 b
.append_ns_from
(s
.items
, s
.bytelen
, from
)
199 redef fun append_to_bytes
(b
) do
200 var from
= if self isa FlatString then first_byte
else 0
201 b
.append_ns_from
(items
, bytelen
, from
)
205 redef class NativeString
206 # Creates a new `Bytes` object from `self` with `strlen` as length
207 fun to_bytes
: Bytes do
208 var len
= cstring_length
209 return new Bytes(self, len
, len
)