Fix DCHECKs in IOBufQueue
[folly.git] / folly / Conv.cpp
index 0ab6dcf50470bd1108714160b4cf7a528beba6dc..8e2f575293475d07485302ee28a2a8b12ba7c952 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -235,7 +235,7 @@ using IsAscii = std::
 // The code in this file that uses tolower() really only cares about
 // 7-bit ASCII characters, so we can take a nice shortcut here.
 inline char tolower_ascii(char in) {
-  return IsAscii::value ? in | 0x20 : std::tolower(in);
+  return IsAscii::value ? in | 0x20 : char(std::tolower(in));
 }
 
 inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
@@ -255,7 +255,7 @@ inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
   return true;
 }
 
-} // anonymous namespace
+} // namespace
 
 Expected<bool, ConversionCode> str_to_bool(StringPiece* src) noexcept {
   auto b = src->begin(), e = src->end();
@@ -269,7 +269,7 @@ Expected<bool, ConversionCode> str_to_bool(StringPiece* src) noexcept {
   }
 
   bool result;
-  size_t len = e - b;
+  size_t len = size_t(e - b);
   switch (*b) {
     case '0':
     case '1': {
@@ -361,11 +361,12 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept {
     // want to raise an error; length will point past the last character
     // that was processed, so we need to check if that character was
     // whitespace or not.
-    if (length == 0 || (result == 0.0 && std::isspace((*src)[length - 1]))) {
+    if (length == 0 ||
+        (result == 0.0 && std::isspace((*src)[size_t(length) - 1]))) {
       return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
     }
-    src->advance(length);
-    return result;
+    src->advance(size_t(length));
+    return Tgt(result);
   }
 
   auto* e = src->end();
@@ -374,7 +375,7 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept {
 
   // There must be non-whitespace, otherwise we would have caught this above
   assert(b < e);
-  size_t size = e - b;
+  size_t size = size_t(e - b);
 
   bool negative = false;
   if (*b == '-') {
@@ -423,7 +424,7 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept {
 
   src->assign(b, e);
 
-  return result;
+  return Tgt(result);
 }
 
 template Expected<float, ConversionCode> str_to_floating<float>(
@@ -463,12 +464,12 @@ class SignedValueHandler<T, true> {
   Expected<T, ConversionCode> finalize(U value) {
     T rv;
     if (negative_) {
-      rv = -value;
+      rv = T(-value);
       if (UNLIKELY(rv > 0)) {
         return makeUnexpected(ConversionCode::NEGATIVE_OVERFLOW);
       }
     } else {
-      rv = value;
+      rv = T(value);
       if (UNLIKELY(rv < 0)) {
         return makeUnexpected(ConversionCode::POSITIVE_OVERFLOW);
       }
@@ -518,7 +519,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
     return makeUnexpected(err);
   }
 
-  size_t size = e - b;
+  size_t size = size_t(e - b);
 
   /* Although the string is entirely made of digits, we still need to
    * check for overflow.
@@ -531,7 +532,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
           return Tgt(0); // just zeros, e.g. "0000"
         }
         if (*b != '0') {
-          size = e - b;
+          size = size_t(e - b);
           break;
         }
       }
@@ -549,7 +550,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
   UT result = 0;
 
   for (; e - b >= 4; b += 4) {
-    result *= 10000;
+    result *= static_cast<UT>(10000);
     const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
     const int32_t r1 = shift100[static_cast<size_t>(b[1])];
     const int32_t r2 = shift10[static_cast<size_t>(b[2])];
@@ -558,7 +559,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
     if (sum >= OOR) {
       goto outOfRange;
     }
-    result += sum;
+    result += UT(sum);
   }
 
   switch (e - b) {
@@ -570,7 +571,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
     if (sum >= OOR) {
       goto outOfRange;
     }
-    result = 1000 * result + sum;
+    result = UT(1000 * result + sum);
     break;
   }
   case 2: {
@@ -580,7 +581,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
     if (sum >= OOR) {
       goto outOfRange;
     }
-    result = 100 * result + sum;
+    result = UT(100 * result + sum);
     break;
   }
   case 1: {
@@ -588,7 +589,7 @@ inline Expected<Tgt, ConversionCode> digits_to(
     if (sum >= OOR) {
       goto outOfRange;
     }
-    result = 10 * result + sum;
+    result = UT(10 * result + sum);
     break;
   }
   default:
@@ -631,7 +632,7 @@ template Expected<unsigned int, ConversionCode> digits_to<unsigned int>(
 
 template Expected<long, ConversionCode> digits_to<long>(
     const char*,
-    const char*);
+    const char*) noexcept;
 template Expected<unsigned long, ConversionCode> digits_to<unsigned long>(
     const char*,
     const char*) noexcept;
@@ -695,7 +696,7 @@ Expected<Tgt, ConversionCode> str_to_integral(StringPiece* src) noexcept {
   auto res = sgn.finalize(tmp.value());
 
   if (res.hasValue()) {
-    src->advance(m - src->data());
+    src->advance(size_t(m - src->data()));
   }
 
   return res;