| 39 | } |
| 40 | |
| 41 | std::optional<std::vector<std::optional<std::string>>> |
| 42 | v8_regex_provider::regex_search(std::string_view input, |
| 43 | const tns::v8_regex_provider::regex_type &pattern) { |
| 44 | auto isolate = v8::Isolate::GetCurrent(); |
| 45 | if (isolate == nullptr) { |
| 46 | return std::nullopt; |
| 47 | } |
| 48 | auto ctx = isolate->GetCurrentContext(); |
| 49 | auto patt = pattern.Get(isolate); |
| 50 | if (patt.IsEmpty()) { |
| 51 | return std::nullopt; |
| 52 | } |
| 53 | |
| 54 | v8::Local<v8::String> local_input; |
| 55 | if (!v8::String::NewFromUtf8( |
| 56 | isolate, input.data(), v8::NewStringType::kNormal, input.size()) |
| 57 | .ToLocal(&local_input)) { |
| 58 | return std::nullopt; |
| 59 | } |
| 60 | |
| 61 | v8::Local<v8::Object> matches; |
| 62 | if (!patt->Exec(isolate->GetCurrentContext(), local_input).ToLocal( |
| 63 | &matches) || matches->IsNull()) { |
| 64 | return std::nullopt; |
| 65 | } |
| 66 | |
| 67 | std::vector<std::optional<std::string>> ret; |
| 68 | if (matches->IsArray()) { |
| 69 | auto array = matches.As<v8::Array>(); |
| 70 | auto len = array->Length(); |
| 71 | ret.reserve(len); |
| 72 | for (int i = 0; i < len; i++) { |
| 73 | v8::Local<v8::Value> item; |
| 74 | if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&item)) { |
| 75 | return std::nullopt; |
| 76 | } |
| 77 | |
| 78 | if (item->IsUndefined()) { |
| 79 | ret.emplace_back(std::nullopt); |
| 80 | } else if (item->IsString()) { |
| 81 | ret.emplace_back(ArgConverter::ConvertToString( |
| 82 | item->ToString(ctx).ToLocalChecked())); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | bool v8_regex_provider::regex_match(std::string_view input, |
| 91 | const tns::v8_regex_provider::regex_type &pattern) { |
nothing calls this directly
no test coverage detected