Commit d7d34a09 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Consistent use of sformat in address-related files

Summary:
[Folly] Consistent use of `sformat` in address-related files.

V.s. `format(/*...*/).str()` and v.s. `to<std::string>`.

Reviewed By: meyering

Differential Revision: D5570334

fbshipit-source-id: 83aedf9a694721fb209e62e94f1a5c5ecd355e81
parent fc10d0b8
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <folly/Conv.h> #include <folly/Format.h>
#include <folly/String.h> #include <folly/String.h>
#include <folly/detail/IPAddressSource.h> #include <folly/detail/IPAddressSource.h>
...@@ -81,12 +81,9 @@ CIDRNetwork IPAddress::createNetwork(StringPiece ipSlashCidr, ...@@ -81,12 +81,9 @@ CIDRNetwork IPAddress::createNetwork(StringPiece ipSlashCidr,
if (elemCount == 0 || // weird invalid string if (elemCount == 0 || // weird invalid string
elemCount > 2) { // invalid string (IP/CIDR/extras) elemCount > 2) { // invalid string (IP/CIDR/extras)
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(sformat(
"Invalid ipSlashCidr specified. ", "Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'",
"Expected IP/CIDR format, got ", ipSlashCidr));
"'",
ipSlashCidr,
"'"));
} }
IPAddress subnet(vec.at(0)); IPAddress subnet(vec.at(0));
auto cidr = auto cidr =
...@@ -97,25 +94,21 @@ CIDRNetwork IPAddress::createNetwork(StringPiece ipSlashCidr, ...@@ -97,25 +94,21 @@ CIDRNetwork IPAddress::createNetwork(StringPiece ipSlashCidr,
cidr = to<uint8_t>(vec.at(1)); cidr = to<uint8_t>(vec.at(1));
} catch (...) { } catch (...) {
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("Mask value ", "'", vec.at(1), "' not a valid mask")); sformat("Mask value '{}' not a valid mask", vec.at(1)));
} }
} }
if (cidr > subnet.bitCount()) { if (cidr > subnet.bitCount()) {
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(sformat(
"CIDR value '", "CIDR value '{}' is > network bit count '{}'",
cidr, cidr,
"' ", subnet.bitCount()));
"is > network bit count ",
"'",
subnet.bitCount(),
"'"));
} }
return std::make_pair(applyMask ? subnet.mask(cidr) : subnet, cidr); return std::make_pair(applyMask ? subnet.mask(cidr) : subnet, cidr);
} }
// public static // public static
std::string IPAddress::networkToString(const CIDRNetwork& network) { std::string IPAddress::networkToString(const CIDRNetwork& network) {
return network.first.str() + "/" + folly::to<std::string>(network.second); return sformat("{}/{}", network.first.str(), network.second);
} }
// public static // public static
...@@ -127,7 +120,7 @@ IPAddress IPAddress::fromBinary(ByteRange bytes) { ...@@ -127,7 +120,7 @@ IPAddress IPAddress::fromBinary(ByteRange bytes) {
} else { } else {
string hexval = detail::Bytes::toHex(bytes.data(), bytes.size()); string hexval = detail::Bytes::toHex(bytes.data(), bytes.size());
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("Invalid address with hex value ", "'", hexval, "'")); sformat("Invalid address with hex value '{}'", hexval));
} }
} }
...@@ -153,8 +146,7 @@ IPAddress::IPAddress(StringPiece addr) ...@@ -153,8 +146,7 @@ IPAddress::IPAddress(StringPiece addr)
{ {
string ip = addr.str(); // inet_pton() needs NUL-terminated string string ip = addr.str(); // inet_pton() needs NUL-terminated string
auto throwFormatException = [&](const string& msg) { auto throwFormatException = [&](const string& msg) {
throw IPAddressFormatException( throw IPAddressFormatException(sformat("Invalid IP '{}': {}", ip, msg));
to<std::string>("Invalid IP '", ip, "': ", msg));
}; };
if (ip.size() < 2) { if (ip.size() < 2) {
...@@ -333,8 +325,9 @@ bool IPAddress::inSubnetWithMask(const IPAddress& subnet, ...@@ -333,8 +325,9 @@ bool IPAddress::inSubnetWithMask(const IPAddress& subnet,
uint8_t IPAddress::getNthMSByte(size_t byteIndex) const { uint8_t IPAddress::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1; const auto highestIndex = byteCount() - 1;
if (byteIndex > highestIndex) { if (byteIndex > highestIndex) {
throw std::invalid_argument(to<string>("Byte index must be <= ", throw std::invalid_argument(sformat(
to<string>(highestIndex), " for addresses of type :", "Byte index must be <= {} for addresses of type: {}",
highestIndex,
detail::familyNameStr(family()))); detail::familyNameStr(family())));
} }
if (isV4()) { if (isV4()) {
...@@ -402,9 +395,10 @@ bool operator<(const IPAddress& addr1, const IPAddress& addr2) { ...@@ -402,9 +395,10 @@ bool operator<(const IPAddress& addr1, const IPAddress& addr2) {
CIDRNetwork CIDRNetwork
IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) { IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) {
if (one.first.family() != two.first.family()) { if (one.first.family() != two.first.family()) {
throw std::invalid_argument(to<string>("Can't compute " throw std::invalid_argument(sformat(
"longest common prefix between addresses of different families. " "Can't compute longest common prefix between addresses of different"
"Passed: ", detail::familyNameStr(one.first.family()), " and ", "families. Passed: {} and {}",
detail::familyNameStr(one.first.family()),
detail::familyNameStr(two.first.family()))); detail::familyNameStr(two.first.family())));
} }
if (one.first.isV4()) { if (one.first.isV4()) {
...@@ -424,14 +418,14 @@ IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) { ...@@ -424,14 +418,14 @@ IPAddress::longestCommonPrefix(const CIDRNetwork& one, const CIDRNetwork& two) {
[[noreturn]] void IPAddress::asV4Throw() const { [[noreturn]] void IPAddress::asV4Throw() const {
auto fam = detail::familyNameStr(family()); auto fam = detail::familyNameStr(family());
throw InvalidAddressFamilyException(to<std::string>( throw InvalidAddressFamilyException(
"Can't convert address with family ", fam, " to AF_INET address")); sformat("Can't convert address with family {} to AF_INET address", fam));
} }
[[noreturn]] void IPAddress::asV6Throw() const { [[noreturn]] void IPAddress::asV6Throw() const {
auto fam = detail::familyNameStr(family()); auto fam = detail::familyNameStr(family());
throw InvalidAddressFamilyException(to<std::string>( throw InvalidAddressFamilyException(
"Can't convert address with family ", fam, " to AF_INET6 address")); sformat("Can't convert address with family {} to AF_INET6 address", fam));
} }
} // namespace folly } // namespace folly
...@@ -74,7 +74,7 @@ uint32_t IPAddressV4::toLong(StringPiece ip) { ...@@ -74,7 +74,7 @@ uint32_t IPAddressV4::toLong(StringPiece ip) {
in_addr addr; in_addr addr;
if (inet_pton(AF_INET, str.c_str(), &addr) != 1) { if (inet_pton(AF_INET, str.c_str(), &addr) != 1) {
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("Can't convert invalid IP '", ip, "' ", "to long")); sformat("Can't convert invalid IP '{}' to long", ip));
} }
return addr.s_addr; return addr.s_addr;
} }
...@@ -100,8 +100,7 @@ IPAddressV4::IPAddressV4(StringPiece addr) ...@@ -100,8 +100,7 @@ IPAddressV4::IPAddressV4(StringPiece addr)
{ {
auto ip = addr.str(); auto ip = addr.str();
if (inet_pton(AF_INET, ip.c_str(), &addr_.inAddr_) != 1) { if (inet_pton(AF_INET, ip.c_str(), &addr_.inAddr_) != 1) {
throw IPAddressFormatException( throw IPAddressFormatException(sformat("Invalid IPv4 address '{}'", addr));
to<std::string>("Invalid IPv4 address '", addr, "'"));
} }
} }
...@@ -114,9 +113,8 @@ IPAddressV4::IPAddressV4(const in_addr src) ...@@ -114,9 +113,8 @@ IPAddressV4::IPAddressV4(const in_addr src)
// public // public
void IPAddressV4::setFromBinary(ByteRange bytes) { void IPAddressV4::setFromBinary(ByteRange bytes) {
if (bytes.size() != 4) { if (bytes.size() != 4) {
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(sformat(
"Invalid IPv4 binary data: length must ", "Invalid IPv4 binary data: length must be 4 bytes, got {}",
"be 4 bytes, got ",
bytes.size())); bytes.size()));
} }
memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr)); memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr));
...@@ -159,8 +157,7 @@ IPAddressV6 IPAddressV4::getIPv6For6To4() const { ...@@ -159,8 +157,7 @@ IPAddressV6 IPAddressV4::getIPv6For6To4() const {
// public // public
string IPAddressV4::toJson() const { string IPAddressV4::toJson() const {
return format( return sformat("{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash());
"{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash()).str();
} }
// public // public
...@@ -168,8 +165,8 @@ bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const { ...@@ -168,8 +165,8 @@ bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const {
auto subnetInfo = IPAddress::createNetwork(cidrNetwork); auto subnetInfo = IPAddress::createNetwork(cidrNetwork);
auto addr = subnetInfo.first; auto addr = subnetInfo.first;
if (!addr.isV4()) { if (!addr.isV4()) {
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(
"Address '", addr.toJson(), "' ", "is not a V4 address")); sformat("Address '{}' is not a V4 address", addr.toJson()));
} }
return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second)); return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second));
} }
...@@ -229,7 +226,7 @@ IPAddressV4 IPAddressV4::mask(size_t numBits) const { ...@@ -229,7 +226,7 @@ IPAddressV4 IPAddressV4::mask(size_t numBits) const {
static const auto bits = bitCount(); static const auto bits = bitCount();
if (numBits > bits) { if (numBits > bits) {
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("numBits(", numBits, ") > bitsCount(", bits, ")")); sformat("numBits({}) > bitsCount({})", numBits, bits));
} }
ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_); ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
...@@ -260,8 +257,9 @@ string IPAddressV4::toInverseArpaName() const { ...@@ -260,8 +257,9 @@ string IPAddressV4::toInverseArpaName() const {
uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const { uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1; const auto highestIndex = byteCount() - 1;
if (byteIndex > highestIndex) { if (byteIndex > highestIndex) {
throw std::invalid_argument(to<string>("Byte index must be <= ", throw std::invalid_argument(sformat(
to<string>(highestIndex), " for addresses of type :", "Byte index must be <= {} for addresses of type: {}",
highestIndex,
detail::familyNameStr(AF_INET))); detail::familyNameStr(AF_INET)));
} }
return bytes()[byteIndex]; return bytes()[byteIndex];
...@@ -270,8 +268,7 @@ uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const { ...@@ -270,8 +268,7 @@ uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
const ByteArray4 IPAddressV4::fetchMask(size_t numBits) { const ByteArray4 IPAddressV4::fetchMask(size_t numBits) {
static const size_t bits = bitCount(); static const size_t bits = bitCount();
if (numBits > bits) { if (numBits > bits) {
throw IPAddressFormatException( throw IPAddressFormatException("IPv4 addresses are 32 bits");
to<std::string>("IPv4 addresses are 32 bits"));
} }
auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits))); auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits)));
ByteArray4 arr; ByteArray4 arr;
......
...@@ -87,7 +87,7 @@ IPAddressV6::IPAddressV6(StringPiece addr) { ...@@ -87,7 +87,7 @@ IPAddressV6::IPAddressV6(StringPiece addr) {
// Allow addresses surrounded in brackets // Allow addresses surrounded in brackets
if (ip.size() < 2) { if (ip.size() < 2) {
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("Invalid IPv6 address '", ip, "': address too short")); sformat("Invalid IPv6 address '{}': address too short", ip));
} }
if (ip.front() == '[' && ip.back() == ']') { if (ip.front() == '[' && ip.back() == ']') {
ip = ip.substr(1, ip.size() - 2); ip = ip.substr(1, ip.size() - 2);
...@@ -105,8 +105,7 @@ IPAddressV6::IPAddressV6(StringPiece addr) { ...@@ -105,8 +105,7 @@ IPAddressV6::IPAddressV6(StringPiece addr) {
scope_ = uint16_t(ipAddr->sin6_scope_id); scope_ = uint16_t(ipAddr->sin6_scope_id);
freeaddrinfo(result); freeaddrinfo(result);
} else { } else {
throw IPAddressFormatException( throw IPAddressFormatException(sformat("Invalid IPv6 address '{}'", ip));
to<std::string>("Invalid IPv6 address '", ip, "'"));
} }
} }
...@@ -177,9 +176,8 @@ Optional<MacAddress> IPAddressV6::getMacAddressFromLinkLocal() const { ...@@ -177,9 +176,8 @@ Optional<MacAddress> IPAddressV6::getMacAddressFromLinkLocal() const {
void IPAddressV6::setFromBinary(ByteRange bytes) { void IPAddressV6::setFromBinary(ByteRange bytes) {
if (bytes.size() != 16) { if (bytes.size() != 16) {
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(sformat(
"Invalid IPv6 binary data: length must ", "Invalid IPv6 binary data: length must be 16 bytes, got {}",
"be 16 bytes, got ",
bytes.size())); bytes.size()));
} }
memcpy(&addr_.in6Addr_.s6_addr, bytes.data(), sizeof(in6_addr)); memcpy(&addr_.in6Addr_.s6_addr, bytes.data(), sizeof(in6_addr));
...@@ -243,8 +241,8 @@ static inline void unpackInto(const unsigned char* src, ...@@ -243,8 +241,8 @@ static inline void unpackInto(const unsigned char* src,
// public // public
IPAddressV4 IPAddressV6::getIPv4For6To4() const { IPAddressV4 IPAddressV6::getIPv4For6To4() const {
if (!is6To4()) { if (!is6To4()) {
throw IPAddressV6::TypeError(format( throw IPAddressV6::TypeError(
"Invalid IP '{}': not a 6to4 address", str()).str()); sformat("Invalid IP '{}': not a 6to4 address", str()));
} }
// convert 16x8 bytes into first 4x16 bytes // convert 16x8 bytes into first 4x16 bytes
uint16_t ints[4] = {0,0,0,0}; uint16_t ints[4] = {0,0,0,0};
...@@ -299,8 +297,7 @@ IPAddressV6::Type IPAddressV6::type() const { ...@@ -299,8 +297,7 @@ IPAddressV6::Type IPAddressV6::type() const {
// public // public
string IPAddressV6::toJson() const { string IPAddressV6::toJson() const {
return format( return sformat("{{family:'AF_INET6', addr:'{}', hash:{}}}", str(), hash());
"{{family:'AF_INET6', addr:'{}', hash:{}}}", str(), hash()).str();
} }
// public // public
...@@ -323,8 +320,8 @@ bool IPAddressV6::inSubnet(StringPiece cidrNetwork) const { ...@@ -323,8 +320,8 @@ bool IPAddressV6::inSubnet(StringPiece cidrNetwork) const {
auto subnetInfo = IPAddress::createNetwork(cidrNetwork); auto subnetInfo = IPAddress::createNetwork(cidrNetwork);
auto addr = subnetInfo.first; auto addr = subnetInfo.first;
if (!addr.isV6()) { if (!addr.isV6()) {
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(
"Address '", addr.toJson(), "' ", "is not a V6 address")); sformat("Address '{}' is not a V6 address", addr.toJson()));
} }
return inSubnetWithMask(addr.asV6(), fetchMask(subnetInfo.second)); return inSubnetWithMask(addr.asV6(), fetchMask(subnetInfo.second));
} }
...@@ -408,7 +405,7 @@ IPAddressV6 IPAddressV6::mask(size_t numBits) const { ...@@ -408,7 +405,7 @@ IPAddressV6 IPAddressV6::mask(size_t numBits) const {
static const auto bits = bitCount(); static const auto bits = bitCount();
if (numBits > bits) { if (numBits > bits) {
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("numBits(", numBits, ") > bitCount(", bits, ")")); sformat("numBits({}) > bitCount({})", numBits, bits));
} }
ByteArray16 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_); ByteArray16 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
return IPAddressV6(ba); return IPAddressV6(ba);
...@@ -419,12 +416,9 @@ string IPAddressV6::str() const { ...@@ -419,12 +416,9 @@ string IPAddressV6::str() const {
char buffer[INET6_ADDRSTRLEN + IFNAMSIZ + 1]; char buffer[INET6_ADDRSTRLEN + IFNAMSIZ + 1];
if (!inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) { if (!inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) {
throw IPAddressFormatException(to<std::string>( throw IPAddressFormatException(sformat(
"Invalid address with hex ", "Invalid address with hex '{}' with error {}",
"'",
detail::Bytes::toHex(bytes(), 16), detail::Bytes::toHex(bytes(), 16),
"'",
" with error ",
strerror(errno))); strerror(errno)));
} }
...@@ -472,8 +466,9 @@ string IPAddressV6::toInverseArpaName() const { ...@@ -472,8 +466,9 @@ string IPAddressV6::toInverseArpaName() const {
uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const { uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1; const auto highestIndex = byteCount() - 1;
if (byteIndex > highestIndex) { if (byteIndex > highestIndex) {
throw std::invalid_argument(to<string>("Byte index must be <= ", throw std::invalid_argument(sformat(
to<string>(highestIndex), " for addresses of type :", "Byte index must be <= {} for addresses of type: {}",
highestIndex,
detail::familyNameStr(AF_INET6))); detail::familyNameStr(AF_INET6)));
} }
return bytes()[byteIndex]; return bytes()[byteIndex];
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <ostream> #include <ostream>
#include <folly/Exception.h> #include <folly/Exception.h>
#include <folly/Format.h>
#include <folly/IPAddressV6.h> #include <folly/IPAddressV6.h>
#include <folly/String.h> #include <folly/String.h>
...@@ -80,24 +81,24 @@ void MacAddress::parse(StringPiece str) { ...@@ -80,24 +81,24 @@ void MacAddress::parse(StringPiece str) {
auto p = str.begin(); auto p = str.begin();
for (unsigned int byteIndex = 0; byteIndex < SIZE; ++byteIndex) { for (unsigned int byteIndex = 0; byteIndex < SIZE; ++byteIndex) {
if (p == str.end()) { if (p == str.end()) {
throw invalid_argument(to<string>("invalid MAC address \"", str, throw invalid_argument(
"\": not enough digits")); sformat("invalid MAC address '{}': not enough digits", str));
} }
// Skip over ':' or '-' separators between bytes // Skip over ':' or '-' separators between bytes
if (byteIndex != 0 && isSeparatorChar(*p)) { if (byteIndex != 0 && isSeparatorChar(*p)) {
++p; ++p;
if (p == str.end()) { if (p == str.end()) {
throw invalid_argument(to<string>("invalid MAC address \"", str, throw invalid_argument(
"\": not enough digits")); sformat("invalid MAC address '{}': not enough digits", str));
} }
} }
// Parse the upper nibble // Parse the upper nibble
uint8_t upper = detail::hexTable[static_cast<uint8_t>(*p)]; uint8_t upper = detail::hexTable[static_cast<uint8_t>(*p)];
if (upper & 0x10) { if (upper & 0x10) {
throw invalid_argument(to<string>("invalid MAC address \"", str, throw invalid_argument(
"\": contains non-hex digit")); sformat("invalid MAC address '{}': contains non-hex digit", str));
} }
++p; ++p;
...@@ -115,8 +116,8 @@ void MacAddress::parse(StringPiece str) { ...@@ -115,8 +116,8 @@ void MacAddress::parse(StringPiece str) {
lower = upper; lower = upper;
upper = 0; upper = 0;
} else { } else {
throw invalid_argument(to<string>("invalid MAC address \"", str, throw invalid_argument(
"\": contains non-hex digit")); sformat("invalid MAC address '{}': contains non-hex digit", str));
} }
} }
++p; ++p;
...@@ -128,8 +129,8 @@ void MacAddress::parse(StringPiece str) { ...@@ -128,8 +129,8 @@ void MacAddress::parse(StringPiece str) {
if (p != str.end()) { if (p != str.end()) {
// String is too long to be a MAC address // String is too long to be a MAC address
throw invalid_argument(to<string>("invalid MAC address \"", str, throw invalid_argument(
"\": found trailing characters")); sformat("invalid MAC address '{}': found trailing characters", str));
} }
// Only update now that we have successfully parsed the entire // Only update now that we have successfully parsed the entire
...@@ -139,8 +140,8 @@ void MacAddress::parse(StringPiece str) { ...@@ -139,8 +140,8 @@ void MacAddress::parse(StringPiece str) {
void MacAddress::setFromBinary(ByteRange value) { void MacAddress::setFromBinary(ByteRange value) {
if (value.size() != SIZE) { if (value.size() != SIZE) {
throw invalid_argument(to<string>("MAC address must be 6 bytes " throw invalid_argument(
"long, got ", value.size())); sformat("MAC address must be 6 bytes long, got ", value.size()));
} }
memcpy(bytes_ + 2, value.begin(), SIZE); memcpy(bytes_ + 2, value.begin(), SIZE);
} }
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <folly/CppAttributes.h> #include <folly/CppAttributes.h>
#include <folly/Exception.h> #include <folly/Exception.h>
#include <folly/Format.h>
#include <folly/Hash.h> #include <folly/Hash.h>
namespace { namespace {
...@@ -625,9 +626,11 @@ struct addrinfo* SocketAddress::getAddrInfo(const char* host, ...@@ -625,9 +626,11 @@ struct addrinfo* SocketAddress::getAddrInfo(const char* host,
struct addrinfo *results; struct addrinfo *results;
int error = getaddrinfo(host, port, &hints, &results); int error = getaddrinfo(host, port, &hints, &results);
if (error != 0) { if (error != 0) {
auto os = folly::to<std::string>( auto os = folly::sformat(
"Failed to resolve address for \"", host, "\": ", "Failed to resolve address for '{}': {} (error={})",
gai_strerror(error), " (error=", error, ")"); host,
gai_strerror(error),
error);
throw std::system_error(error, std::generic_category(), os); throw std::system_error(error, std::generic_category(), os);
} }
...@@ -685,9 +688,8 @@ void SocketAddress::getIpString(char *buf, size_t buflen, int flags) const { ...@@ -685,9 +688,8 @@ void SocketAddress::getIpString(char *buf, size_t buflen, int flags) const {
int rc = getnameinfo((sockaddr*)&tmp_sock, sizeof(sockaddr_storage), int rc = getnameinfo((sockaddr*)&tmp_sock, sizeof(sockaddr_storage),
buf, buflen, nullptr, 0, flags); buf, buflen, nullptr, 0, flags);
if (rc != 0) { if (rc != 0) {
auto os = folly::to<std::string>( auto os = sformat(
"getnameinfo() failed in getIpString() error = ", "getnameinfo() failed in getIpString() error = {}", gai_strerror(rc));
gai_strerror(rc));
throw std::system_error(rc, std::generic_category(), os); throw std::system_error(rc, std::generic_category(), os);
} }
} }
......
...@@ -21,14 +21,13 @@ ...@@ -21,14 +21,13 @@
namespace folly { namespace detail { namespace folly { namespace detail {
std::string familyNameStrDefault(sa_family_t family) { std::string familyNameStrDefault(sa_family_t family) {
return folly::sformat("sa_family_t({})", folly::to<std::string>(family)); return sformat("sa_family_t({})", family);
} }
[[noreturn]] void getNthMSBitImplThrow(size_t bitCount, sa_family_t family) { [[noreturn]] void getNthMSBitImplThrow(size_t bitCount, sa_family_t family) {
throw std::invalid_argument(folly::to<std::string>( throw std::invalid_argument(sformat(
"Bit index must be < ", "Bit index must be < {} for addresses of type: {}",
bitCount, bitCount,
" for addresses of type :",
familyNameStr(family))); familyNameStr(family)));
} }
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <folly/Conv.h> #include <folly/Format.h>
#include <folly/detail/IPAddress.h> #include <folly/detail/IPAddress.h>
// BSDish platforms don't provide standard access to s6_addr16 // BSDish platforms don't provide standard access to s6_addr16
...@@ -74,11 +74,9 @@ struct Bytes { ...@@ -74,11 +74,9 @@ struct Bytes {
0xff // /8 0xff // /8
}}; }};
if (oneMask > kBitCount || twoMask > kBitCount) { if (oneMask > kBitCount || twoMask > kBitCount) {
throw std::invalid_argument(folly::to<std::string>( throw std::invalid_argument(sformat(
"Invalid mask " "Invalid mask length: {}. Mask length must be <= {}",
"length: ", std::max(oneMask, twoMask),
oneMask > twoMask ? oneMask : twoMask,
". Mask length must be <= ",
kBitCount)); kBitCount));
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include <folly/Conv.h> #include <folly/Format.h>
#include <folly/IPAddress.h> #include <folly/IPAddress.h>
#include <glog/logging.h> #include <glog/logging.h>
...@@ -50,7 +50,7 @@ BENCHMARK_DRAW_LINE() ...@@ -50,7 +50,7 @@ BENCHMARK_DRAW_LINE()
BENCHMARK(ipv4_to_fully_qualified_port, iters) { BENCHMARK(ipv4_to_fully_qualified_port, iters) {
IPAddressV4 ip("255.255.255.255"); IPAddressV4 ip("255.255.255.255");
while (iters--) { while (iters--) {
string outputString = to<std::string>(ip.toFullyQualified(), ':', 65535); string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535);
folly::doNotOptimizeAway(outputString); folly::doNotOptimizeAway(outputString);
folly::doNotOptimizeAway(outputString.data()); folly::doNotOptimizeAway(outputString.data());
} }
...@@ -99,7 +99,7 @@ BENCHMARK_DRAW_LINE() ...@@ -99,7 +99,7 @@ BENCHMARK_DRAW_LINE()
BENCHMARK(ipv6_to_fully_qualified_port, iters) { BENCHMARK(ipv6_to_fully_qualified_port, iters) {
IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725"); IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
while (iters--) { while (iters--) {
string outputString = to<std::string>(ip.toFullyQualified(), ':', 65535); string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535);
folly::doNotOptimizeAway(outputString); folly::doNotOptimizeAway(outputString);
folly::doNotOptimizeAway(outputString.data()); folly::doNotOptimizeAway(outputString.data());
} }
......
...@@ -781,8 +781,7 @@ TEST(IPAddress, V6Types) { ...@@ -781,8 +781,7 @@ TEST(IPAddress, V6Types) {
EXPECT_FALSE(ipv6.isTeredo()) << "isTeredo was true"; EXPECT_FALSE(ipv6.isTeredo()) << "isTeredo was true";
break; break;
default: default:
throw std::range_error("Invalid expected type: " + FAIL() << "Invalid expected type: " << to<std::string>(tc.second);
folly::to<std::string>(tc.second));
} }
} }
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include <folly/Conv.h> #include <folly/Format.h>
#include <folly/IPAddressV6.h> #include <folly/IPAddressV6.h>
#include <folly/MacAddress.h> #include <folly/MacAddress.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
...@@ -138,7 +138,7 @@ TEST(MacAddress, createMulticast) { ...@@ -138,7 +138,7 @@ TEST(MacAddress, createMulticast) {
} }
void testCmp(const char* str1, const char* str2) { void testCmp(const char* str1, const char* str2) {
SCOPED_TRACE(folly::to<std::string>(str1, " < ", str2)); SCOPED_TRACE(folly::sformat("{} < {}", str1, str2));
MacAddress m1(str1); MacAddress m1(str1);
MacAddress m2(str2); MacAddress m2(str2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment