Expected coroutines support
[folly.git] / folly / Format.cpp
index 177f9783e4afa4de4cf55ddac3205f30f8d15dec..380cddbe418788ecdfabad7e686c820997661fa0 100644 (file)
@@ -16,7 +16,8 @@
 
 #include <folly/Format.h>
 
-#include <folly/portability/Constexpr.h>
+#include <folly/ConstexprMath.h>
+#include <folly/CppAttributes.h>
 
 #include <double-conversion/double-conversion.h>
 
@@ -26,7 +27,7 @@ namespace detail {
 extern const FormatArg::Align formatAlignTable[];
 extern const FormatArg::Sign formatSignTable[];
 
-}  // namespace detail
+} // namespace detail
 
 using namespace folly::detail;
 
@@ -50,14 +51,14 @@ void FormatValue<double>::formatHelper(
   }
 
   // 2+: for null terminator and optional sign shenanigans.
-  constexpr size_t bufLen =
+  constexpr int bufLen =
       2 + constexpr_max(
               2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
                   DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
               constexpr_max(8 + DoubleToStringConverter::kMaxExponentialDigits,
                             7 + DoubleToStringConverter::kMaxPrecisionDigits));
   char buf[bufLen];
-  StringBuilder builder(buf + 1, static_cast<int> (sizeof(buf) - 1));
+  StringBuilder builder(buf + 1, bufLen - 1);
 
   char plusSign;
   switch (arg.sign) {
@@ -81,6 +82,7 @@ void FormatValue<double>::formatHelper(
   switch (arg.presentation) {
   case '%':
     val *= 100;
+    FOLLY_FALLTHROUGH;
   case 'f':
   case 'F':
     {
@@ -159,7 +161,7 @@ void FormatValue<double>::formatHelper(
     prefixLen = 1;
   }
 
-  piece = fbstring(p, len);
+  piece = fbstring(p, size_t(len));
 }
 
 
@@ -168,7 +170,7 @@ void FormatArg::initSlow() {
   auto end = fullArgString.end();
 
   // Parse key
-  auto p = static_cast<const char*>(memchr(b, ':', end - b));
+  auto p = static_cast<const char*>(memchr(b, ':', size_t(end - b)));
   if (!p) {
     key_ = StringPiece(b, end);
     return;
@@ -177,7 +179,9 @@ void FormatArg::initSlow() {
 
   if (*p == ':') {
     // parse format spec
-    if (++p == end) return;
+    if (++p == end) {
+      return;
+    }
 
     // fill/align, or just align
     Align a;
@@ -187,30 +191,40 @@ void FormatArg::initSlow() {
       fill = *p;
       align = a;
       p += 2;
-      if (p == end) return;
+      if (p == end) {
+        return;
+      }
     } else if ((a = formatAlignTable[static_cast<unsigned char>(*p)]) !=
                Align::INVALID) {
       align = a;
-      if (++p == end) return;
+      if (++p == end) {
+        return;
+      }
     }
 
     Sign s;
     unsigned char uSign = static_cast<unsigned char>(*p);
     if ((s = formatSignTable[uSign]) != Sign::INVALID) {
       sign = s;
-      if (++p == end) return;
+      if (++p == end) {
+        return;
+      }
     }
 
     if (*p == '#') {
       basePrefix = true;
-      if (++p == end) return;
+      if (++p == end) {
+        return;
+      }
     }
 
     if (*p == '0') {
       enforce(align == Align::DEFAULT, "alignment specified twice");
       fill = '0';
       align = Align::PAD_AFTER_SIGN;
-      if (++p == end) return;
+      if (++p == end) {
+        return;
+      }
     }
 
     auto readInt = [&] {
@@ -225,20 +239,30 @@ void FormatArg::initSlow() {
       width = kDynamicWidth;
       ++p;
 
-      if (p == end) return;
+      if (p == end) {
+        return;
+      }
 
-      if (*p >= '0' && *p <= '9') widthIndex = readInt();
+      if (*p >= '0' && *p <= '9') {
+        widthIndex = readInt();
+      }
 
-      if (p == end) return;
+      if (p == end) {
+        return;
+      }
     } else if (*p >= '0' && *p <= '9') {
       width = readInt();
 
-      if (p == end) return;
+      if (p == end) {
+        return;
+      }
     }
 
     if (*p == ',') {
       thousandsSeparator = true;
-      if (++p == end) return;
+      if (++p == end) {
+        return;
+      }
     }
 
     if (*p == '.') {
@@ -256,11 +280,15 @@ void FormatArg::initSlow() {
         trailingDot = true;
       }
 
-      if (p == end) return;
+      if (p == end) {
+        return;
+      }
     }
 
     presentation = *p;
-    if (++p == end) return;
+    if (++p == end) {
+      return;
+    }
   }
 
   error("extra characters in format string");
@@ -326,6 +354,6 @@ void insertThousandsGroupingUnsafe(char* start_buffer, char** end_buffer) {
     remaining_digits -= current_group_size;
   }
 }
-} // detail
+} // namespace detail
 
-}  // namespace folly
+} // namespace folly