Index: llvm/trunk/include/llvm/ADT/StringExtras.h =================================================================== --- llvm/trunk/include/llvm/ADT/StringExtras.h +++ llvm/trunk/include/llvm/ADT/StringExtras.h @@ -15,10 +15,12 @@ #define LLVM_ADT_STRINGEXTRAS_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include #include #include +#include #include #include #include @@ -129,6 +131,32 @@ return !S.getAsInteger(Base, Num); } +namespace detail { +template +inline bool to_float(const Twine &T, N &Num, N (*StrTo)(const char *, char **)) { + SmallString<32> Storage; + StringRef S = T.toNullTerminatedStringRef(Storage); + char *End; + N Temp = StrTo(S.data(), &End); + if (*End != '\0') + return false; + Num = Temp; + return true; +} +} + +inline bool to_float(const Twine &T, float &Num) { + return detail::to_float(T, Num, std::strtof); +} + +inline bool to_float(const Twine &T, double &Num) { + return detail::to_float(T, Num, std::strtod); +} + +inline bool to_float(const Twine &T, long double &Num) { + return detail::to_float(T, Num, std::strtold); +} + static inline std::string utostr(uint64_t X, bool isNeg = false) { char Buffer[21]; char *BufPtr = std::end(Buffer); Index: llvm/trunk/lib/Support/CommandLine.cpp =================================================================== --- llvm/trunk/lib/Support/CommandLine.cpp +++ llvm/trunk/lib/Support/CommandLine.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/Config/config.h" @@ -1522,13 +1523,9 @@ // parser/parser implementation // static bool parseDouble(Option &O, StringRef Arg, double &Value) { - SmallString<32> TmpStr(Arg.begin(), Arg.end()); - const char *ArgStart = TmpStr.c_str(); - char *End; - Value = strtod(ArgStart, &End); - if (*End != 0) - return O.error("'" + Arg + "' value invalid for floating point argument!"); - return false; + if (to_float(Arg, Value)) + return false; + return O.error("'" + Arg + "' value invalid for floating point argument!"); } bool parser::parse(Option &O, StringRef ArgName, StringRef Arg, Index: llvm/trunk/lib/Support/YAMLTraits.cpp =================================================================== --- llvm/trunk/lib/Support/YAMLTraits.cpp +++ llvm/trunk/lib/Support/YAMLTraits.cpp @@ -10,6 +10,7 @@ #include "llvm/Support/YAMLTraits.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/Casting.h" @@ -912,12 +913,9 @@ } StringRef ScalarTraits::input(StringRef Scalar, void *, double &Val) { - SmallString<32> buff(Scalar.begin(), Scalar.end()); - char *end; - Val = strtod(buff.c_str(), &end); - if (*end != '\0') - return "invalid floating point number"; - return StringRef(); + if (to_float(Scalar, Val)) + return StringRef(); + return "invalid floating point number"; } void ScalarTraits::output(const float &Val, void *, raw_ostream &Out) { @@ -925,12 +923,9 @@ } StringRef ScalarTraits::input(StringRef Scalar, void *, float &Val) { - SmallString<32> buff(Scalar.begin(), Scalar.end()); - char *end; - Val = strtod(buff.c_str(), &end); - if (*end != '\0') - return "invalid floating point number"; - return StringRef(); + if (to_float(Scalar, Val)) + return StringRef(); + return "invalid floating point number"; } void ScalarTraits::output(const Hex8 &Val, void *, raw_ostream &Out) { Index: llvm/trunk/unittests/ADT/StringExtrasTest.cpp =================================================================== --- llvm/trunk/unittests/ADT/StringExtrasTest.cpp +++ llvm/trunk/unittests/ADT/StringExtrasTest.cpp @@ -65,4 +65,22 @@ EvenBytes.size()); EXPECT_EQ(EvenStr, toHex(EvenData)); EXPECT_EQ(EvenData, fromHex(EvenStr)); -} \ No newline at end of file +} + +TEST(StringExtrasTest, to_float) { + float F; + EXPECT_TRUE(to_float("4.7", F)); + EXPECT_FLOAT_EQ(4.7, F); + + double D; + EXPECT_TRUE(to_float("4.7", D)); + EXPECT_DOUBLE_EQ(4.7, D); + + long double LD; + EXPECT_TRUE(to_float("4.7", LD)); + EXPECT_DOUBLE_EQ(4.7, LD); + + EXPECT_FALSE(to_float("foo", F)); + EXPECT_FALSE(to_float("7.4 foo", F)); + EXPECT_FLOAT_EQ(4.7, F); // F should be unchanged +}