Commit cc4ec64c by Zhenyao Mo Committed by Jamie Madill

Use the same mechanism to process int/float literals

This also fixes the float overflow errno leaking bug. BUG= R=alokp@chromium.org Review URL: https://codereview.appspot.com/13368050
parent ac44cd2b
#define MAJOR_VERSION 2 #define MAJOR_VERSION 2
#define MINOR_VERSION 0 #define MINOR_VERSION 0
#define BUILD_VERSION 0 #define BUILD_VERSION 0
#define BUILD_REVISION 2014 #define BUILD_REVISION 2015
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x) #define MACRO_STRINGIFY(x) STRINGIFY(x)
......
...@@ -4,39 +4,15 @@ ...@@ -4,39 +4,15 @@
// found in the LICENSE file. // found in the LICENSE file.
// //
#include <math.h> #include "compiler/util.h"
#include <stdlib.h>
#include <cerrno>
#include <limits> #include <limits>
#include "util.h" #include "compiler/preprocessor/numeric_lex.h"
#ifdef _MSC_VER
#include <locale.h>
#else
#include <sstream>
#endif
bool atof_clamp(const char *str, float *value) bool atof_clamp(const char *str, float *value)
{ {
bool success = true; bool success = pp::numeric_lex_float(str, value);
#ifdef _MSC_VER
_locale_t l = _create_locale(LC_NUMERIC, "C");
double dvalue = _atof_l(str, l);
_free_locale(l);
if (errno == ERANGE || dvalue > std::numeric_limits<float>::max())
success = false;
else
*value = static_cast<float>(dvalue);
#else
std::istringstream s(str);
std::locale l("C");
s.imbue(l);
s >> *value;
if (s.fail())
success = false;
#endif
if (!success) if (!success)
*value = std::numeric_limits<float>::max(); *value = std::numeric_limits<float>::max();
return success; return success;
...@@ -44,13 +20,9 @@ bool atof_clamp(const char *str, float *value) ...@@ -44,13 +20,9 @@ bool atof_clamp(const char *str, float *value)
bool atoi_clamp(const char *str, int *value) bool atoi_clamp(const char *str, int *value)
{ {
long int lvalue = strtol(str, 0, 0); bool success = pp::numeric_lex_int(str, value);
if (errno == ERANGE || lvalue > std::numeric_limits<int>::max()) if (!success)
{
*value = std::numeric_limits<int>::max(); *value = std::numeric_limits<int>::max();
return false; return success;
}
*value = static_cast<int>(lvalue);
return true;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment