Skip to content

Commit

Permalink
Apply Nlohmann's fix in nlohmann/json#631 to address localeconv() and…
Browse files Browse the repository at this point in the history
… lconv not being available on some versions of the Android SDK.
  • Loading branch information
mkoscumb committed Jul 18, 2019
1 parent 9625fd4 commit 4519f02
Showing 1 changed file with 0 additions and 62 deletions.
62 changes: 0 additions & 62 deletions lib/include/mat/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8331,35 +8331,6 @@ class basic_json
// check if buffer was large enough
assert(static_cast<size_t>(written_bytes) < m_buf.size());

// read information from locale
const auto loc = localeconv();
assert(loc != nullptr);
const char thousands_sep = !loc->thousands_sep ? '\0'
: loc->thousands_sep[0];

const char decimal_point = !loc->decimal_point ? '\0'
: loc->decimal_point[0];

// erase thousands separator
if (thousands_sep != '\0')
{
const auto end = std::remove(m_buf.begin(), m_buf.begin() + written_bytes, thousands_sep);
std::fill(end, m_buf.end(), '\0');
}

// convert decimal point to '.'
if (decimal_point != '\0' and decimal_point != '.')
{
for (auto& c : m_buf)
{
if (c == decimal_point)
{
c = '.';
break;
}
}
}

// determine if need to append ".0"
size_t i = 0;
bool value_is_int_like = true;
Expand Down Expand Up @@ -11146,46 +11117,13 @@ class basic_json
// when necessary; data will point to either the original
// string, or buf, or tempstr containing the fixed string.
std::string tempstr;
std::array<char, 64> buf;
const size_t len = static_cast<size_t>(m_end - m_start);

// lexer will reject empty numbers
assert(len > 0);

// since dealing with strtod family of functions, we're
// getting the decimal point char from the C locale facilities
// instead of C++'s numpunct facet of the current std::locale
const auto loc = localeconv();
assert(loc != nullptr);
const char decimal_point_char = (loc->decimal_point == nullptr) ? '.' : loc->decimal_point[0];

const char* data = m_start;

if (decimal_point_char != '.')
{
const size_t ds_pos = static_cast<size_t>(std::find(m_start, m_end, '.') - m_start);

if (ds_pos != len)
{
// copy the data into the local buffer or tempstr, if
// buffer is too small; replace decimal separator, and
// update data to point to the modified bytes
if ((len + 1) < buf.size())
{
std::copy(m_start, m_end, buf.begin());
buf[len] = 0;
buf[ds_pos] = decimal_point_char;
data = buf.data();
}
else
{
tempstr.assign(m_start, m_end);
tempstr[ds_pos] = decimal_point_char;
data = tempstr.c_str();
}
}
}

char* endptr = nullptr;
value = 0;
// this calls appropriate overload depending on T
Expand Down

0 comments on commit 4519f02

Please sign in to comment.