StringPiece ltrimWhitespace(StringPiece sp) { // Spaces other than ' ' characters are less common but should be // checked. This configuration where we loop on the ' ' // separately from oddspaces was empirically fastest. loop: for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) { } if (!sp.empty() && is_oddspace(sp.front())) { sp.pop_front(); goto loop; } return sp; }
StringPiece trim(StringPiece sp, StringPiece chars) { for (; !sp.empty() && chars.find(sp.front()) != StringPiece::npos; ) { sp.pop_front(); } for (; !sp.empty() && chars.find(sp.back()) != StringPiece::npos; ) { sp.pop_back(); } return sp; }
bool IPAddressV6::validate(StringPiece ip) { if (ip.size() > 0 && ip.front() == '[' && ip.back() == ']') { ip = ip.subpiece(1, ip.size() - 2); } constexpr size_t kStrMaxLen = INET6_ADDRSTRLEN; std::array<char, kStrMaxLen + 1> ip_cstr; const size_t len = std::min(ip.size(), kStrMaxLen); std::memcpy(ip_cstr.data(), ip.data(), len); ip_cstr[len] = 0; struct in6_addr addr; return 1 == inet_pton(AF_INET6, ip_cstr.data(), &addr); }
int LogName::cmp(StringPiece a, StringPiece b) { // Ignore trailing separators auto stripTrailingSeparators = [](StringPiece& s) { while (!s.empty() && isSeparator(s.back())) { s.uncheckedSubtract(1); } }; stripTrailingSeparators(a); stripTrailingSeparators(b); // Advance ptr until it no longer points to a category separator. // This is used to skip over consecutive sequences of separator characters. auto skipOverSeparators = [](StringPiece& s) { while (!s.empty() && isSeparator(s.front())) { s.uncheckedAdvance(1); } }; bool ignoreSeparator = true; while (true) { if (ignoreSeparator) { skipOverSeparators(a); skipOverSeparators(b); } if (a.empty()) { return b.empty() ? 0 : -1; } else if (b.empty()) { return 1; } if (isSeparator(a.front())) { if (!isSeparator(b.front())) { return '.' - b.front(); } ignoreSeparator = true; } else { if (a.front() != b.front()) { return a.front() - b.front(); } ignoreSeparator = false; } a.uncheckedAdvance(1); b.uncheckedAdvance(1); } }