From 8c29216b47fbfa9a87c7319b7850b3661521186c Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 4 Nov 2024 01:41:25 +0100 Subject: [PATCH] Add Bytes.opt to get an optional byte This method was already defined for the ByteArray type, but now it's also available on the String type. Changelog: added --- std/src/std/byte_array.inko | 52 +++++++++++++++++------------------ std/src/std/string.inko | 19 +++++++++++++ std/test/std/test_string.inko | 5 ++++ 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/std/src/std/byte_array.inko b/std/src/std/byte_array.inko index 1d45db172..b51d0cb27 100644 --- a/std/src/std/byte_array.inko +++ b/std/src/std/byte_array.inko @@ -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 @@ -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) diff --git a/std/src/std/string.inko b/std/src/std/string.inko index 3c9c312b9..ac6bbe99a 100644 --- a/std/src/std/string.inko +++ b/std/src/std/string.inko @@ -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 @@ -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 diff --git a/std/test/std/test_string.inko b/std/test/std/test_string.inko index a09095ca2..9f1263ac1 100644 --- a/std/test/std/test_string.inko +++ b/std/test/std/test_string.inko @@ -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)