Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
6b84c415
Commit
6b84c415
authored
Dec 08, 2016
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
refactored get_from_vector function
Snippet from
http://stackoverflow.com/a/41031865/266378
parent
81a42724
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
55 deletions
+59
-55
json.hpp
src/json.hpp
+33
-28
json.hpp.re2c
src/json.hpp.re2c
+26
-27
No files found.
src/json.hpp
View file @
6b84c415
...
...
@@ -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
)
{
return
static_cast
<
T
>
(
vec
[
current_idx
+
1
]);
}
template
<
typename
T
,
typename
std
::
enable_if
<
sizeof
(
T
)
==
2
,
int
>::
type
=
0
>
static
constexpr
T
get_from_vector
(
const
std
::
vector
<
uint8_t
>&
vec
,
const
size_t
current_idx
)
/*
Precondition:
vec: | | | a | b | c | d | | | T: | | | | |
^ ^ ^ ^
current_index idx ptr sizeof(T)
Postcondition:
vec: | | | a | b | c | d | | | T: | d | c | b | a |
^ ^ ^
| idx ptr
current_index
Code from <http://stackoverflow.com/a/41031865/266378>
*/
template
<
typename
T
>
static
T
get_from_vector
(
const
std
::
vector
<
uint8_t
>&
vec
,
const
size_t
current_index
)
{
return
static_cast
<
T
>
((
static_cast
<
T
>
(
vec
[
current_idx
+
1
])
<<
010
)
+
static_cast
<
T
>
(
vec
[
current_idx
+
2
]));
}
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
)
T
result
;
uint8_t
*
ptr
=
reinterpret_cast
<
uint8_t
*>
(
&
result
);
size_t
idx
=
current_index
+
1
+
sizeof
(
T
);
while
(
idx
>
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
)
+
static_cast
<
T
>
(
vec
[
current_idx
+
4
]));
*
ptr
++
=
vec
[
--
idx
];
}
template
<
typename
T
,
typename
std
::
enable_if
<
sizeof
(
T
)
==
8
,
int
>::
type
=
0
>
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
)
+
(
static_cast
<
T
>
(
vec
[
current_idx
+
2
])
<<
060
)
+
(
static_cast
<
T
>
(
vec
[
current_idx
+
3
])
<<
050
)
+
(
static_cast
<
T
>
(
vec
[
current_idx
+
4
])
<<
040
)
+
(
static_cast
<
T
>
(
vec
[
current_idx
+
5
])
<<
030
)
+
(
static_cast
<
T
>
(
vec
[
current_idx
+
6
])
<<
020
)
+
(
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
)
...
...
@@ -7173,7 +7172,13 @@ class basic_json
{
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
exp
=
(
half
>>
10
)
&
0x1f
;
const
int
mant
=
half
&
0x3ff
;
...
...
src/json.hpp.re2c
View file @
6b84c415
...
...
@@ -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)
{
return static_cast<T>(vec[current_idx + 1]);
}
template<typename T, typename std::enable_if<sizeof(T) == 2, int>::type = 0>
static constexpr T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_idx)
/*
Precondition:
vec: | | | a | b | c | d | | | T: | | | | |
^ ^ ^ ^
current_index idx ptr sizeof(T)
Postcondition:
vec: | | | a | b | c | d | | | T: | d | c | b | a |
^ ^ ^
| idx ptr
current_index
Code from <http://stackoverflow.com/a/41031865/266378>
*/
template<typename T>
static T get_from_vector(const std::vector<uint8_t>& vec, const size_t current_index)
{
return static_cast<T>((static_cast<T>(vec[current_idx + 1]) << 010) +
static_cast<T>(vec[current_idx + 2]));
}
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)
T result;
uint8_t* ptr = reinterpret_cast<uint8_t*>(&result);
size_t idx = current_index + 1 + sizeof(T);
while (idx > 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) +
static_cast<T>(vec[current_idx + 4]));
*ptr++ = vec[--idx];
}
template<typename T, typename std::enable_if<sizeof(T) == 8, int>::type = 0>
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) +
(static_cast<T>(vec[current_idx + 2]) << 060) +
(static_cast<T>(vec[current_idx + 3]) << 050) +
(static_cast<T>(vec[current_idx + 4]) << 040) +
(static_cast<T>(vec[current_idx + 5]) << 030) +
(static_cast<T>(vec[current_idx + 6]) << 020) +
(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)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment