Skip to content

Commit 7ff6663

Browse files
committed
func get
1 parent 6072003 commit 7ff6663

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Blob.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ module {
6969
/// ```
7070
public func size(blob : Blob) : Nat = blob.size();
7171

72+
/// Returns the byte at index `index` as an option.
73+
/// Returns `null` when `index >= size`. Indexing is zero-based.
74+
///
75+
/// Example:
76+
/// ```motoko include=import
77+
/// let blob : Blob = "abcdefg\08";
78+
/// assert Blob.get(blob, 8) == ?8;
79+
/// assert Blob.get(blob, 10) == null;
80+
/// ```
81+
///
82+
/// Runtime: `O(1)`
83+
///
84+
/// Space: `O(1)`
85+
public func get(blob : Blob, index : Nat) : ?Nat8 = if (index < blob.size()) ?(blob.get index) else null;
86+
7287
/// Creates a `Blob` from an array of bytes (`[Nat8]`), by copying each element.
7388
///
7489
/// Example:

0 commit comments

Comments
 (0)