Skip to content

Commit 55ce12f

Browse files
committed
func at
1 parent 19df92a commit 55ce12f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Blob.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ module {
8484
/// Space: `O(1)`
8585
public func get(blob : Blob, index : Nat) : ?Nat8 = if (index < blob.size()) ?(blob.get index) else null;
8686

87+
/// Returns the byte at index `index`. Indexing is zero-based.
88+
/// Traps if `index >= size`, error message may not be descriptive.
89+
///
90+
/// Example:
91+
/// ```motoko include=import
92+
/// let blob : Blob = "abcdefgh\08";
93+
/// assert Blob.get(blob, 8) == 8;
94+
/// ```
95+
///
96+
/// Runtime: `O(1)`
97+
public func at(blob : Blob, index : Nat) : Nat8 = blob.get index;
98+
8799
/// Creates a `Blob` from an array of bytes (`[Nat8]`), by copying each element.
88100
///
89101
/// Example:

validation/api/api.lock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
{
5050
"name": "Blob",
5151
"exports": [
52+
"public func at(blob : Blob, index : Nat) : Nat8",
5253
"public type Blob",
5354
"public func compare(b1 : Blob, b2 : Blob) : Order.Order",
5455
"public func empty() : Blob",

0 commit comments

Comments
 (0)