Optimize is_email_address.

This commit is contained in:
levlam 2022-08-20 12:44:22 +03:00
parent 4206246b0c
commit fc9e878539

View File

@ -953,34 +953,31 @@ bool is_email_address(Slice str) {
Slice userdata; Slice userdata;
Slice domain; Slice domain;
std::tie(userdata, domain) = split(str, '@'); std::tie(userdata, domain) = split(str, '@');
vector<Slice> userdata_parts; if (domain.empty()) {
return false;
}
size_t prev = 0; size_t prev = 0;
size_t userdata_part_count = 0;
for (size_t i = 0; i < userdata.size(); i++) { for (size_t i = 0; i < userdata.size(); i++) {
if (userdata[i] == '.' || userdata[i] == '+') { if (userdata[i] == '.' || userdata[i] == '+') {
userdata_parts.push_back(userdata.substr(prev, i - prev)); if (i - prev >= 27) {
return false;
}
userdata_part_count++;
prev = i + 1; prev = i + 1;
} } else if (!is_alpha_digit_or_underscore_or_minus(userdata[i])) {
}
userdata_parts.push_back(userdata.substr(prev));
if (userdata_parts.size() >= 12) {
return false;
}
for (auto &part : userdata_parts) {
for (auto c : part) {
if (!is_alpha_digit_or_underscore_or_minus(c)) {
return false; return false;
} }
} }
} userdata_part_count++;
if (userdata_parts.back().empty() || userdata_parts.back().size() >= 36) { if (userdata_part_count >= 12) {
return false; return false;
} }
userdata_parts.pop_back(); auto last_part_length = userdata.size() - prev;
for (auto &part : userdata_parts) { if (last_part_length == 0 || last_part_length >= 36) {
if (part.size() >= 27) {
return false; return false;
} }
}
vector<Slice> domain_parts = full_split(domain, '.'); vector<Slice> domain_parts = full_split(domain, '.');
if (domain_parts.size() <= 1 || domain_parts.size() > 7) { if (domain_parts.size() <= 1 || domain_parts.size() > 7) {