From 8bd42a1a98377b717bfa3fae6efde4f72c18fea4 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Tue, 14 Feb 2017 19:06:37 +0000 Subject: [Support] Add StringRef::getAsDouble. Differential Revision: https://reviews.llvm.org/D29918 llvm-svn: 295089 --- llvm/lib/Support/StringRef.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'llvm/lib/Support/StringRef.cpp') diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index d81250e48dd..9b7cc1c1d18 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/edit_distance.h" @@ -595,6 +596,18 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { return false; } +bool StringRef::getAsDouble(double &Result, bool AllowInexact) const { + APFloat F(0.0); + APFloat::opStatus Status = + F.convertFromString(*this, APFloat::rmNearestTiesToEven); + if (Status != APFloat::opOK) { + if (!AllowInexact || Status != APFloat::opInexact) + return true; + } + + Result = F.convertToDouble(); + return false; +} // Implementation of StringRef hashing. hash_code llvm::hash_value(StringRef S) { -- cgit v1.2.3