Upa URL C++ library
A WHATWG URL Standard implementation
Loading...
Searching...
No Matches
regex_engine_std.h
Go to the documentation of this file.
1// Copyright 2025-2026 Rimas Misevičius
2// Distributed under the BSD-style license that can be
3// found in the LICENSE file.
4//
5#ifndef UPA_REGEX_ENGINE_STD_H
6#define UPA_REGEX_ENGINE_STD_H
7
8#include <cstddef>
9#include <optional>
10#include <regex>
11#include <string>
12#include <string_view>
13
14namespace upa {
15
25public:
26 class result {
27 public:
28 inline std::size_t size() const {
29 return arr_.size();
30 }
31 inline std::optional<std::string> get(std::size_t ind, std::string_view) const {
32 const auto& res = arr_[ind];
33 if (res.matched)
34 return std::string{ res.first, res.second };
35 return std::nullopt;
36 }
37 private:
38 std::match_results<std::string_view::const_iterator> arr_;
39 friend class regex_engine_std;
40 };
41
42 inline bool init(std::string_view regex_str, bool ignore_case) {
43 // 4. If options's ignore case is true then set flags to "vi".
44 // 5. Otherwise set flags to "v"
45 // Note that the std::regex does not have the "v" flag.
46 const std::regex::flag_type flag = ignore_case
47 ? (std::regex::ECMAScript | std::regex::icase)
48 : std::regex::ECMAScript;
49 try {
50 re_.assign(regex_str.data(), regex_str.length(), flag);
51 return true;
52 }
53 catch (const std::regex_error&) {
54 return false;
55 }
56 }
57
58 inline bool exec(std::string_view input, result& res) const {
59 return std::regex_match(input.begin(), input.end(), res.arr_, re_);
60 }
61 inline bool test(std::string_view input) const {
62 return std::regex_match(input.begin(), input.end(), re_);
63 }
64
65private:
66 std::regex re_;
67};
68
69} // namespace upa
70
71#endif // UPA_REGEX_ENGINE_STD_H
std::optional< std::string > get(std::size_t ind, std::string_view) const
Regex engine class based on std::regex.
bool init(std::string_view regex_str, bool ignore_case)
bool test(std::string_view input) const
bool exec(std::string_view input, result &res) const
Definition url.h:50