support -nan in conversion
authorFan Guo <fguo@fb.com>
Mon, 10 Sep 2012 17:08:46 +0000 (10:08 -0700)
committerJordan DeLong <jdelong@fb.com>
Mon, 17 Sep 2012 01:30:58 +0000 (18:30 -0700)
Summary:
Folly supports -inf and nan but not -nan, instead it raised unexpected exceptions not handled in the upstream.

This diff is separated out from D569816 -- Diff1.

Test Plan: std::isnan(folly::to<double>(-nan))

Reviewed By: tudorb@fb.com

FB internal diff: D569939

folly/Conv.h

index c64b52efc356bbf027c8b4c7db07c447e2df6095..98c66b3d38721f2569878b62b5999d7eb3ca77ab 100644 (file)
@@ -836,6 +836,16 @@ to(StringPiece *const src) {
     return std::numeric_limits<Tgt>::quiet_NaN();
   }
 
+  // "-nan"?
+  if (src->size() >= 4 &&
+      toupper((*src)[0]) == '-' &&
+      toupper((*src)[1]) == 'N' &&
+      toupper((*src)[2]) == 'A' &&
+      toupper((*src)[3]) == 'N') {
+    src->advance(4);
+    return -std::numeric_limits<Tgt>::quiet_NaN();
+  }
+
   // All bets are off
   throw std::range_error("Unable to convert \"" + src->toString()
                          + "\" to a floating point value.");