Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
757d2c6c
Unverified
Commit
757d2c6c
authored
Mar 06, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
replaced at() calls in msgpack/cbor
parent
54073332
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
24 deletions
+40
-24
src/json.hpp
src/json.hpp
+20
-12
src/json.hpp.re2c
src/json.hpp.re2c
+20
-12
No files found.
src/json.hpp
View file @
757d2c6c
...
...
@@ -7957,12 +7957,12 @@ class basic_json
*/
static
basic_json
from_msgpack_internal
(
const
std
::
vector
<
uint8_t
>&
v
,
size_t
&
idx
)
{
// make sure reading 1 byte is safe
check_length
(
v
.
size
(),
1
,
idx
);
// store and increment index
const
size_t
current_idx
=
idx
++
;
// make sure reading 1 byte is safe
check_length
(
v
.
size
(),
1
,
current_idx
);
if
(
v
[
current_idx
]
<=
0xbf
)
{
if
(
v
[
current_idx
]
<=
0x7f
)
// positive fixint
...
...
@@ -8026,9 +8026,10 @@ class basic_json
{
// copy bytes in reverse order into the double variable
float
res
;
check_length
(
v
.
size
(),
sizeof
(
float
),
current_idx
+
1
);
for
(
size_t
byte
=
0
;
byte
<
sizeof
(
float
);
++
byte
)
{
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
float
)
-
byte
-
1
]
=
v
.
at
(
current_idx
+
1
+
byte
)
;
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
float
)
-
byte
-
1
]
=
v
[
current_idx
+
1
+
byte
]
;
}
idx
+=
sizeof
(
float
);
// skip content bytes
return
res
;
...
...
@@ -8038,9 +8039,10 @@ class basic_json
{
// copy bytes in reverse order into the double variable
double
res
;
check_length
(
v
.
size
(),
sizeof
(
double
),
current_idx
+
1
);
for
(
size_t
byte
=
0
;
byte
<
sizeof
(
double
);
++
byte
)
{
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
double
)
-
byte
-
1
]
=
v
.
at
(
current_idx
+
1
+
byte
)
;
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
double
)
-
byte
-
1
]
=
v
[
current_idx
+
1
+
byte
]
;
}
idx
+=
sizeof
(
double
);
// skip content bytes
return
res
;
...
...
@@ -8198,7 +8200,10 @@ class basic_json
// store and increment index
const
size_t
current_idx
=
idx
++
;
switch
(
v
.
at
(
current_idx
))
// make sure reading 1 byte is safe
check_length
(
v
.
size
(),
1
,
current_idx
);
switch
(
v
[
current_idx
])
{
// Integer 0x00..0x17 (0..23)
case
0x00
:
...
...
@@ -8379,7 +8384,7 @@ class basic_json
case
0x7f
:
// UTF-8 string (indefinite length)
{
std
::
string
result
;
while
(
v
.
at
(
idx
)
!=
0xff
)
while
(
check_length
(
v
.
size
(),
1
,
idx
),
v
[
idx
]
!=
0xff
)
{
string_t
s
=
from_cbor_internal
(
v
,
idx
);
result
+=
s
;
...
...
@@ -8475,7 +8480,7 @@ class basic_json
case
0x9f
:
// array (indefinite length)
{
basic_json
result
=
value_t
::
array
;
while
(
v
.
at
(
idx
)
!=
0xff
)
while
(
check_length
(
v
.
size
(),
1
,
idx
),
v
[
idx
]
!=
0xff
)
{
result
.
push_back
(
from_cbor_internal
(
v
,
idx
));
}
...
...
@@ -8575,7 +8580,7 @@ class basic_json
case
0xbf
:
// map (indefinite length)
{
basic_json
result
=
value_t
::
object
;
while
(
v
.
at
(
idx
)
!=
0xff
)
while
(
check_length
(
v
.
size
(),
1
,
idx
),
v
[
idx
]
!=
0xff
)
{
std
::
string
key
=
from_cbor_internal
(
v
,
idx
);
result
[
key
]
=
from_cbor_internal
(
v
,
idx
);
...
...
@@ -8611,7 +8616,8 @@ class basic_json
// 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 Fig. 3.
const
int
half
=
(
v
.
at
(
current_idx
+
1
)
<<
8
)
+
v
.
at
(
current_idx
+
2
);
check_length
(
v
.
size
(),
2
,
current_idx
+
1
);
const
int
half
=
(
v
[
current_idx
+
1
]
<<
8
)
+
v
[
current_idx
+
2
];
const
int
exp
=
(
half
>>
10
)
&
0x1f
;
const
int
mant
=
half
&
0x3ff
;
double
val
;
...
...
@@ -8636,9 +8642,10 @@ class basic_json
{
// copy bytes in reverse order into the float variable
float
res
;
check_length
(
v
.
size
(),
sizeof
(
float
),
current_idx
+
1
);
for
(
size_t
byte
=
0
;
byte
<
sizeof
(
float
);
++
byte
)
{
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
float
)
-
byte
-
1
]
=
v
.
at
(
current_idx
+
1
+
byte
)
;
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
float
)
-
byte
-
1
]
=
v
[
current_idx
+
1
+
byte
]
;
}
idx
+=
sizeof
(
float
);
// skip content bytes
return
res
;
...
...
@@ -8648,9 +8655,10 @@ class basic_json
{
// copy bytes in reverse order into the double variable
double
res
;
check_length
(
v
.
size
(),
sizeof
(
double
),
current_idx
+
1
);
for
(
size_t
byte
=
0
;
byte
<
sizeof
(
double
);
++
byte
)
{
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
double
)
-
byte
-
1
]
=
v
.
at
(
current_idx
+
1
+
byte
)
;
reinterpret_cast
<
uint8_t
*>
(
&
res
)[
sizeof
(
double
)
-
byte
-
1
]
=
v
[
current_idx
+
1
+
byte
]
;
}
idx
+=
sizeof
(
double
);
// skip content bytes
return
res
;
...
...
src/json.hpp.re2c
View file @
757d2c6c
...
...
@@ -7957,12 +7957,12 @@ class basic_json
*/
static basic_json from_msgpack_internal(const std::vector<uint8_t>& v, size_t& idx)
{
// make sure reading 1 byte is safe
check_length(v.size(), 1, idx);
// store and increment index
const size_t current_idx = idx++;
// make sure reading 1 byte is safe
check_length(v.size(), 1, current_idx);
if (v[current_idx] <= 0xbf)
{
if (v[current_idx] <= 0x7f) // positive fixint
...
...
@@ -8026,9 +8026,10 @@ class basic_json
{
// copy bytes in reverse order into the double variable
float res;
check_length(v.size(), sizeof(float), current_idx + 1);
for (size_t byte = 0; byte < sizeof(float); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(float) - byte - 1] = v
.at(current_idx + 1 + byte)
;
reinterpret_cast<uint8_t*>(&res)[sizeof(float) - byte - 1] = v
[current_idx + 1 + byte]
;
}
idx += sizeof(float); // skip content bytes
return res;
...
...
@@ -8038,9 +8039,10 @@ class basic_json
{
// copy bytes in reverse order into the double variable
double res;
check_length(v.size(), sizeof(double), current_idx + 1);
for (size_t byte = 0; byte < sizeof(double); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(double) - byte - 1] = v
.at(current_idx + 1 + byte)
;
reinterpret_cast<uint8_t*>(&res)[sizeof(double) - byte - 1] = v
[current_idx + 1 + byte]
;
}
idx += sizeof(double); // skip content bytes
return res;
...
...
@@ -8198,7 +8200,10 @@ class basic_json
// store and increment index
const size_t current_idx = idx++;
switch (v.at(current_idx))
// make sure reading 1 byte is safe
check_length(v.size(), 1, current_idx);
switch (v[current_idx])
{
// Integer 0x00..0x17 (0..23)
case 0x00:
...
...
@@ -8379,7 +8384,7 @@ class basic_json
case 0x7f: // UTF-8 string (indefinite length)
{
std::string result;
while (
v.at(idx)
!= 0xff)
while (
check_length(v.size(), 1, idx), v[idx]
!= 0xff)
{
string_t s = from_cbor_internal(v, idx);
result += s;
...
...
@@ -8475,7 +8480,7 @@ class basic_json
case 0x9f: // array (indefinite length)
{
basic_json result = value_t::array;
while (
v.at(idx)
!= 0xff)
while (
check_length(v.size(), 1, idx), v[idx]
!= 0xff)
{
result.push_back(from_cbor_internal(v, idx));
}
...
...
@@ -8575,7 +8580,7 @@ class basic_json
case 0xbf: // map (indefinite length)
{
basic_json result = value_t::object;
while (
v.at(idx)
!= 0xff)
while (
check_length(v.size(), 1, idx), v[idx]
!= 0xff)
{
std::string key = from_cbor_internal(v, idx);
result[key] = from_cbor_internal(v, idx);
...
...
@@ -8611,7 +8616,8 @@ class basic_json
// 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 Fig. 3.
const int half = (v.at(current_idx + 1) << 8) + v.at(current_idx + 2);
check_length(v.size(), 2, current_idx + 1);
const int half = (v[current_idx + 1] << 8) + v[current_idx + 2];
const int exp = (half >> 10) & 0x1f;
const int mant = half & 0x3ff;
double val;
...
...
@@ -8636,9 +8642,10 @@ class basic_json
{
// copy bytes in reverse order into the float variable
float res;
check_length(v.size(), sizeof(float), current_idx + 1);
for (size_t byte = 0; byte < sizeof(float); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(float) - byte - 1] = v
.at(current_idx + 1 + byte)
;
reinterpret_cast<uint8_t*>(&res)[sizeof(float) - byte - 1] = v
[current_idx + 1 + byte]
;
}
idx += sizeof(float); // skip content bytes
return res;
...
...
@@ -8648,9 +8655,10 @@ class basic_json
{
// copy bytes in reverse order into the double variable
double res;
check_length(v.size(), sizeof(double), current_idx + 1);
for (size_t byte = 0; byte < sizeof(double); ++byte)
{
reinterpret_cast<uint8_t*>(&res)[sizeof(double) - byte - 1] = v
.at(current_idx + 1 + byte)
;
reinterpret_cast<uint8_t*>(&res)[sizeof(double) - byte - 1] = v
[current_idx + 1 + byte]
;
}
idx += sizeof(double); // skip content bytes
return res;
...
...
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