userver: userver/utils/regex.hpp Source File
Loading...
Searching...
No Matches
regex.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/regex.hpp
4/// @brief @copybrief utils::regex
5
6#include <string>
7
8#include <userver/utils/fast_pimpl.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils {
13
14class match_results;
15
16/// @ingroup userver_universal userver_containers
17///
18/// @brief Small alias for boost::regex / std::regex without huge includes
19class regex final {
20 public:
21 regex();
22 explicit regex(std::string_view pattern);
23
24 ~regex();
25
26 regex(const regex&);
27 regex(regex&&) noexcept;
28
29 regex& operator=(const regex&);
30 regex& operator=(regex&&) noexcept;
31
32 bool operator==(const regex&) const;
33
34 std::string str() const;
35
36 private:
37 struct Impl;
38 utils::FastPimpl<Impl, 16, 8> impl_;
39
40 friend class match_results;
41 friend bool regex_match(std::string_view str, const regex& pattern);
42 friend bool regex_match(std::string_view str, match_results& m,
43 const regex& pattern);
44 friend bool regex_search(std::string_view str, const regex& pattern);
45 friend bool regex_search(std::string_view str, match_results& m,
46 const regex& pattern);
47 friend std::string regex_replace(std::string_view str, const regex& pattern,
48 std::string_view repl);
49};
50
51/// @ingroup userver_universal userver_containers
52///
53/// @brief Small alias for boost::smatch / std::regex without huge includes
54class match_results final {
55 public:
56 match_results();
57
58 ~match_results();
59
60 match_results(const match_results&);
61
62 match_results& operator=(const match_results&);
63
64 std::size_t size() const;
65 std::string_view operator[](int sub) const;
66
67 private:
68 struct Impl;
69 utils::FastPimpl<Impl, 80, 8> impl_;
70
71 friend bool regex_match(std::string_view str, const regex& pattern);
72 friend bool regex_match(std::string_view str, match_results& m,
73 const regex& pattern);
74 friend bool regex_search(std::string_view str, const regex& pattern);
75 friend bool regex_search(std::string_view str, match_results& m,
76 const regex& pattern);
77 friend std::string regex_replace(std::string_view str, const regex& pattern,
78 std::string_view repl);
79};
80
81/// @brief Determines whether the regular expression matches the entire target
82/// character sequence
83bool regex_match(std::string_view str, const regex& pattern);
84
85/// @brief Returns true if the specified regular expression matches
86/// the whole of the input. Fills in what matched in m.
87bool regex_match(std::string_view str, match_results& m, const regex& pattern);
88
89/// @brief Determines whether the regular expression matches anywhere in the
90/// target character sequence. Fills in what matched in m
91bool regex_search(std::string_view str, match_results& m, const regex& pattern);
92
93/// @brief Determines whether the regular expression matches anywhere in the
94/// target character sequence
95bool regex_search(std::string_view str, const regex& pattern);
96
97/// @brief Create a new string where all regular expression matches replaced
98/// with repl
99std::string regex_replace(std::string_view str, const regex& pattern,
100 std::string_view repl);
101
102} // namespace utils
103
104USERVER_NAMESPACE_END