[llvm] r295089 - [Support] Add StringRef::getAsDouble.
Zachary Turner via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 14 11:06:37 PST 2017
Author: zturner
Date: Tue Feb 14 13:06:37 2017
New Revision: 295089
URL: http://llvm.org/viewvc/llvm-project?rev=295089&view=rev
Log:
[Support] Add StringRef::getAsDouble.
Differential Revision: https://reviews.llvm.org/D29918
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/lib/Support/StringRef.cpp
llvm/trunk/unittests/ADT/StringRefTest.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=295089&r1=295088&r2=295089&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Tue Feb 14 13:06:37 2017
@@ -557,6 +557,14 @@ namespace llvm {
/// string is well-formed in the given radix.
bool getAsInteger(unsigned Radix, APInt &Result) const;
+ /// Parse the current string as an IEEE double-precision floating
+ /// point value. The string must be a well-formed double.
+ ///
+ /// If \p AllowInexact is false, the function will fail if the string
+ /// cannot be represented exactly. Otherwise, the function only fails
+ /// in case of an overflow or underflow.
+ bool getAsDouble(double &Result, bool AllowInexact = true) const;
+
/// @}
/// @name String Operations
/// @{
Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=295089&r1=295088&r2=295089&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Tue Feb 14 13:06:37 2017
@@ -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 Ra
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) {
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=295089&r1=295088&r2=295089&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Tue Feb 14 13:06:37 2017
@@ -852,6 +852,27 @@ TEST(StringRefTest, consumeIntegerSigned
}
}
+struct GetDoubleStrings {
+ const char *Str;
+ bool AllowInexact;
+ bool ShouldFail;
+ double D;
+} DoubleStrings[] = {{"0", false, false, 0.0},
+ {"0.0", false, false, 0.0},
+ {"-0.0", false, false, -0.0},
+ {"123.45", false, true, 123.45},
+ {"123.45", true, false, 123.45}};
+
+TEST(StringRefTest, getAsDouble) {
+ for (const auto &Entry : DoubleStrings) {
+ double Result;
+ StringRef S(Entry.Str);
+ EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
+ if (!Entry.ShouldFail)
+ EXPECT_EQ(Result, Entry.D);
+ }
+}
+
static const char *join_input[] = { "a", "b", "c" };
static const char join_result1[] = "a";
static const char join_result2[] = "a:b:c";
More information about the llvm-commits
mailing list