Browse files

support -nan in conversion

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
  • Loading branch information...
1 parent 8a64dcb commit 78ee28054f94760f2f1d27380c452ee964b427b4 Fan Guo committed with jdelong Sep 10, 2012
Showing with 10 additions and 0 deletions.
  1. +10 −0 folly/Conv.h
View
10 folly/Conv.h
@@ -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.");

0 comments on commit 78ee280

Please sign in to comment.