-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.hpp
More file actions
34 lines (26 loc) · 868 Bytes
/
regex.hpp
File metadata and controls
34 lines (26 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include <regex.h>
namespace cfbox::util {
class scoped_regex {
regex_t regex_{};
bool valid_ = false;
public:
scoped_regex() = default;
~scoped_regex() {
if (valid_) regfree(®ex_);
}
scoped_regex(const scoped_regex&) = delete;
scoped_regex& operator=(const scoped_regex&) = delete;
auto compile(const char* pattern, int flags) -> int {
if (valid_) { regfree(®ex_); valid_ = false; }
int rc = regcomp(®ex_, pattern, flags);
valid_ = (rc == 0);
return rc;
}
auto exec(const char* str, std::size_t nmatch, regmatch_t* matches, int flags) const -> int {
return regexec(®ex_, str, nmatch, matches, flags);
}
auto get() const -> const regex_t* { return ®ex_; }
auto valid() const -> bool { return valid_; }
};
} // namespace cfbox::util