std.byte_array.ByteArray
class pub builtin ByteArray
An array of bytes.
Byte arrays are arrays specialised for storing individual bytes in the most
efficient way possible. Unlike a regular Array
of Int
values, each value
only requires a single byte of space, instead of requiring 8 bytes of space.
Byte arrays are primarily meant for reading and writing data from/to a stream,
such as a file or a socket. If you simply want to store a list of numbers,
you're better off using the Array
type.
Static methods
filled
Show source codeHide source code
fn pub static filled(with: Int, times: Int) -> ByteArray {
let bytes = new
bytes.resize(times, with)
bytes
}
fn pub static filled(with: Int, times: Int) -> ByteArray
Returns a ByteArray
filled with the given byte.
The times
argument specifies how many times the with
argument must
exist in the byte array.
Examples
let bytes = ByteArray.filled(with: 0, times: 2)
bytes.get(0) # => 0
bytes.get(1) # => 0
from_array
Show source codeHide source code
fn pub static from_array(array: ref Array[Int]) -> ByteArray {
let bytes = ByteArray.new
array.iter.each(fn (v) { bytes.push(v) })
bytes
}
fn pub static from_array(array: ref Array[Int]) -> ByteArray
Returns a new ByteArray
created from the given Array
.
from_pointer
Show source codeHide source code
fn pub static from_pointer(pointer: Pointer[UInt8], size: Int) -> ByteArray {
inko_byte_array_from_pointer(_INKO.state, pointer, size)
}
fn pub static from_pointer(pointer: Pointer[UInt8], size: Int) -> ByteArray
Returns a ByteArray
created from a raw pointer.
The size
argument specifies the number of bytes to read starting at the
given pointer.
The purpose of this method is to allow creating a ByteArray
from a pointer
returned by C code. Avoid using this method for anything else.
new
Show source codeHide source code
fn pub static new -> ByteArray {
inko_byte_array_new(_INKO.state)
}
fn pub static new -> ByteArray
Returns a new empty ByteArray
.
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}
fn pub !=(other: T) -> Bool
Returns true
if self
and the given object are not equal to each other.
==
Show source codeHide source code
fn pub ==(other: ref ByteArray) -> Bool {
inko_byte_array_eq(self, other)
}
fn pub ==(other: ref ByteArray) -> Bool
Returns true
if two ByteArray
objects are equal to each other.
Two ByteArray
objects are considered equal if they have the exact same
values in the exact same order.
Examples
Comparing two ByteArray
objects:
ByteArray.from_array([10]) == ByteArray.from_array([10]) # => true
ByteArray.from_array([10]) == ByteArray.from_array([20]) # => false
append
Show source codeHide source code
fn pub mut append(other: ByteArray) {
inko_byte_array_append(self, other)
}
fn pub mut append(other: ByteArray)
Appends the bytes of the given ByteArray
to self
.
Examples
let a = ByteArray.from_array([10])
let b = ByteArray.from_array([20])
a.append(b)
a # => ByteArray.from_array([10, 20])
byte
Show source codeHide source code
fn pub byte(index: Int) -> Int {
get(index)
}
fn pub byte(index: Int) -> Int
An alias for ByteArray.get
.
bytes
Show source codeHide source code
fn pub bytes -> Stream[Int] {
iter
}
fn pub bytes -> Stream[Int]
An alias for ByteArray.iter
.
clear
Show source codeHide source code
fn pub mut clear {
inko_byte_array_clear(self)
}
fn pub mut clear
Removes all values from this ByteArray
.
Examples
Removing all values:
let bytes = ByteArray.from_array([10, 20, 30])
bytes.clear
bytes.size # => 0
clone
Show source codeHide source code
fn pub clone -> ByteArray {
inko_byte_array_clone(_INKO.state, self)
}
fn pub clone -> ByteArray
Creates a clone of self
.
contains?
Show source codeHide source code
fn pub contains?(value: ref Int) -> Bool {
iter.any?(fn (ours) { ours == value })
}
fn pub contains?(value: ref Int) -> Bool
Returns true
if the given byte is contained in self
.
Examples
let bytes = ByteArray.from_array([10, 20])
bytes.contains?(10) # => true
copy_from
Show source codeHide source code
fn pub mut copy_from(bytes: ref ByteArray, at: Int, size: Int) -> Int {
inko_byte_array_copy_from(self, bytes, at, size)
}
fn pub mut copy_from(bytes: ref ByteArray, at: Int, size: Int) -> Int
Copies up to size
bytes from bytes
into self
, starting at the index
at
.
The return value is the number of bytes copied. This value may be less than
size
if there are fewer bytes in bytes
.
Examples
let a = ByteArray.from_array([1, 2, 3, 4])
let b = ByteArray.new
b.copy_from(a, at: 0, size: 2)
b # => ByteArray.from_array([1, 2])
drain_to_string
Show source codeHide source code
fn pub mut drain_to_string -> String {
inko_byte_array_drain_to_string(_INKO.state, self)
}
fn pub mut drain_to_string -> String
Returns a new String
using the bytes in this ByteArray
, draining it in
the process.
After this method is finished, self
is left empty. This allows one to
convert a temporary ByteArray
into a String
, without requiring the list
of bytes to be allocated twice.
Examples
Draining a ByteArray
into a String
:
let bytes = ByteArray.from_array([105, 110, 107, 111])
bytes.drain_to_string # => 'inko'
bytes.empty? # => true
empty?
Show source codeHide source code
fn pub empty? -> Bool {
size == 0
}
fn pub empty? -> Bool
Returns true
if self
is empty.
Examples
ByteArray.new.empty? => true
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
let fmt = formatter.array
iter.each(fn (byte) { fmt.value(byte) })
fmt.finish
}
fn pub fmt(formatter: mut Formatter)
Formats self
in a human-readable format for debugging purposes.
get
Show source codeHide source code
fn pub get(index: Int) -> Int {
bounds_check(index, size)
inko_byte_array_get(self, index)
}
fn pub get(index: Int) -> Int
Returns the byte at the given index.
Panics
This method panics if the index is out of bounds.
Examples
Retrieving an existing byte:
let bytes = ByteArray.from_array([10, 20])
bytes.get(0) # => 10
hash
Show source codeHide source code
fn pub hash[H: mut + Hasher](hasher: mut H) {
iter.each(fn (v) { hasher.write(v) })
}
fn pub hash[H: mut + Hasher](hasher: mut H: mut)
Writes the hash for self
into the given Hasher
.
into_byte_array
Show source codeHide source code
fn pub move into_byte_array -> ByteArray {
self
}
fn pub move into_byte_array -> ByteArray
into_string
Show source codeHide source code
fn pub move into_string -> String {
drain_to_string
}
fn pub move into_string -> String
Moves self
into a String
.
iter
Show source codeHide source code
fn pub iter -> Stream[Int] {
let mut idx = 0
let max = size
Stream.new(fn move {
if idx < max {
Option.Some(inko_byte_array_get(self, idx := idx + 1))
} else {
Option.None
}
})
}
fn pub iter -> Stream[Int]
Returns an iterator over the bytes in self
.
last
Show source codeHide source code
fn pub last -> Option[Int] {
opt(size - 1)
}
fn pub last -> Option[Int]
Returns the last byte in self
Examples
ByteArray.new.last # => Option.None
ByteArray.from_array([10, 20]).last # => Option.Some(20)
opt
Show source codeHide source code
fn pub opt(index: Int) -> Option[Int] {
if index < 0 or index >= size { return Option.None }
Option.Some(inko_byte_array_get(self, index))
}
fn pub opt(index: Int) -> Option[Int]
Returns the byte at the given index, returning None if the index is out of bounds.
Examples
Retrieving an existing byte:
let bytes = ByteArray.from_array([10, 20])
bytes.opt(0) # => Option.Some(10)
Retrieving a non-existing byte:
let bytes = ByteArray.from_array([10, 20])
bytes.opt(5) # => Option.None
pop
Show source codeHide source code
fn pub mut pop -> Option[Int] {
match inko_byte_array_pop(self) {
case -1 -> Option.None
case val -> Option.Some(val)
}
}
fn pub mut pop -> Option[Int]
Removes a value from the back of the ByteArray
, returning the removed
value.
If no value was found, a None is returned instead.
Examples
Popping an existing value:
let bytes = ByteArray.from_array([10])
bytes.pop # => Option.Some(10)
bytes.size # => 0
Popping a value when the ByteArray
is empty:
let bytes = ByteArray.new
bytes.pop # => Option.None
push
Show source codeHide source code
fn pub mut push(value: Int) {
inko_byte_array_push(self, value)
_INKO.moved(value)
}
fn pub mut push(value: Int)
Pushes a value to the back of the ByteArray
, returning the pushed value.
Examples
Pushing a value into a ByteArray
:
let bytes = ByteArray.new
bytes.push(10) # => 10
bytes.size # => 1
remove_at
Show source codeHide source code
fn pub mut remove_at(index: Int) -> Int {
bounds_check(index, size)
inko_byte_array_remove(self, index)
}
fn pub mut remove_at(index: Int) -> Int
Removes the value at the given index, returning the removed value.
Examples
Removing an existing value:
let bytes = ByteArray.from_array([10])
bytes.remove_at(0) # => 10
bytes.size # => 0
Panics
This method panics if the index is out of bounds.
resize
Show source codeHide source code
fn pub mut resize(size: Int, value: Int) {
if size < 0 { panic('The new size must be greater than zero') }
inko_byte_array_resize(self, size, value)
}
fn pub mut resize(size: Int, value: Int)
Resizes self
to the new size.
If the given size is greater than the current size, the value
argument is
used to fill in the additional slots. If the given size is less than the
current size, self
is simply truncated.
Panics
This method panics if the given size is less than zero.
Examples
let bytes = ByteArray.new
bytes.resize(size: 2, value: 1)
bytes # => ByteArray.from_array([1, 1])
bytes.resize(size: 0, value: 0)
bytes # => ByteArray.new
reverse
Show source codeHide source code
fn pub mut reverse {
let mut a = 0
let mut b = size - 1
while a < b {
let a_val = inko_byte_array_get(self, a)
inko_byte_array_set(self, a, inko_byte_array_set(self, b, a_val))
a += 1
b -= 1
}
}
fn pub mut reverse
Reverses self
in-place
Examples
let a = ByteArray.from_array([10, 20, 30])
a.reverse
a # => ByteArray.from_array([30, 20, 10])
set
Show source codeHide source code
fn pub mut set(index: Int, value: Int) {
bounds_check(index, size)
inko_byte_array_set(self, index, value)
_INKO.moved(value)
}
fn pub mut set(index: Int, value: Int)
Stores a byte at the given index, then returns it.
Panics
This method panics if the index is out of bounds.
Examples
Setting the value of an existing index:
let bytes = ByteArray.from_array([10, 20])
bytes.set(0, 30)
bytes.get(0) # => 30
size
Show source codeHide source code
fn pub size -> Int {
inko_byte_array_size(self)
}
fn pub size -> Int
Returns the number of bytes in this ByteArray
.
Examples
Obtaining the size of a ByteArray
ByteArray.new.size # => 0
ByteArray.from_array([10]).size # => 1
slice
Show source codeHide source code
fn pub slice(start: Int, size: Int) -> ByteArray {
bounds_check(start, self.size)
inko_byte_array_slice(_INKO.state, self, start, size)
}
fn pub slice(start: Int, size: Int) -> ByteArray
Slices self
into a new ByteArray
.
Similar to slicing a String
, slicing a ByteArray
allows one to extract
a sub-array by providing a start position and the number of bytes to
include starting at the start position.
Examples
Slicing a ByteArray
:
let bytes = ByteArray.from_array([1, 2, 3, 4])
let sliced = bytes.slice(start: 1, size: 2)
sliced.get(0) # => 2
sliced.get(1) # => 3
to_array
Show source codeHide source code
fn pub to_array -> Array[Int] {
iter.to_array
}
fn pub to_array -> Array[Int]
Converts the ByteArray
to an Array!(Int)
.
Examples
Converting a ByteArray
:
let bytes = ByteArray.from_array([105, 110, 107, 111])
bytes.to_array # => [105, 110, 107, 111]
to_byte_array
Show source codeHide source code
fn pub to_byte_array -> ByteArray {
clone
}
fn pub to_byte_array -> ByteArray
to_pointer
Show source codeHide source code
fn pub to_pointer -> Pointer[UInt8] {
inko_byte_array_to_pointer(self)
}
fn pub to_pointer -> Pointer[UInt8]
Returns a raw pointer to the bytes of self
.
This method is meant to be used when passing byte arrays to foreign functions. You should avoid using it for anything else.
to_string
Show source codeHide source code
fn pub to_string -> String {
inko_byte_array_to_string(_INKO.state, self)
}
fn pub to_string -> String
Returns a new String
using the bytes in this ByteArray
.
Any invalid UTF-8 sequences will be replaced with U+FFFD REPLACEMENT
CHARACTER
, which looks like this: �
Examples
Converting a ByteArray
into a String
:
let bytes = ByteArray.from_array([105, 110, 107, 111])
bytes.to_string # => 'inko'
Implemented traits
IntoByteArray
impl IntoByteArray for ByteArray
ToByteArray
impl ToByteArray for ByteArray
Clone
impl Clone[ByteArray] for ByteArray
Contains
impl Contains[Int] for ByteArray
Equal
impl Equal[ref ByteArray] for ByteArray
Drop
impl Drop for ByteArray
Format
impl Format for ByteArray
Hash
impl Hash for ByteArray
Bytes
impl Bytes for ByteArray
IntoString
impl IntoString for ByteArray
ToString
impl ToString for ByteArray