Skip to content

Commit

Permalink
wide_string_input_adapter fallback to get_character
Browse files Browse the repository at this point in the history
  • Loading branch information
TianyiChen committed Jun 8, 2024
1 parent ea8b03d commit fa2712d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 8 additions & 4 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class input_stream_adapter
template<class T>
std::size_t get_elements(T* dest, std::size_t count = 1)
{
auto res = sb->sgetn(reinterpret_cast<char*>(dest), count * sizeof(T));
auto res = sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T)));
if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
{
is->clear(is->rdstate() | std::ios::eofbit);
Expand Down Expand Up @@ -359,10 +359,14 @@ class wide_string_input_adapter
}

template<class T>
std::size_t get_elements(T*, std::size_t = 1)
std::size_t get_elements(T* dest, std::size_t count = 1)
{
JSON_THROW(other_error::create(500, "Unexpected get_elements call to wchar input adapter", nullptr));
return 0;
auto ptr = reinterpret_cast<char*>(dest);
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
{
ptr[read_index] = get_character();
}
return count * sizeof(T);
}

private:
Expand Down
10 changes: 7 additions & 3 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6281,7 +6281,7 @@ class input_stream_adapter
template<class T>
std::size_t get_elements(T* dest, std::size_t count = 1)
{
auto res = sb->sgetn(reinterpret_cast<char*>(dest), count * sizeof(T));
auto res = sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T)));
if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
{
is->clear(is->rdstate() | std::ios::eofbit);
Expand Down Expand Up @@ -6506,8 +6506,12 @@ class wide_string_input_adapter
template<class T>
std::size_t get_elements(T* dest, std::size_t count = 1)
{
JSON_THROW(other_error::create(500, "Unexpected get_elements call to wchar input adapter", nullptr));
return 0;
auto ptr = reinterpret_cast<char*>(dest);
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
{
ptr[read_index] = get_character();
}
return count * sizeof(T);
}

private:
Expand Down

0 comments on commit fa2712d

Please sign in to comment.