Upa URL C++ library
A WHATWG URL Standard implementation
main
Toggle main menu visibility
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
14
namespace
upa
{
15
24
class
regex_engine_std
{
25
public
:
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
65
private
:
66
std::regex re_;
67
};
68
69
}
// namespace upa
70
71
#endif
// UPA_REGEX_ENGINE_STD_H
upa::regex_engine_std::result
Definition
regex_engine_std.h:26
upa::regex_engine_std::result::get
std::optional< std::string > get(std::size_t ind, std::string_view) const
Definition
regex_engine_std.h:31
upa::regex_engine_std::result::regex_engine_std
friend class regex_engine_std
Definition
regex_engine_std.h:39
upa::regex_engine_std::result::size
std::size_t size() const
Definition
regex_engine_std.h:28
upa::regex_engine_std
Regex engine class based on std::regex.
Definition
regex_engine_std.h:24
upa::regex_engine_std::init
bool init(std::string_view regex_str, bool ignore_case)
Definition
regex_engine_std.h:42
upa::regex_engine_std::test
bool test(std::string_view input) const
Definition
regex_engine_std.h:61
upa::regex_engine_std::exec
bool exec(std::string_view input, result &res) const
Definition
regex_engine_std.h:58
upa
Definition
url.h:50
include
upa
regex_engine_std.h
Generated by
1.18.0