Skip to content

Commit

Permalink
feat(Matcher): findall function implementented
Browse files Browse the repository at this point in the history
  • Loading branch information
Demali-876 committed Nov 15, 2024
1 parent 76887ab commit c789fc7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Matcher.mo
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,42 @@ module {
lastIndex = 0
})
};
public func findAll(nfa : NFA, text : Text, flags : ?Flags) : Result.Result<[Match], MatchError> {
regex := nfa;
var startIndex = 0;
let textSize = text.size();
var matches = Buffer.Buffer<Match>(0);

while (startIndex < textSize) {
let remainingString : Text = substring(text, startIndex, textSize);
switch (search(nfa, remainingString, flags)) {
case (#ok(matchResult)) {
switch (matchResult.status) {
case (#FullMatch) {
let adjustedMatch = {
string = text;
value = matchResult.value;
status = #FullMatch;
position = (startIndex, startIndex + matchResult.position.1);
capturedGroups = matchResult.capturedGroups;
spans = matchResult.spans;
lastIndex = startIndex + matchResult.lastIndex
};
matches.add(adjustedMatch);
startIndex := adjustedMatch.lastIndex
};
case (#NoMatch) {
startIndex += 1
}
}
};
case (#err(e)) {
return #err(e)
}
}
};
return #ok(Buffer.toArray(matches))
};

}
}

0 comments on commit c789fc7

Please sign in to comment.