Skip to content

Commit 52e32c5

Browse files
committed
feat(re): ARK-316 return a dictionary with details for each match
1 parent 64a5cde commit 52e32c5

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

draft/re/src/impl.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#include <Ark/VM/Value/Dict.hpp>
12
#include <module.hpp>
23
#include <utils.hpp>
34

45
#include <vector>
6+
#include <ranges>
57

68
#include <re2/re2.h>
79

@@ -13,9 +15,21 @@ namespace Regex
1315

1416
Value toValue(const Match& match, const std::string& text)
1517
{
16-
if (const auto& span = match.spans.front(); !span.empty)
17-
return Value(text.substr(span.start, span.size));
18-
return Nil;
18+
internal::Dict dict;
19+
const auto& span = match.spans.front();
20+
if (!span.empty)
21+
dict.set(Value("match"), Value(text.substr(span.start, span.size)));
22+
else
23+
dict.set(Value("match"), Nil);
24+
dict.set(Value("start"), Value(static_cast<int64_t>(span.start)));
25+
dict.set(Value("end"), Value(static_cast<int64_t>(span.size + span.start)));
26+
27+
Value groups(ValueType::List);
28+
for (const Span& s : match.spans | std::ranges::views::drop(1))
29+
groups.push_back(Value(text.substr(s.start, s.size)));
30+
dict.set(Value("groups"), groups);
31+
32+
return Value(std::move(dict));
1933
}
2034

2135
Value toValue(const MatchImpl_t& res)

0 commit comments

Comments
 (0)