StringPiece rtrimWhitespace(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.back() == ' '; sp.pop_back()) { } if (!sp.empty() && is_oddspace(sp.back())) { sp.pop_back(); 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); }