Skip to content

Commit

Permalink
feat(Extensions): substring text function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Demali-876 committed Nov 6, 2024
1 parent d43281b commit 9b8a8f0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Extensions.mo
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ module{
arr[i];
};

//slice a text
//slice a text with optional end
public func slice(text : Text, start : Nat, end : ?Nat) : Text {
let chars = Text.toArray(text);
let slicedChars = switch (end) {
Expand All @@ -266,6 +266,16 @@ module{
};
Array.tabulate<T>(end - start, func(i:Nat) { arr[start + i] })
};

public func substring(text: Text, start: Nat, end: Nat) : Text {
let chars = Text.toArray(text);
// Assert valid indices if start equals end, return empty text
assert(start <= end);
assert(end <= chars.size());
if (start == end) return "";

Text.fromIter(Array.slice<Char>(chars, start, end));
};
//Error handling
public func errorToText(error: Types.RegexError): Text {
switch (error) {
Expand Down

0 comments on commit 9b8a8f0

Please sign in to comment.