Commit 6b84c415 by Niels Lohmann

🔨 refactored get_from_vector function

parent 81a42724
...@@ -6262,36 +6262,35 @@ class basic_json ...@@ -6262,36 +6262,35 @@ class basic_json
} }
} }
template<typename T, typename std::enable_if<sizeof(T) == 1, int>::type = 0> /*
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx) Precondition:
{
return static_cast<T>(vec[current_idx + 1]); vec: | | | a | b | c | d | | | T: | | | | |
} ^ ^ ^ ^
template<typename T, typename std::enable_if<sizeof(T) == 2, int>::type = 0> current_index idx ptr sizeof(T)
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx)
{
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 010) + Postcondition:
static_cast<T>(vec[current_idx + 2]));
} vec: | | | a | b | c | d | | | T: | d | c | b | a |
template<typename T, typename std::enable_if<sizeof(T) == 4, int>::type = 0> ^ ^ ^
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx) | idx ptr
{ current_index
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 030) +
(static_cast<T>(vec[current_idx + 2]) << 020) +
(static_cast<T>(vec[current_idx + 3]) << 010) + Code from <http://stackoverflow.com/a/41031865/266378>
static_cast<T>(vec[current_idx + 4])); */
} template<typename T>
template<typename T, typename std::enable_if<sizeof(T) == 8, int>::type = 0> static T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_index)
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx)
{ {
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 070) + T result;
(static_cast<T>(vec[current_idx + 2]) << 060) + uint8_t* ptr = reinterpret_cast<uint8_t*>(&result);
(static_cast<T>(vec[current_idx + 3]) << 050) + size_t idx = current_index + 1 + sizeof(T);
(static_cast<T>(vec[current_idx + 4]) << 040) + while (idx > current_index)
(static_cast<T>(vec[current_idx + 5]) << 030) + {
(static_cast<T>(vec[current_idx + 6]) << 020) + *ptr++ = vec[--idx];
(static_cast<T>(vec[current_idx + 7]) << 010) + }
static_cast<T>(vec[current_idx + 8])); return result;
} }
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v) static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v)
...@@ -7173,7 +7172,13 @@ class basic_json ...@@ -7173,7 +7172,13 @@ class basic_json
{ {
idx += 2; // skip two content bytes idx += 2; // skip two content bytes
// code from RFC 7049, Appendix D // code from RFC 7049, Appendix D, Figure 3:
// As half-precision floating-point numbers were only added to IEEE
// 754 in 2008, today's programming platforms often still only have
// limited support for them. It is very easy to include at least
// decoding support for them even without such support. An example
// of a small decoder for half-precision floating-point numbers in
// the C language is shown in Figure 3.
const int half = (v[current_idx + 1] << 8) + v[current_idx + 2]; const int half = (v[current_idx + 1] << 8) + v[current_idx + 2];
const int exp = (half >> 10) & 0x1f; const int exp = (half >> 10) & 0x1f;
const int mant = half & 0x3ff; const int mant = half & 0x3ff;
......
...@@ -6262,36 +6262,35 @@ class basic_json ...@@ -6262,36 +6262,35 @@ class basic_json
} }
} }
template<typename T, typename std::enable_if<sizeof(T) == 1, int>::type = 0> /*
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx) Precondition:
{
return static_cast<T>(vec[current_idx + 1]); vec: | | | a | b | c | d | | | T: | | | | |
} ^ ^ ^ ^
template<typename T, typename std::enable_if<sizeof(T) == 2, int>::type = 0> current_index idx ptr sizeof(T)
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx)
{
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 010) + Postcondition:
static_cast<T>(vec[current_idx + 2]));
} vec: | | | a | b | c | d | | | T: | d | c | b | a |
template<typename T, typename std::enable_if<sizeof(T) == 4, int>::type = 0> ^ ^ ^
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx) | idx ptr
{ current_index
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 030) +
(static_cast<T>(vec[current_idx + 2]) << 020) +
(static_cast<T>(vec[current_idx + 3]) << 010) + Code from <http://stackoverflow.com/a/41031865/266378>
static_cast<T>(vec[current_idx + 4])); */
} template<typename T>
template<typename T, typename std::enable_if<sizeof(T) == 8, int>::type = 0> static T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_index)
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx)
{ {
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 070) + T result;
(static_cast<T>(vec[current_idx + 2]) << 060) + uint8_t* ptr = reinterpret_cast<uint8_t*>(&result);
(static_cast<T>(vec[current_idx + 3]) << 050) + size_t idx = current_index + 1 + sizeof(T);
(static_cast<T>(vec[current_idx + 4]) << 040) + while (idx > current_index)
(static_cast<T>(vec[current_idx + 5]) << 030) + {
(static_cast<T>(vec[current_idx + 6]) << 020) + *ptr++ = vec[--idx];
(static_cast<T>(vec[current_idx + 7]) << 010) + }
static_cast<T>(vec[current_idx + 8])); return result;
} }
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v) static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v)
......
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