Skip to content

Commit

Permalink
addressed locale-issues #107
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Aug 4, 2015
1 parent c910853 commit 2c32593
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
17 changes: 9 additions & 8 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation.
#include <iostream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -6932,18 +6933,18 @@ class basic_json
read past the current token. The latter case needs to be treated by the
caller function.
@note This function is locale-independent, see
http://stackoverflow.com/a/1333899/266378
@throw std::range_error if passed value is out of range
*/
long double get_number() const
{
// conversion
typename string_t::value_type* endptr;
const auto float_val = std::strtold(reinterpret_cast<typename string_t::const_pointer>(m_start),
&endptr);

// return float_val if the whole number was translated and NAN
// otherwise
return (reinterpret_cast<lexer_char_t*>(endptr) == m_cursor) ? float_val : NAN;
long double f = NAN;
std::stringstream ss(get_token());
ss.imbue(std::locale("C"));
ss >> f;
return f;
}

private:
Expand Down
17 changes: 9 additions & 8 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation.
#include <iostream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -6238,18 +6239,18 @@ class basic_json
read past the current token. The latter case needs to be treated by the
caller function.

@note This function is locale-independent, see
http://stackoverflow.com/a/1333899/266378

@throw std::range_error if passed value is out of range
*/
long double get_number() const
{
// conversion
typename string_t::value_type* endptr;
const auto float_val = std::strtold(reinterpret_cast<typename string_t::const_pointer>(m_start),
&endptr);

// return float_val if the whole number was translated and NAN
// otherwise
return (reinterpret_cast<lexer_char_t*>(endptr) == m_cursor) ? float_val : NAN;
long double f = NAN;
std::stringstream ss(get_token());
ss.imbue(std::locale("C"));
ss >> f;
return f;
}

private:
Expand Down

0 comments on commit 2c32593

Please sign in to comment.