std.bytes.Bytes
trait pub Bytes
A type that is a contiguous sequence of bytes.
This trait is used for providing various foundational methods for Inko's
different byte sequence types, such as String
, ByteArray
and Slice
.
Required methods
get
Show source codeHide source code
fn pub get(index: Int) -> Result[Int, OutOfBounds]
fn pub get(index: Int) -> Result[Int, OutOfBounds]
Returns the byte at the given index.
If the index is out of bounds, a std.bounds.OutOfBounds
error is returned.
pointer
Show source codeHide source code
fn pub pointer -> Pointer[UInt8]
fn pub pointer -> Pointer[UInt8]
Returns a raw pointer to the bytes of self
This method is meant for FFI purposes, and use of it should be avoided at all costs.
size
Show source codeHide source code
fn pub size -> Int
fn pub size -> Int
Returns the number of bytes in self
.
Default methods
bytes
Show source codeHide source code
fn pub bytes -> Iter[ref Self] {
Iter(source: self, index: 0)
}
fn pub bytes -> Iter[ref Self]
Returns an iterator over the bytes in self
.
Examples
let bytes = ByteArray.from_array([10, 20, 30])
let iter = bytes.bytes
iter.next # => Option.Some(10)
contains_bytes?
Show source codeHide source code
fn pub contains_bytes?[B: Bytes](bytes: ref B) -> Bool {
index_of(bytes, starting_at: 0).some?
}
fn pub contains_bytes?[B: Bytes](bytes: ref B) -> Bool
Returns true
if self
contains the given byte sequence.
Examples
'hello'.contains_bytes?('hello') # => true
ends_with?
Show source codeHide source code
fn pub ends_with?[T: Bytes](suffix: ref T) -> Bool {
ptr.ends_with?(pointer, size, suffix.pointer, suffix.size)
}
fn pub ends_with?[T: Bytes](suffix: ref T) -> Bool
Returns true
if self
ends with the given sequence of bytes.
Examples
'hello'.ends_with?('o'.to_byte_array) # => true
'hello'.ends_with?('o') # => true
equals?
Show source codeHide source code
fn pub equals?[O: Bytes](other: ref O) -> Bool {
let len = size
len == other.size and (len == 0 or ptr.equal(pointer, other.pointer, len))
}
fn pub equals?[O: Bytes](other: ref O) -> Bool
Returns true
if self
and other
contain identical bytes.
This allows comparing of any Bytes
type with any other Bytes
type (e.g.
comparing a String
to a ByteArray
). In contrast, methods such as
String.==
only support comparing against values of the same type.
Examples
'hello'.equals?('hello') # => true
'hello'.equals?('hello'.to_byte_array) # => true
equals_while_ignoring_case?
Show source codeHide source code
fn pub equals_while_ignoring_case?[O: Bytes](other: ref O) -> Bool {
let len = size
if len == 0 { return true }
if len != other.size { return false }
let lhs = pointer
let rhs = other.pointer
let mut idx = 0
# Since we have to convert ASCII bytes to lowercase (when necessary) we
# can't use a more efficient comparison and instead have to compare the data
# byte by byte.
while idx < len {
let mut a = ptr.add(lhs, idx).0 as Int
let mut b = ptr.add(rhs, idx).0 as Int
if upper?(a) { a = to_lower(a) }
if upper?(b) { b = to_lower(b) }
if a != b { return false }
idx += 1
}
true
}
fn pub equals_while_ignoring_case?[O: Bytes](other: ref O) -> Bool
Returns true
if self
and other
contain identical bytes, using an ASCII
case-insensitive comparison.
This method only performs case-insensitive matching for characters in the ASCII range, meaning that "foo" and "FOO" are considered identical but "á" and "Á" are considered different.
Examples
'foo'.equals_while_ignoring_case?('foo') # => true
'foo'.equals_while_ignoring_case?('FOO') # => true
'abc'.equals_while_ignoring_case?('def') # => false
'á'.equals_while_ignoring_case?('Á') # => false
index_of
Show source codeHide source code
fn pub index_of[B: Bytes](value: ref B, starting_at: Int) -> Option[Int] {
# This is a naive string searching algorithm (see
# https://en.wikipedia.org/wiki/String-searching_algorithm) for more details
# on the various algorithms.
#
# We're using the naive algorithm because:
#
# 1. It's easy to implement
# 2. It doesn't require any pre-processing
# 3. At the time of writing there was no need for something more performant
let find_size = value.size
if find_size == 0 or size == 0 or find_size > size { return Option.None }
let mut a = starting_at
let max = size - find_size
while a <= max {
let mut b = 0
while b < find_size and get(a + b) == value.get(b) { b += 1 }
if b == find_size { return Option.Some(a) }
a += 1
}
Option.None
}
fn pub index_of[B: Bytes](value: ref B, starting_at: Int) -> Option[Int]
Returns the byte index of the first occurrence of the given sequence of bytes, starting at the given byte index.
Examples
'hello'.index_of('h', starting_at: 0) # => Option.Some(0)
'hello'.index_of('l', starting_at: 0) # => Option.Some(2)
'hello'.index_of('l', starting_at: 3) # => Option.Some(3)
'hello'.index_of('x', starting_at: 0) # => Option.None
starts_with?
Show source codeHide source code
fn pub starts_with?[T: Bytes](prefix: ref T) -> Bool {
ptr.starts_with?(pointer, size, prefix.pointer, prefix.size)
}
fn pub starts_with?[T: Bytes](prefix: ref T) -> Bool
Returns true
if self
starts with the given sequence of bytes.
Examples
'hello'.starts_with?('h'.to_byte_array) # => true
'hello'.starts_with?('h') # => true
Implementations
ByteArray
impl Bytes for ByteArray
Slice
impl Bytes for Slice
String
impl Bytes for String