userver: userver/utils/regex.hpp Source File
Loading...
Searching...
No Matches
regex.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <exception>
8#include <string_view>
9
10#include <userver/compiler/impl/lifetime.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utils {
16
17// NOLINTBEGIN(readability-identifier-naming)
18
19class match_results;
20struct Re2Replacement;
21
23class RegexError : public std::exception {};
24
47class regex final {
48public:
51
54 explicit regex(std::string_view pattern);
55
56 regex(const regex&);
57 regex(regex&&) noexcept;
58 regex& operator=(const regex&);
59 regex& operator=(regex&&) noexcept;
60 ~regex();
61
64 bool operator==(const regex&) const;
65
67 std::string_view GetPatternView() const USERVER_IMPL_LIFETIME_BOUND;
68
70 std::string str() const;
71
72private:
73 class Impl;
74 utils::FastPimpl<Impl, 16, 8> impl_;
75
76 friend class match_results;
77 friend bool regex_match(std::string_view str, const regex& pattern);
78 friend bool regex_match(std::string_view str, match_results& m, const regex& pattern);
79 friend bool regex_search(std::string_view str, const regex& pattern);
80 friend bool regex_search(std::string_view str, match_results& m, const regex& pattern);
81 friend std::string regex_replace(std::string_view str, const regex& pattern, std::string_view repl);
82 friend std::string regex_replace(std::string_view str, const regex& pattern, Re2Replacement repl);
83};
84
100class match_results final {
101public:
105
107 match_results& operator=(const match_results&);
109
111 std::size_t size() const;
112
115 std::string_view operator[](std::size_t sub) const;
116
120 std::size_t position(std::size_t sub) const;
121
124 std::size_t length(std::size_t sub) const;
125
127 std::string_view prefix() const;
128
130 std::string_view suffix() const;
131
132private:
133 struct Impl;
135
136 friend bool regex_match(std::string_view str, const regex& pattern);
137 friend bool regex_match(std::string_view str, match_results& m, const regex& pattern);
138 friend bool regex_search(std::string_view str, const regex& pattern);
139 friend bool regex_search(std::string_view str, match_results& m, const regex& pattern);
140 friend std::string regex_replace(std::string_view str, const regex& pattern, std::string_view repl);
141 friend std::string regex_replace(std::string_view str, const regex& pattern, Re2Replacement repl);
142};
143
146bool regex_match(std::string_view str, const regex& pattern);
147
151bool regex_match(std::string_view str, match_results& m, const regex& pattern);
152
155bool regex_search(std::string_view str, const regex& pattern);
156
160bool regex_search(std::string_view str, match_results& m, const regex& pattern);
161
168std::string regex_replace(std::string_view str, const regex& pattern, std::string_view repl);
169
187struct Re2Replacement final {
188 std::string_view replacement;
189};
190
193std::string regex_replace(std::string_view str, const regex& pattern, Re2Replacement repl);
194
195// NOLINTEND(readability-identifier-naming)
196
197} // namespace utils
198
199USERVER_NAMESPACE_END