|
| 1 | +#include <module.hpp> |
| 2 | +#include <utils.hpp> |
| 3 | + |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include <re2/re2.h> |
| 7 | + |
| 8 | +namespace Regex |
| 9 | +{ |
| 10 | + using namespace Ark; |
| 11 | + using namespace Ark::literals; |
| 12 | + using namespace re2; |
| 13 | + |
| 14 | + Value toValue(const Match& match, const std::string& text) |
| 15 | + { |
| 16 | + if (const auto& span = match.spans.front(); !span.empty) |
| 17 | + return Value(text.substr(span.start, span.size)); |
| 18 | + return Nil; |
| 19 | + } |
| 20 | + |
| 21 | + Value toValue(const MatchImpl_t& res) |
| 22 | + { |
| 23 | + if (res.empty) |
| 24 | + return Nil; |
| 25 | + |
| 26 | + if (res.expect_many) |
| 27 | + { |
| 28 | + Value output(ValueType::List); |
| 29 | + for (const Match& match : res.matches) |
| 30 | + { |
| 31 | + // todo: create a dict with all the groups |
| 32 | + output.push_back(toValue(match, res.text)); |
| 33 | + } |
| 34 | + return output; |
| 35 | + } |
| 36 | + |
| 37 | + const Match& match = res.matches.front(); |
| 38 | + return toValue(match, res.text); |
| 39 | + } |
| 40 | + |
| 41 | + Value search(std::vector<Value>& args, VM* vm) |
| 42 | + { |
| 43 | + if (!types::check(args, ValueType::String, ValueType::String)) |
| 44 | + throw types::TypeCheckingError( |
| 45 | + "re:search", |
| 46 | + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, |
| 47 | + args); |
| 48 | + const std::string& regex = args[0].string(); |
| 49 | + const std::string& input = args[1].string(); |
| 50 | + return toValue(matchImpl(RE2::UNANCHORED, regex, input, true)); |
| 51 | + } |
| 52 | + |
| 53 | + Value match(std::vector<Value>& args, VM* vm) |
| 54 | + { |
| 55 | + if (!types::check(args, ValueType::String, ValueType::String)) |
| 56 | + throw types::TypeCheckingError( |
| 57 | + "re:match", |
| 58 | + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, |
| 59 | + args); |
| 60 | + const std::string& regex = args[0].string(); |
| 61 | + const std::string& input = args[1].string(); |
| 62 | + return toValue(matchImpl(RE2::ANCHOR_START, regex, input, true)); |
| 63 | + } |
| 64 | + |
| 65 | + Value full_match(std::vector<Value>& args, VM* vm) |
| 66 | + { |
| 67 | + if (!types::check(args, ValueType::String, ValueType::String)) |
| 68 | + throw types::TypeCheckingError( |
| 69 | + "re:fullMatch", |
| 70 | + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, |
| 71 | + args); |
| 72 | + const std::string& regex = args[0].string(); |
| 73 | + const std::string& input = args[1].string(); |
| 74 | + return toValue(matchImpl(RE2::ANCHOR_BOTH, regex, input, true)); |
| 75 | + } |
| 76 | + |
| 77 | + Value find_all(std::vector<Value>& args, VM* vm) |
| 78 | + { |
| 79 | + if (!types::check(args, ValueType::String, ValueType::String)) |
| 80 | + throw types::TypeCheckingError( |
| 81 | + "re:findAll", |
| 82 | + { { types::Contract { { types::Typedef("regex", ValueType::String), types::Typedef("input", ValueType::String) } } } }, |
| 83 | + args); |
| 84 | + const std::string& regex = args[0].string(); |
| 85 | + const std::string& input = args[1].string(); |
| 86 | + return toValue(matchImpl(RE2::UNANCHORED, regex, input, false)); |
| 87 | + } |
| 88 | + |
| 89 | + Value sub(std::vector<Value>& args, VM* vm) |
| 90 | + { |
| 91 | + if (!types::check(args, ValueType::String, ValueType::String, ValueType::String)) |
| 92 | + throw types::TypeCheckingError( |
| 93 | + "re:sub", |
| 94 | + { { types::Contract { { types::Typedef("regex", ValueType::String), |
| 95 | + types::Typedef("replacement", ValueType::String), |
| 96 | + types::Typedef("input", ValueType::String) } } } }, |
| 97 | + args); |
| 98 | + const std::string& regex = args[0].string(); |
| 99 | + const std::string& replacement = args[1].string(); |
| 100 | + std::string input = args[2].string(); |
| 101 | + |
| 102 | + // todo: control how many times we want to replace? |
| 103 | + RE2::GlobalReplace(&input, regex, replacement); |
| 104 | + return Value(input); |
| 105 | + } |
| 106 | + |
| 107 | + Value split(std::vector<Value>& args, VM* vm) |
| 108 | + { |
| 109 | + if (!types::check(args, ValueType::String, ValueType::String) && !types::check(args, ValueType::String, ValueType::String, ValueType::Number)) |
| 110 | + throw types::TypeCheckingError( |
| 111 | + "re:split", |
| 112 | + { { types::Contract { |
| 113 | + { types::Typedef("regex", ValueType::String), |
| 114 | + types::Typedef("input", ValueType::String) } }, |
| 115 | + types::Contract { { types::Typedef("regex", ValueType::String), |
| 116 | + types::Typedef("input", ValueType::String), |
| 117 | + types::Typedef("max_split", ValueType::Number) } } } }, |
| 118 | + args); |
| 119 | + const std::string& regex = args[0].string(); |
| 120 | + const std::string& input = args[1].string(); |
| 121 | + long max_split = 0; |
| 122 | + if (args.size() == 3) |
| 123 | + max_split = static_cast<long>(args[2].number()); |
| 124 | + |
| 125 | + Value output(ValueType::List); |
| 126 | + if (max_split < 0) |
| 127 | + { |
| 128 | + output.push_back(args[1]); |
| 129 | + return output; |
| 130 | + } |
| 131 | + |
| 132 | + long num_split = 0; |
| 133 | + std::size_t end = 0; |
| 134 | + const MatchImpl_t res = matchImpl(RE2::UNANCHORED, regex, input, false); |
| 135 | + if (res.empty) |
| 136 | + return Nil; |
| 137 | + |
| 138 | + for (const Match& match : res.matches) |
| 139 | + { |
| 140 | + output.push_back(Value(res.text.substr(end, match.start() - end))); |
| 141 | + for (std::size_t i = 1; i < match.spans.size(); ++i) |
| 142 | + { |
| 143 | + if (!match.spans[i].empty) |
| 144 | + output.push_back(Value(res.text.substr(match.spans[i].start, match.spans[i].size))); |
| 145 | + else |
| 146 | + output.push_back(Nil); |
| 147 | + } |
| 148 | + |
| 149 | + end = match.end(); |
| 150 | + num_split++; |
| 151 | + |
| 152 | + if (num_split == max_split) |
| 153 | + break; |
| 154 | + } |
| 155 | + if (end < res.text.size()) |
| 156 | + output.push_back(Value(res.text.substr(end))); |
| 157 | + |
| 158 | + return output; |
| 159 | + } |
| 160 | + |
| 161 | + Value escape(std::vector<Value>& args, VM* vm) |
| 162 | + { |
| 163 | + if (!types::check(args, ValueType::String)) |
| 164 | + throw types::TypeCheckingError( |
| 165 | + "re:escape", |
| 166 | + { { types::Contract { { types::Typedef("text", ValueType::String) } } } }, |
| 167 | + args); |
| 168 | + return Value(RE2::QuoteMeta(args[0].string())); |
| 169 | + } |
| 170 | +} |
0 commit comments