123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #include "Regex.h"
- namespace android {
- namespace vintf {
- namespace details {
- Regex::~Regex() {
- clear();
- }
- void Regex::clear() {
- if (mImpl != nullptr) {
- regfree(mImpl.get());
- mImpl = nullptr;
- }
- }
- bool Regex::compile(const std::string& pattern) {
- clear();
- mImpl = std::make_unique<regex_t>();
- int status = regcomp(mImpl.get(), pattern.c_str(), REG_EXTENDED | REG_NEWLINE);
- return status == 0;
- }
- bool Regex::matches(const std::string& s) const {
- regmatch_t match;
- int status =
- regexec(mImpl.get(), s.c_str(), 1 , &match , 0 );
- return status == 0 && match.rm_so == 0 && match.rm_eo >= 0 &&
- static_cast<size_t>(match.rm_eo) == s.length();
- }
- }
- }
- }
|