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 MINOR_VERSION 0
#define BUILD_VERSION 0
#define BUILD_REVISION 2014
#define BUILD_REVISION 2015
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
......
......@@ -4,39 +4,15 @@
// found in the LICENSE file.
//
#include <math.h>
#include <stdlib.h>
#include "compiler/util.h"
#include <cerrno>
#include <limits>
#include "util.h"
#ifdef _MSC_VER
#include <locale.h>
#else
#include <sstream>
#endif
#include "compiler/preprocessor/numeric_lex.h"
bool atof_clamp(const char *str, float *value)
{
bool success = true;
#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
bool success = pp::numeric_lex_float(str, value);
if (!success)
*value = std::numeric_limits<float>::max();
return success;
......@@ -44,13 +20,9 @@ bool atof_clamp(const char *str, float *value)
bool atoi_clamp(const char *str, int *value)
{
long int lvalue = strtol(str, 0, 0);
if (errno == ERANGE || lvalue > std::numeric_limits<int>::max())
{
bool success = pp::numeric_lex_int(str, value);
if (!success)
*value = std::numeric_limits<int>::max();
return false;
}
*value = static_cast<int>(lvalue);
return true;
return success;
}
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