Improve folly::RequestContext onSet and onUnset efficiency
[folly.git] / folly / IPAddressV4.cpp
index bcd582df89280ea5dd9d09298a36359561e8a60a..c373e092a77683db999c0b9252f960349eee3a5a 100644 (file)
@@ -29,7 +29,6 @@ using std::string;
 
 namespace folly {
 
-
 // free functions
 size_t hash_value(const IPAddressV4& addr) {
   return addr.hash();
@@ -74,7 +73,7 @@ uint32_t IPAddressV4::toLong(StringPiece ip) {
   in_addr addr;
   if (inet_pton(AF_INET, str.c_str(), &addr) != 1) {
     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;
 }
@@ -85,38 +84,27 @@ uint32_t IPAddressV4::toLongHBO(StringPiece ip) {
 }
 
 // public default constructor
-IPAddressV4::IPAddressV4() {
-}
+IPAddressV4::IPAddressV4() {}
 
 // ByteArray4 constructor
-IPAddressV4::IPAddressV4(const ByteArray4& src)
-  : addr_(src)
-{
-}
+IPAddressV4::IPAddressV4(const ByteArray4& src) : addr_(src) {}
 
 // public string constructor
-IPAddressV4::IPAddressV4(StringPiece addr)
-  : addr_()
-{
+IPAddressV4::IPAddressV4(StringPiece addr) : addr_() {
   auto ip = addr.str();
   if (inet_pton(AF_INET, ip.c_str(), &addr_.inAddr_) != 1) {
-    throw IPAddressFormatException(
-        to<std::string>("Invalid IPv4 address '", addr, "'"));
+    throw IPAddressFormatException(sformat("Invalid IPv4 address '{}'", addr));
   }
 }
 
 // in_addr constructor
-IPAddressV4::IPAddressV4(const in_addr src)
-  : addr_(src)
-{
-}
+IPAddressV4::IPAddressV4(const in_addr src) : addr_(src) {}
 
 // public
 void IPAddressV4::setFromBinary(ByteRange bytes) {
   if (bytes.size() != 4) {
-    throw IPAddressFormatException(to<std::string>(
-        "Invalid IPv4 binary data: length must ",
-        "be 4 bytes, got ",
+    throw IPAddressFormatException(sformat(
+        "Invalid IPv4 binary data: length must be 4 bytes, got {}",
         bytes.size()));
   }
   memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr));
@@ -159,8 +147,7 @@ IPAddressV6 IPAddressV4::getIPv6For6To4() const {
 
 // public
 string IPAddressV4::toJson() const {
-  return format(
-      "{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash()).str();
+  return sformat("{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash());
 }
 
 // public
@@ -168,18 +155,18 @@ bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const {
   auto subnetInfo = IPAddress::createNetwork(cidrNetwork);
   auto addr = subnetInfo.first;
   if (!addr.isV4()) {
-    throw IPAddressFormatException(to<std::string>(
-        "Address '", addr.toJson(), "' ", "is not a V4 address"));
+    throw IPAddressFormatException(
+        sformat("Address '{}' is not a V4 address", addr.toJson()));
   }
   return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second));
 }
 
 // public
-bool IPAddressV4::inSubnetWithMask(const IPAddressV4& subnet,
-                                   const ByteArray4 cidrMask) const {
-  const ByteArray4 mask = detail::Bytes::mask(toByteArray(), cidrMask);
-  const ByteArray4 subMask = detail::Bytes::mask(subnet.toByteArray(),
-                                                 cidrMask);
+bool IPAddressV4::inSubnetWithMask(
+    const IPAddressV4& subnet,
+    const ByteArray4 cidrMask) const {
+  const auto mask = detail::Bytes::mask(toByteArray(), cidrMask);
+  const auto subMask = detail::Bytes::mask(subnet.toByteArray(), cidrMask);
   return (mask == subMask);
 }
 
@@ -199,24 +186,26 @@ bool IPAddressV4::isLinkLocal() const {
 bool IPAddressV4::isNonroutable() const {
   auto ip = toLongHBO();
   return isPrivate() ||
-      (ip <= 0x00FFFFFF)                     || // 0.0.0.0-0.255.255.255
+      (/* align */ true && ip <= 0x00FFFFFF) || // 0.0.0.0-0.255.255.255
       (ip >= 0xC0000000 && ip <= 0xC00000FF) || // 192.0.0.0-192.0.0.255
       (ip >= 0xC0000200 && ip <= 0xC00002FF) || // 192.0.2.0-192.0.2.255
       (ip >= 0xC6120000 && ip <= 0xC613FFFF) || // 198.18.0.0-198.19.255.255
       (ip >= 0xC6336400 && ip <= 0xC63364FF) || // 198.51.100.0-198.51.100.255
       (ip >= 0xCB007100 && ip <= 0xCB0071FF) || // 203.0.113.0-203.0.113.255
-      (ip >= 0xE0000000 && ip <= 0xFFFFFFFF);   // 224.0.0.0-255.255.255.255
+      (ip >= 0xE0000000 && ip <= 0xFFFFFFFF) || // 224.0.0.0-255.255.255.255
+      false;
 }
 
 // public
 bool IPAddressV4::isPrivate() const {
   auto ip = toLongHBO();
-  return
+  return // some ranges below
       (ip >= 0x0A000000 && ip <= 0x0AFFFFFF) || // 10.0.0.0-10.255.255.255
       (ip >= 0x7F000000 && ip <= 0x7FFFFFFF) || // 127.0.0.0-127.255.255.255
       (ip >= 0xA9FE0000 && ip <= 0xA9FEFFFF) || // 169.254.0.0-169.254.255.255
       (ip >= 0xAC100000 && ip <= 0xAC1FFFFF) || // 172.16.0.0-172.31.255.255
-      (ip >= 0xC0A80000 && ip <= 0xC0A8FFFF);   // 192.168.0.0-192.168.255.255
+      (ip >= 0xC0A80000 && ip <= 0xC0A8FFFF) || // 192.168.0.0-192.168.255.255
+      false;
 }
 
 // public
@@ -229,7 +218,7 @@ IPAddressV4 IPAddressV4::mask(size_t numBits) const {
   static const auto bits = bitCount();
   if (numBits > bits) {
     throw IPAddressFormatException(
-        to<std::string>("numBits(", numBits, ") > bitsCount(", bits, ")"));
+        sformat("numBits({}) > bitsCount({})", numBits, bits));
   }
 
   ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
@@ -260,8 +249,9 @@ string IPAddressV4::toInverseArpaName() const {
 uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
   const auto highestIndex = byteCount() - 1;
   if (byteIndex > highestIndex) {
-    throw std::invalid_argument(to<string>("Byte index must be <= ",
-        to<string>(highestIndex), " for addresses of type :",
+    throw std::invalid_argument(sformat(
+        "Byte index must be <= {} for addresses of type: {}",
+        highestIndex,
         detail::familyNameStr(AF_INET)));
   }
   return bytes()[byteIndex];
@@ -270,8 +260,7 @@ uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
 const ByteArray4 IPAddressV4::fetchMask(size_t numBits) {
   static const size_t bits = bitCount();
   if (numBits > bits) {
-    throw IPAddressFormatException(
-        to<std::string>("IPv4 addresses are 32 bits"));
+    throw IPAddressFormatException("IPv4 addresses are 32 bits");
   }
   auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits)));
   ByteArray4 arr;
@@ -287,4 +276,4 @@ CIDRNetworkV4 IPAddressV4::longestCommonPrefix(
   return {IPAddressV4(prefix.first), prefix.second};
 }
 
-} // folly
+} // namespace folly