Skip to content

Commit

Permalink
Add Bytes.opt to get an optional byte
Browse files Browse the repository at this point in the history
This method was already defined for the ByteArray type, but now it's
also available on the String type.

Changelog: added
  • Loading branch information
yorickpeterse committed Nov 4, 2024
1 parent 1989377 commit 8c29216
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
52 changes: 26 additions & 26 deletions std/src/std/byte_array.inko
Original file line number Diff line number Diff line change
Expand Up @@ -308,32 +308,6 @@ class builtin ByteArray {
inko_byte_array_drain_to_string(_INKO.state, self)
}

# Returns the byte at the given index, returning None if the index is out of
# bounds.
#
# # Examples
#
# Retrieving an existing byte:
#
# ```inko
# let bytes = ByteArray.from_array([10, 20])
#
# bytes.opt(0) # => Option.Some(10)
# ```
#
# Retrieving a non-existing byte:
#
# ```inko
# let bytes = ByteArray.from_array([10, 20])
#
# bytes.opt(5) # => Option.None
# ```
fn pub opt(index: Int) -> Option[Int] {
if index < 0 or index >= size { return Option.None }

Option.Some(inko_byte_array_get(self, index))
}

# Returns the byte at the given index.
#
# # Panics
Expand Down Expand Up @@ -556,6 +530,32 @@ class builtin ByteArray {
}

impl Bytes for ByteArray {
# Returns the byte at the given index, returning None if the index is out of
# bounds.
#
# # Examples
#
# Retrieving an existing byte:
#
# ```inko
# let bytes = ByteArray.from_array([10, 20])
#
# bytes.opt(0) # => Option.Some(10)
# ```
#
# Retrieving a non-existing byte:
#
# ```inko
# let bytes = ByteArray.from_array([10, 20])
#
# bytes.opt(5) # => Option.None
# ```
fn pub opt(index: Int) -> Option[Int] {
if index < 0 or index >= size { return Option.None }

Option.Some(inko_byte_array_get(self, index))
}

# An alias for `ByteArray.get`.
fn pub byte(index: Int) -> Int {
get(index)
Expand Down
19 changes: 19 additions & 0 deletions std/src/std/string.inko
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ trait pub ToString {
# (e.g. by iterating over them), but don't care if the input is a `String` or
# `ByteArray`.
trait pub Bytes {
# Returns the byte at the given index, returning an `Option.None` if the index
# is out of bounds.
fn pub opt(index: Int) -> Option[Int]

# Returns the byte at the given byte index.
#
# # Panics
Expand Down Expand Up @@ -677,6 +681,21 @@ class builtin String {
}

impl Bytes for String {
# Returns the byte at the given index, returning None if the index is out of
# bounds.
#
# # Examples
#
# ```inko
# 'abc'.opt(0) # => Option.Some(97)
# 'abc'.opt(10) # => Option.None
# ```
fn pub opt(index: Int) -> Option[Int] {
if index < 0 or index >= size { return Option.None }

Option.Some(byte_unchecked(index))
}

# Returns the byte at the given byte index.
#
# # Examples
Expand Down
5 changes: 5 additions & 0 deletions std/test/std/test_string.inko
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ fn pub tests(t: mut Tests) {
t.equal('AÄ'.size, 3)
})

t.test('String.opt', fn (t) {
t.equal('foo'.opt(0), Option.Some(102))
t.equal('foo'.opt(10), Option.None)
})

t.test('String.byte', fn (t) {
t.equal('foo'.byte(0), 102)
t.equal('foo'.byte(1), 111)
Expand Down

0 comments on commit 8c29216

Please sign in to comment.