2018-01-10 12:18:31 +03:00
|
|
|
#pragma once
|
2017-08-14 17:48:55 +03:00
|
|
|
|
2019-03-17 02:27:44 +03:00
|
|
|
#include <array> // array
|
2017-08-14 20:28:01 +03:00
|
|
|
#include <cassert> // assert
|
|
|
|
#include <cstddef> // size_t
|
2019-03-17 14:01:49 +03:00
|
|
|
#include <cstdio> //FILE *
|
2017-08-14 20:28:01 +03:00
|
|
|
#include <cstring> // strlen
|
|
|
|
#include <istream> // istream
|
|
|
|
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
|
|
|
|
#include <memory> // shared_ptr, make_shared, addressof
|
|
|
|
#include <numeric> // accumulate
|
|
|
|
#include <string> // string, char_traits
|
|
|
|
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
|
|
|
|
#include <utility> // pair, declval
|
2017-08-14 17:48:55 +03:00
|
|
|
|
2019-01-13 17:31:22 +03:00
|
|
|
#include <nlohmann/detail/iterators/iterator_traits.hpp>
|
2018-01-29 13:21:11 +03:00
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
2018-03-21 00:39:08 +03:00
|
|
|
/// the supported input formats
|
2018-09-14 23:58:22 +03:00
|
|
|
enum class input_format_t { json, cbor, msgpack, ubjson, bson };
|
2018-03-21 00:39:08 +03:00
|
|
|
|
2017-08-14 17:48:55 +03:00
|
|
|
////////////////////
|
|
|
|
// input adapters //
|
|
|
|
////////////////////
|
|
|
|
|
2018-12-12 17:28:42 +03:00
|
|
|
/*!
|
|
|
|
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
|
2018-12-12 22:15:49 +03:00
|
|
|
buffer. This adapter is a very low level adapter.
|
2018-12-12 17:28:42 +03:00
|
|
|
*/
|
2020-02-19 22:59:31 +03:00
|
|
|
class file_input_adapter
|
2018-12-11 21:17:13 +03:00
|
|
|
{
|
2018-12-12 18:18:37 +03:00
|
|
|
public:
|
2019-07-01 23:37:30 +03:00
|
|
|
JSON_HEDLEY_NON_NULL(2)
|
2018-12-13 00:33:25 +03:00
|
|
|
explicit file_input_adapter(std::FILE* f) noexcept
|
|
|
|
: m_file(f)
|
2018-12-11 21:17:13 +03:00
|
|
|
{}
|
|
|
|
|
2019-03-17 14:01:49 +03:00
|
|
|
// make class move-only
|
|
|
|
file_input_adapter(const file_input_adapter&) = delete;
|
2019-03-18 00:25:18 +03:00
|
|
|
file_input_adapter(file_input_adapter&&) = default;
|
2019-03-17 14:01:49 +03:00
|
|
|
file_input_adapter& operator=(const file_input_adapter&) = delete;
|
2020-03-03 07:50:01 +03:00
|
|
|
file_input_adapter& operator=(file_input_adapter&&) = delete;
|
2019-03-17 14:01:49 +03:00
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
std::char_traits<char>::int_type get_character() noexcept
|
2018-12-11 21:17:13 +03:00
|
|
|
{
|
2018-12-13 00:33:25 +03:00
|
|
|
return std::fgetc(m_file);
|
2018-12-11 21:17:13 +03:00
|
|
|
}
|
2019-03-17 14:01:49 +03:00
|
|
|
|
2018-12-12 18:18:37 +03:00
|
|
|
private:
|
2018-12-11 21:17:13 +03:00
|
|
|
/// the file pointer to read from
|
2018-12-13 00:33:25 +03:00
|
|
|
std::FILE* m_file;
|
2018-12-11 21:17:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-08-14 17:48:55 +03:00
|
|
|
/*!
|
|
|
|
Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
|
|
|
|
beginning of input. Does not support changing the underlying std::streambuf
|
|
|
|
in mid-input. Maintains underlying std::istream and std::streambuf to support
|
|
|
|
subsequent use of standard std::istream operations to process any input
|
|
|
|
characters following those used in parsing the JSON input. Clears the
|
|
|
|
std::istream flags; any input errors (e.g., EOF) will be detected by the first
|
|
|
|
subsequent call for input from the std::istream.
|
|
|
|
*/
|
2020-02-19 22:59:31 +03:00
|
|
|
class input_stream_adapter
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
|
|
|
public:
|
2020-02-19 22:59:31 +03:00
|
|
|
~input_stream_adapter()
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
|
|
|
// clear stream flags; we use underlying streambuf I/O, do not
|
2018-11-07 18:02:50 +03:00
|
|
|
// maintain ifstream flags, except eof
|
2020-02-19 22:59:31 +03:00
|
|
|
if (is)
|
|
|
|
{
|
|
|
|
is->clear(is->rdstate() & std::ios::eofbit);
|
|
|
|
}
|
2017-08-14 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit input_stream_adapter(std::istream& i)
|
2020-02-19 22:59:31 +03:00
|
|
|
: is(&i), sb(i.rdbuf())
|
2018-04-02 22:10:48 +03:00
|
|
|
{}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
// delete because of pointer members
|
|
|
|
input_stream_adapter(const input_stream_adapter&) = delete;
|
|
|
|
input_stream_adapter& operator=(input_stream_adapter&) = delete;
|
2020-03-03 07:50:01 +03:00
|
|
|
input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete;
|
2020-02-19 22:59:31 +03:00
|
|
|
|
|
|
|
input_stream_adapter(input_stream_adapter&& rhs) : is(rhs.is), sb(rhs.sb)
|
|
|
|
{
|
|
|
|
rhs.is = nullptr;
|
|
|
|
rhs.sb = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:48:55 +03:00
|
|
|
// std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
|
|
|
|
// ensure that std::char_traits<char>::eof() and the character 0xFF do not
|
|
|
|
// end up as the same value, eg. 0xFFFFFFFF.
|
2020-02-19 22:59:31 +03:00
|
|
|
std::char_traits<char>::int_type get_character()
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
auto res = sb->sbumpc();
|
2018-11-07 18:02:50 +03:00
|
|
|
// set eof manually, as we don't use the istream interface.
|
|
|
|
if (res == EOF)
|
2018-11-09 23:10:32 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
is->clear(is->rdstate() | std::ios::eofbit);
|
2018-11-09 23:10:32 +03:00
|
|
|
}
|
2018-11-07 18:02:50 +03:00
|
|
|
return res;
|
2017-08-14 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// the associated input stream
|
2020-02-19 22:59:31 +03:00
|
|
|
std::istream* is = nullptr;
|
|
|
|
std::streambuf* sb = nullptr;
|
2017-08-14 17:48:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// input adapter for buffer input
|
2020-02-19 22:59:31 +03:00
|
|
|
class input_buffer_adapter
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
|
|
|
public:
|
2018-10-27 19:31:03 +03:00
|
|
|
input_buffer_adapter(const char* b, const std::size_t l) noexcept
|
2019-08-30 13:39:46 +03:00
|
|
|
: cursor(b), limit(b == nullptr ? nullptr : (b + l))
|
2018-04-02 22:10:48 +03:00
|
|
|
{}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
// delete because of pointer members
|
|
|
|
input_buffer_adapter(const input_buffer_adapter&) = delete;
|
|
|
|
input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter(input_buffer_adapter&&) = default;
|
2020-03-03 07:50:01 +03:00
|
|
|
input_buffer_adapter& operator=(input_buffer_adapter&&) = delete;
|
2017-08-14 17:48:55 +03:00
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
std::char_traits<char>::int_type get_character() noexcept
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
2019-07-01 23:37:30 +03:00
|
|
|
if (JSON_HEDLEY_LIKELY(cursor < limit))
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
2019-08-30 13:39:46 +03:00
|
|
|
assert(cursor != nullptr and limit != nullptr);
|
2017-08-14 17:48:55 +03:00
|
|
|
return std::char_traits<char>::to_int_type(*(cursor++));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::char_traits<char>::eof();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// pointer to the current character
|
|
|
|
const char* cursor;
|
|
|
|
/// pointer past the last character
|
2018-04-02 23:25:17 +03:00
|
|
|
const char* const limit;
|
2017-08-14 17:48:55 +03:00
|
|
|
};
|
|
|
|
|
2018-10-03 15:51:49 +03:00
|
|
|
template<typename WideStringType, size_t T>
|
|
|
|
struct wide_string_input_helper
|
2018-04-01 20:12:36 +03:00
|
|
|
{
|
2018-10-03 15:51:49 +03:00
|
|
|
// UTF-32
|
2019-03-17 02:27:44 +03:00
|
|
|
static void fill_buffer(const WideStringType& str,
|
|
|
|
size_t& current_wchar,
|
|
|
|
std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
|
|
|
|
size_t& utf8_bytes_index,
|
|
|
|
size_t& utf8_bytes_filled)
|
2018-04-01 20:12:36 +03:00
|
|
|
{
|
2018-10-03 15:51:49 +03:00
|
|
|
utf8_bytes_index = 0;
|
|
|
|
|
|
|
|
if (current_wchar == str.size())
|
|
|
|
{
|
|
|
|
utf8_bytes[0] = std::char_traits<char>::eof();
|
|
|
|
utf8_bytes_filled = 1;
|
|
|
|
}
|
|
|
|
else
|
2018-04-01 20:12:36 +03:00
|
|
|
{
|
2018-10-03 15:51:49 +03:00
|
|
|
// get the current character
|
2019-03-15 17:55:52 +03:00
|
|
|
const auto wc = static_cast<unsigned int>(str[current_wchar++]);
|
2018-04-01 20:12:36 +03:00
|
|
|
|
2018-10-03 15:51:49 +03:00
|
|
|
// UTF-32 to UTF-8 encoding
|
|
|
|
if (wc < 0x80)
|
2018-04-01 20:12:36 +03:00
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
|
2018-04-01 20:12:36 +03:00
|
|
|
utf8_bytes_filled = 1;
|
|
|
|
}
|
2018-10-03 15:51:49 +03:00
|
|
|
else if (wc <= 0x7FF)
|
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((wc >> 6u) & 0x1Fu));
|
|
|
|
utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
|
2018-10-03 15:51:49 +03:00
|
|
|
utf8_bytes_filled = 2;
|
|
|
|
}
|
|
|
|
else if (wc <= 0xFFFF)
|
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((wc >> 12u) & 0x0Fu));
|
|
|
|
utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
|
|
|
|
utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
|
2018-10-03 15:51:49 +03:00
|
|
|
utf8_bytes_filled = 3;
|
|
|
|
}
|
|
|
|
else if (wc <= 0x10FFFF)
|
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((wc >> 18u) & 0x07u));
|
|
|
|
utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 12u) & 0x3Fu));
|
|
|
|
utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
|
|
|
|
utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
|
2018-10-03 15:51:49 +03:00
|
|
|
utf8_bytes_filled = 4;
|
2018-04-01 20:12:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-03 15:51:49 +03:00
|
|
|
// unknown character
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
|
2018-10-03 15:51:49 +03:00
|
|
|
utf8_bytes_filled = 1;
|
2018-04-01 20:12:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 15:51:49 +03:00
|
|
|
};
|
2018-04-01 20:12:36 +03:00
|
|
|
|
2018-10-03 15:51:49 +03:00
|
|
|
template<typename WideStringType>
|
|
|
|
struct wide_string_input_helper<WideStringType, 2>
|
|
|
|
{
|
|
|
|
// UTF-16
|
2019-03-17 02:27:44 +03:00
|
|
|
static void fill_buffer(const WideStringType& str,
|
|
|
|
size_t& current_wchar,
|
|
|
|
std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
|
|
|
|
size_t& utf8_bytes_index,
|
|
|
|
size_t& utf8_bytes_filled)
|
2018-04-01 20:12:36 +03:00
|
|
|
{
|
|
|
|
utf8_bytes_index = 0;
|
|
|
|
|
|
|
|
if (current_wchar == str.size())
|
|
|
|
{
|
|
|
|
utf8_bytes[0] = std::char_traits<char>::eof();
|
|
|
|
utf8_bytes_filled = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get the current character
|
2019-03-15 17:55:52 +03:00
|
|
|
const auto wc = static_cast<unsigned int>(str[current_wchar++]);
|
2018-04-01 20:12:36 +03:00
|
|
|
|
|
|
|
// UTF-16 to UTF-8 encoding
|
|
|
|
if (wc < 0x80)
|
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
|
2018-04-01 20:12:36 +03:00
|
|
|
utf8_bytes_filled = 1;
|
|
|
|
}
|
|
|
|
else if (wc <= 0x7FF)
|
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((wc >> 6u)));
|
|
|
|
utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
|
2018-04-01 20:12:36 +03:00
|
|
|
utf8_bytes_filled = 2;
|
|
|
|
}
|
|
|
|
else if (0xD800 > wc or wc >= 0xE000)
|
|
|
|
{
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((wc >> 12u)));
|
|
|
|
utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
|
|
|
|
utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
|
2018-04-01 20:12:36 +03:00
|
|
|
utf8_bytes_filled = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (current_wchar < str.size())
|
|
|
|
{
|
2019-03-15 17:55:52 +03:00
|
|
|
const auto wc2 = static_cast<unsigned int>(str[current_wchar++]);
|
|
|
|
const auto charcode = 0x10000u + (((wc & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
|
|
|
|
utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
|
|
|
|
utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
|
|
|
|
utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
|
2018-04-01 20:12:36 +03:00
|
|
|
utf8_bytes_filled = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// unknown character
|
|
|
|
++current_wchar;
|
2019-03-17 02:27:44 +03:00
|
|
|
utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
|
2018-04-01 20:12:36 +03:00
|
|
|
utf8_bytes_filled = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 15:51:49 +03:00
|
|
|
};
|
2018-04-01 20:12:36 +03:00
|
|
|
|
2018-10-03 14:41:34 +03:00
|
|
|
template<typename WideStringType>
|
2020-02-19 22:59:31 +03:00
|
|
|
class wide_string_input_adapter
|
2018-10-03 14:41:34 +03:00
|
|
|
{
|
|
|
|
public:
|
2019-03-18 15:53:48 +03:00
|
|
|
explicit wide_string_input_adapter(const WideStringType& w) noexcept
|
2018-10-27 19:31:03 +03:00
|
|
|
: str(w)
|
|
|
|
{}
|
2018-04-01 20:12:36 +03:00
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
std::char_traits<char>::int_type get_character() noexcept
|
2018-10-03 14:41:34 +03:00
|
|
|
{
|
|
|
|
// check if buffer needs to be filled
|
|
|
|
if (utf8_bytes_index == utf8_bytes_filled)
|
2018-04-01 20:12:36 +03:00
|
|
|
{
|
2018-10-03 14:41:34 +03:00
|
|
|
fill_buffer<sizeof(typename WideStringType::value_type)>();
|
2018-04-01 20:12:36 +03:00
|
|
|
|
2018-10-03 14:41:34 +03:00
|
|
|
assert(utf8_bytes_filled > 0);
|
|
|
|
assert(utf8_bytes_index == 0);
|
2018-04-01 20:12:36 +03:00
|
|
|
}
|
2018-10-03 14:41:34 +03:00
|
|
|
|
|
|
|
// use buffer
|
|
|
|
assert(utf8_bytes_filled > 0);
|
|
|
|
assert(utf8_bytes_index < utf8_bytes_filled);
|
|
|
|
return utf8_bytes[utf8_bytes_index++];
|
2018-04-01 20:12:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-03 14:41:34 +03:00
|
|
|
template<size_t T>
|
|
|
|
void fill_buffer()
|
|
|
|
{
|
|
|
|
wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
|
|
|
|
}
|
2018-10-04 21:42:19 +03:00
|
|
|
|
2018-04-01 20:12:36 +03:00
|
|
|
/// the wstring to process
|
|
|
|
const WideStringType& str;
|
|
|
|
|
|
|
|
/// index of the current wchar in str
|
|
|
|
std::size_t current_wchar = 0;
|
|
|
|
|
|
|
|
/// a buffer for UTF-8 bytes
|
|
|
|
std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
|
|
|
|
|
|
|
|
/// index to the utf8_codes array for the next valid byte
|
|
|
|
std::size_t utf8_bytes_index = 0;
|
|
|
|
/// number of valid bytes in the utf8_codes array
|
|
|
|
std::size_t utf8_bytes_filled = 0;
|
|
|
|
};
|
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
inline file_input_adapter input_adapter(std::FILE* file)
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return file_input_adapter(file);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
inline input_stream_adapter input_adapter(std::istream& stream)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return input_stream_adapter(stream);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
inline input_stream_adapter input_adapter(std::istream&& stream)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return input_stream_adapter(stream);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CharT,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_pointer<CharT>::value and
|
|
|
|
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
int>::type = 0>
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter input_adapter(CharT b, std::size_t l)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return input_buffer_adapter(reinterpret_cast<const char*>(b), l);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CharT,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_pointer<CharT>::value and
|
|
|
|
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
int>::type = 0>
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter input_adapter(CharT b)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
|
|
|
return input_adapter(reinterpret_cast<const char*>(b),
|
|
|
|
std::strlen(reinterpret_cast<const char*>(b)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class IteratorType,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
|
|
|
|
int>::type = 0>
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter input_adapter(IteratorType first, IteratorType last)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// assertion to check that the iterator range is indeed contiguous,
|
|
|
|
// see https://stackoverflow.com/a/35008842/266378 for more discussion
|
|
|
|
const auto is_contiguous = std::accumulate(
|
|
|
|
first, last, std::pair<bool, int>(true, 0),
|
|
|
|
[&first](std::pair<bool, int> res, decltype(*first) val)
|
|
|
|
{
|
|
|
|
res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
|
|
|
|
return res;
|
|
|
|
}).first;
|
|
|
|
assert(is_contiguous);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// assertion to check that each element is 1 byte long
|
|
|
|
static_assert(
|
|
|
|
sizeof(typename iterator_traits<IteratorType>::value_type) == 1,
|
|
|
|
"each element in the iterator range must have the size of 1 byte");
|
|
|
|
|
|
|
|
const auto len = static_cast<size_t>(std::distance(first, last));
|
|
|
|
if (JSON_HEDLEY_LIKELY(len > 0))
|
|
|
|
{
|
|
|
|
// there is at least one element: use the address of first
|
2020-02-19 22:59:31 +03:00
|
|
|
return input_buffer_adapter(reinterpret_cast<const char*>(&(*first)), len);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// the address of first cannot be used: use nullptr
|
2020-02-19 22:59:31 +03:00
|
|
|
return input_buffer_adapter(nullptr, len);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
inline wide_string_input_adapter<std::wstring> input_adapter(const std::wstring& ws)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return wide_string_input_adapter<std::wstring>(ws);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:48:55 +03:00
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
inline wide_string_input_adapter<std::u16string> input_adapter(const std::u16string& ws)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return wide_string_input_adapter<std::u16string>(ws);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
inline wide_string_input_adapter<std::u32string> input_adapter(const std::u32string& ws)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return wide_string_input_adapter<std::u32string>(ws);
|
2020-02-19 18:32:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class ContiguousContainer, typename
|
|
|
|
std::enable_if<not std::is_pointer<ContiguousContainer>::value and
|
|
|
|
std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
|
|
|
|
int>::type = 0>
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter input_adapter(const ContiguousContainer& c)
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
|
|
|
return input_adapter(std::begin(c), std::end(c));
|
|
|
|
}
|
2018-04-01 20:12:36 +03:00
|
|
|
|
|
|
|
|
2020-02-19 18:32:49 +03:00
|
|
|
template<class T, std::size_t N>
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter input_adapter(T (&array)[N])
|
2020-02-19 18:32:49 +03:00
|
|
|
{
|
|
|
|
return input_adapter(std::begin(array), std::end(array));
|
|
|
|
}
|
2018-04-01 20:12:36 +03:00
|
|
|
|
2020-02-19 18:32:49 +03:00
|
|
|
// This class only handles inputs of input_buffer_adapter type.
|
|
|
|
// It's required so that expressions like {ptr, len} can be implicitely casted
|
|
|
|
// to the correct adapter.
|
|
|
|
class span_input_adapter
|
|
|
|
{
|
|
|
|
public:
|
2017-08-14 17:48:55 +03:00
|
|
|
template<typename CharT,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_pointer<CharT>::value and
|
|
|
|
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
int>::type = 0>
|
2020-02-19 18:32:49 +03:00
|
|
|
span_input_adapter(CharT b, std::size_t l)
|
2020-02-19 22:59:31 +03:00
|
|
|
: ia(reinterpret_cast<const char*>(b), l) {}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
template<typename CharT,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_pointer<CharT>::value and
|
|
|
|
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
int>::type = 0>
|
2020-02-19 18:32:49 +03:00
|
|
|
span_input_adapter(CharT b)
|
|
|
|
: span_input_adapter(reinterpret_cast<const char*>(b),
|
|
|
|
std::strlen(reinterpret_cast<const char*>(b))) {}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
template<class IteratorType,
|
|
|
|
typename std::enable_if<
|
2018-11-07 21:39:25 +03:00
|
|
|
std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
|
2017-08-14 17:48:55 +03:00
|
|
|
int>::type = 0>
|
2020-02-19 18:32:49 +03:00
|
|
|
span_input_adapter(IteratorType first, IteratorType last)
|
|
|
|
: ia(input_adapter(first, last)) {}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
template<class T, std::size_t N>
|
2020-02-19 18:32:49 +03:00
|
|
|
span_input_adapter(T (&array)[N])
|
|
|
|
: span_input_adapter(std::begin(array), std::end(array)) {}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
|
|
|
/// input adapter for contiguous container
|
|
|
|
template<class ContiguousContainer, typename
|
|
|
|
std::enable_if<not std::is_pointer<ContiguousContainer>::value and
|
2018-11-07 21:39:25 +03:00
|
|
|
std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
|
2017-08-14 17:48:55 +03:00
|
|
|
int>::type = 0>
|
2020-02-19 18:32:49 +03:00
|
|
|
span_input_adapter(const ContiguousContainer& c)
|
|
|
|
: span_input_adapter(std::begin(c), std::end(c)) {}
|
2017-08-14 17:48:55 +03:00
|
|
|
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter&& get()
|
2017-08-14 17:48:55 +03:00
|
|
|
{
|
2020-02-19 22:59:31 +03:00
|
|
|
return std::move(ia);
|
2017-08-14 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-02-19 22:59:31 +03:00
|
|
|
input_buffer_adapter ia;
|
2017-08-14 17:48:55 +03:00
|
|
|
};
|
2018-10-07 19:39:18 +03:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace nlohmann
|