NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
regex.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_STRING_REGEX_HPP__
2#define NEFORCE_CORE_STRING_REGEX_HPP__
3
12
17#include <pcre2.h>
18NEFORCE_BEGIN_NAMESPACE__
19
25
30struct regex_exception final : value_exception {
31 explicit regex_exception(const char* info = "Regex Operation Failed") noexcept :
32 value_exception(info) {}
33
34 explicit regex_exception(const exception& e) :
35 value_exception(e) {}
36
37 ~regex_exception() override = default;
38
39 NEFORCE_NODISCARD const char* type() const noexcept override { return "regex_exception"; }
40};
41 // Exceptions
43
49
57class NEFORCE_API match_result {
58public:
60
61private:
62 vector<string> groups_;
63 vector<pair<size_t, size_t>> group_positions_;
64 size_t position_ = string::npos;
65 size_t length_ = 0;
66 string subject_;
67
68public:
72 match_result() = default;
73
82 match_result(string subject, size_t pos, size_t len, const vector<string>& groups,
83 vector<pair<size_t, size_t>> group_positions);
84
89 NEFORCE_NODISCARD bool matched() const noexcept { return position_ != string::npos; }
90
95 NEFORCE_NODISCARD size_t position() const noexcept { return position_; }
96
101 NEFORCE_NODISCARD size_t length() const noexcept { return length_; }
102
107 NEFORCE_NODISCARD string_view data() const noexcept { return matched() ? groups_[0].view() : ""; }
108
113 NEFORCE_NODISCARD size_t size() const noexcept { return groups_.size(); }
114
120 NEFORCE_NODISCARD string_view operator[](const size_t idx) const noexcept {
121 return idx < groups_.size() ? groups_[idx].view() : "";
122 }
123
129 NEFORCE_NODISCARD pair<size_t, size_t> position(const size_t idx) const noexcept {
130 return idx < group_positions_.size() ? group_positions_[idx] : pair<size_t, size_t>{string::npos, 0};
131 }
132
137 NEFORCE_NODISCARD string_view prefix() const noexcept { return matched() ? subject_.view(0, position_) : ""; }
138
143 NEFORCE_NODISCARD string_view suffix() const noexcept {
144 return matched() ? subject_.view(position_ + length_) : "";
145 }
146
160 NEFORCE_NODISCARD string format(string_view fmt) const;
161
166 NEFORCE_NODISCARD iterator begin() const noexcept { return groups_.begin(); }
167
172 NEFORCE_NODISCARD iterator end() const noexcept { return groups_.end(); }
173};
174
175
176class NEFORCE_API regex_iterator;
177
178class NEFORCE_API regex_token_iterator;
179
187class NEFORCE_API regex {
188private:
189 struct pcre2_code_deleter {
190 void operator()(::pcre2_code* code) const noexcept {
191 if (code != nullptr) {
192 ::pcre2_code_free(code);
193 }
194 }
195 };
196
198 string pattern_;
199 uint32_t options_;
200 int capture_count_ = 0;
201
202 friend class regex_iterator;
203 friend class regex_token_iterator;
204
205private:
206 void compile(const string& pattern, uint32_t options = 0);
207
208 match_result do_match(::PCRE2_SPTR subject, size_t length, size_t start_offset, uint32_t options,
209 const string& subject_str) const;
210
211public:
218 explicit regex(const string& pattern, uint32_t options = 0);
219
220 regex(regex&& other) noexcept;
221 regex& operator=(regex&& other) noexcept;
222
228 regex(const regex& other);
229
236 regex& operator=(const regex& other);
237
245 NEFORCE_NODISCARD match_result do_match(const string& str) const;
246
252 NEFORCE_NODISCARD bool match(const string& str) const;
253
260 NEFORCE_NODISCARD match_result search(const string& str, size_t pos = 0) const;
261
267 NEFORCE_NODISCARD vector<match_result> find_all(const string& str) const;
268
275 NEFORCE_NODISCARD string replace_first(const string& str, string_view fmt) const;
276
283 NEFORCE_NODISCARD string replace_all(const string& str, string_view fmt) const;
284
291 NEFORCE_NODISCARD string replace_all_callback(const string& str,
292 function<string(const match_result&)> callback) const;
293
300 NEFORCE_NODISCARD vector<string> split(const string& str, int max_splits = -1) const;
301
306 NEFORCE_NODISCARD int capture_count() const noexcept { return capture_count_; }
307
312 NEFORCE_NODISCARD const string& pattern() const noexcept { return pattern_; }
313
318 NEFORCE_NODISCARD bool valid() const noexcept { return code_ != nullptr; }
319
325 NEFORCE_NODISCARD regex_iterator begin(const string& str) const;
326
332 NEFORCE_NODISCARD regex_iterator end(const string& str) const;
333};
334
341class NEFORCE_API regex_iterator {
342public:
346 using pointer = const match_result*;
347 using reference = const match_result&;
348
349private:
350 const regex* regex_ = nullptr;
351 string subject_;
352
353 match_result current_;
354 size_t next_pos_ = 0;
355 bool done_ = true;
356
357 void find_next();
358
359public:
363 regex_iterator() = default;
364
371 regex_iterator(const regex* re, string str, size_t pos = 0);
372
377 NEFORCE_NODISCARD reference operator*() const noexcept { return current_; }
378
383 NEFORCE_NODISCARD pointer operator->() const noexcept { return &current_; }
384
390
396 regex_iterator tmp = *this;
397 ++(*this);
398 return tmp;
399 }
400
406 NEFORCE_NODISCARD bool operator==(const regex_iterator& other) const noexcept;
407
413 NEFORCE_NODISCARD bool operator!=(const regex_iterator& other) const noexcept { return !(*this == other); }
414
421 static regex_iterator begin(const regex* re, const string& str) { return {re, str, 0}; }
422
429 static regex_iterator end(const regex* re, const string& str) {
431 it.regex_ = re;
432 it.subject_ = str;
433 it.done_ = true;
434 return it;
435 }
436};
437
445class NEFORCE_API regex_token_iterator {
446public:
451 enum class state {
452 BEFORE_FIRST,
453 BETWEEN_MATCHES,
454 AFTER_LAST,
456 };
457
458private:
459 const regex* regex_ = nullptr;
460 string subject_;
461 regex_iterator match_iterator_;
462 regex_iterator end_iterator_;
463 string current_;
464 int index_ = 0;
465 state state_ = state::END;
466 size_t last_pos_ = 0;
467
468private:
469 void find_next();
470
471public:
476
483 regex_token_iterator(const regex* re, string str, int index = 0);
484
489 NEFORCE_NODISCARD string_view operator*() const noexcept { return current_.view(); }
490
496
502 regex_token_iterator tmp = *this;
503 ++(*this);
504 return tmp;
505 }
506
512 NEFORCE_NODISCARD bool operator==(const regex_token_iterator& other) const noexcept;
513
519 NEFORCE_NODISCARD bool operator!=(const regex_token_iterator& other) const noexcept { return !(*this == other); }
520};
521
523inline regex_iterator regex::begin(const string& str) const { return regex_iterator::begin(this, str); }
524
525inline regex_iterator regex::end(const string& str) const { return regex_iterator::end(this, str); }
527 // Regex
529
530NEFORCE_END_NAMESPACE__
531#endif // NEFORCE_CORE_STRING_REGEX_HPP__
constexpr basic_string_view view(const size_type off, const size_type count=npos) const
获取子视图
static constexpr size_type npos
函数包装器主模板声明
正则表达式匹配结果类
iterator begin() const noexcept
格式化替换结果
match_result(string subject, size_t pos, size_t len, const vector< string > &groups, vector< pair< size_t, size_t > > group_positions)
构造函数
iterator end() const noexcept
获取捕获组迭代器结束位置
pair< size_t, size_t > position(const size_t idx) const noexcept
获取指定捕获组的位置信息
string_view data() const noexcept
获取完整匹配的文本
bool matched() const noexcept
检查是否匹配成功
string_view suffix() const noexcept
获取匹配后的后缀
size_t length() const noexcept
获取匹配长度
string_view operator[](const size_t idx) const noexcept
获取指定捕获组
size_t size() const noexcept
获取捕获组数量
string_view prefix() const noexcept
获取匹配前的前缀
match_result()=default
默认构造函数
size_t position() const noexcept
获取匹配起始位置
vector< string >::const_iterator iterator
捕获组迭代器类型
正则表达式匹配迭代器
forward_iterator_tag iterator_category
迭代器类型
regex_iterator(const regex *re, string str, size_t pos=0)
构造函数
const match_result * pointer
指针类型
ptrdiff_t difference_type
差值类型
match_result value_type
元素类型
pointer operator->() const noexcept
成员访问操作符
const match_result & reference
引用类型
regex_iterator operator++(int)
后置递增操作符
regex_iterator()=default
默认构造函数,构造结束迭代器
static regex_iterator end(const regex *re, const string &str)
获取结束迭代器
bool operator==(const regex_iterator &other) const noexcept
相等比较操作符
static regex_iterator begin(const regex *re, const string &str)
获取起始迭代器
regex_iterator & operator++()
前置递增操作符
bool operator!=(const regex_iterator &other) const noexcept
不等比较操作符
reference operator*() const noexcept
解引用操作符
state
迭代器状态枚举
bool operator==(const regex_token_iterator &other) const noexcept
相等比较操作符
string_view operator*() const noexcept
解引用操作符
regex_token_iterator(const regex *re, string str, int index=0)
构造函数
regex_token_iterator()=default
默认构造函数
bool operator!=(const regex_token_iterator &other) const noexcept
不等比较操作符
regex_token_iterator & operator++()
前置递增操作符
regex_token_iterator operator++(int)
后置递增操作符
正则表达式类
string replace_first(const string &str, string_view fmt) const
替换第一个匹配
regex_iterator begin(const string &str) const
获取匹配结果迭代器起始位置
int capture_count() const noexcept
获取捕获组数量
vector< string > split(const string &str, int max_splits=-1) const
使用正则表达式分割字符串
vector< match_result > find_all(const string &str) const
查找所有匹配
bool match(const string &str) const
检查是否完全匹配
string replace_all(const string &str, string_view fmt) const
替换所有匹配
string replace_all_callback(const string &str, function< string(const match_result &)> callback) const
使用回调函数替换所有匹配
regex(const regex &other)
拷贝构造函数
bool valid() const noexcept
检查正则表达式是否有效
regex_iterator end(const string &str) const
获取匹配结果迭代器结束位置
match_result do_match(const string &str) const
执行完整匹配
regex & operator=(const regex &other)
拷贝赋值运算符
match_result search(const string &str, size_t pos=0) const
在字符串中搜索第一个匹配
const string & pattern() const noexcept
获取正则表达式模式
regex(const string &pattern, uint32_t options=0)
从字符串构造正则表达式
独占智能指针
动态大小数组容器
vector_iterator< true, vector< T, Alloc > > const_iterator
常量迭代器类型
通用函数包装器
unsigned int uint32_t
32位无符号整数类型
@ END
从文件结尾开始
constexpr string format(const string_view fmt, Args &&... args)
格式化字符串
int64_t ptrdiff_t
指针差类型
basic_string_view< char > string_view
字符字符串视图
字符串类型别名和实用函数
存储两个值的元组对
const char * type() const noexcept override
获取异常类型
独占智能指针
动态大小数组容器