Compare commits

...

2 Commits

Author SHA1 Message Date
blankie 0e709f25cd
Avoid copying strings in StringFilter 2023-02-17 12:33:08 +07:00
blankie 12cf71130f
Add LICENSE 2023-02-17 12:13:42 +07:00
3 changed files with 58 additions and 27 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2023 blank X
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -94,55 +94,64 @@ const std::string* StringFilter::error() const {
}
bool StringFilter::_match_no_regex(const LogcatEntry& entry) const {
bool matched;
if (this->exact_match) {
switch (this->key) {
case FilterKey::User: matched = entry.user.value_or("") == this->other; break;
case FilterKey::Tag: matched = entry.tag == this->other; break;
case FilterKey::Message: matched = entry.message == this->other; break;
case FilterKey::User: return entry.user && *entry.user == this->other;
case FilterKey::Tag: return entry.tag == this->other;
case FilterKey::Message: return entry.message == this->other;
default: throw std::runtime_error("StringFilter::match received unhandled key");
}
} else {
switch (this->key) {
case FilterKey::User: matched = entry.user.value_or("").find(this->other) != std::string::npos; break;
case FilterKey::Tag: matched = entry.tag.find(this->other) != std::string::npos; break;
case FilterKey::Message: matched = entry.message.find(this->other) != std::string::npos; break;
case FilterKey::User: return entry.user && entry.user->find(this->other) != std::string::npos;
case FilterKey::Tag: return entry.tag.find(this->other) != std::string::npos;
case FilterKey::Message: return entry.message.find(this->other) != std::string::npos;
default: throw std::runtime_error("StringFilter::match received unhandled key");
}
}
return !this->inverted ? matched : !matched;
}
bool StringFilter::match(const LogcatEntry& entry) const {
bool matched;
if (this->error()) {
log(std::string("StringFilter::match called despite there being an error: ") + *this->error());
return true;
}
if (!this->other.starts_with("regex:")) {
return this->_match_no_regex(entry);
}
bool StringFilter::_match_regex(const LogcatEntry& entry) const {
if (!this->_regex) {
log("StringFilter::match called with a regex despite there being no regex");
return true;
}
std::string str;
bool matched;
const std::string* str;
regmatch_t pmatch[1];
int eflags = REG_STARTEND;
switch (this->key) {
case FilterKey::User: str = entry.user.value_or(""); break;
case FilterKey::Tag: str = entry.tag; break;
case FilterKey::Message: str = entry.message; break;
case FilterKey::User: str = entry.user ? &*entry.user : nullptr; break;
case FilterKey::Tag: str = &entry.tag; break;
case FilterKey::Message: str = &entry.message; break;
default: throw std::runtime_error("StringFilter::match received unhandled key");
}
pmatch[0].rm_so = 0;
pmatch[0].rm_eo = static_cast<regoff_t>(str.size());
matched = this->_regex->match(str.data(), 1, pmatch, REG_STARTEND);
if (this->exact_match) {
matched = matched && pmatch[0].rm_so == 0 && static_cast<size_t>(pmatch[0].rm_eo) == str.size();
pmatch[0].rm_eo = str ? static_cast<regoff_t>(str->size()) : 0;
if (!this->exact_match) {
eflags |= REG_NOSUB;
}
matched = this->_regex->match(str ? str->data() : "", 1, pmatch, eflags);
if (this->exact_match) {
matched = matched && pmatch[0].rm_so == 0 && str
? static_cast<size_t>(pmatch[0].rm_eo) == str->size()
: pmatch[0].rm_so == 0;
}
return matched;
}
bool StringFilter::match(const LogcatEntry& entry) const {
if (this->error()) {
log(std::string("StringFilter::match called despite there being an error: ") + *this->error());
return true;
}
bool matched = !this->other.starts_with("regex:")
? this->_match_no_regex(entry)
: this->_match_regex(entry);
return !this->inverted ? matched : !matched;
}

View File

@ -74,6 +74,7 @@ public:
private:
bool _match_no_regex(const LogcatEntry& entry) const;
bool _match_regex(const LogcatEntry& entry) const;
std::string _error;
std::optional<Pcre2Regex> _regex;