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
fc7f4b8f
Commit
fc7f4b8f
authored
Jun 28, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace some raw loops with algorithms
parent
7ffa07e3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
70 deletions
+54
-70
src/json.hpp
src/json.hpp
+27
-35
src/json.hpp.re2c
src/json.hpp.re2c
+27
-35
No files found.
src/json.hpp
View file @
fc7f4b8f
...
...
@@ -46,6 +46,7 @@ SOFTWARE.
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <string>
...
...
@@ -1554,22 +1555,13 @@ class basic_json
bool
type_deduction
=
true
,
value_t
manual_type
=
value_t
::
array
)
{
// the initializer list could describe an object
bool
is_an_object
=
true
;
// check if each element is an array with two elements whose first
// element is a string
for
(
const
auto
&
element
:
init
)
bool
is_an_object
=
std
::
all_of
(
init
.
begin
(),
init
.
end
(),
[](
const
basic_json
&
element
)
{
if
(
not
element
.
is_array
()
or
element
.
size
()
!=
2
or
not
element
[
0
].
is_string
())
{
// we found an element that makes it impossible to use the
// initializer list as object
is_an_object
=
false
;
break
;
}
}
return
element
.
is_array
()
and
element
.
size
()
==
2
and
element
[
0
].
is_string
();
});
// adjust type if type deduction is not wanted
if
(
not
type_deduction
)
...
...
@@ -1595,10 +1587,10 @@ class basic_json
assert
(
m_value
.
object
!=
nullptr
);
for
(
auto
&
element
:
ini
t
)
std
::
for_each
(
init
.
begin
(),
init
.
end
(),
[
this
](
const
basic_json
&
elemen
t
)
{
m_value
.
object
->
emplace
(
*
(
element
[
0
].
m_value
.
string
),
element
[
1
]);
}
}
);
}
else
{
...
...
@@ -3235,11 +3227,13 @@ class basic_json
// operator[] only works for arrays
if
(
is_array
())
{
// fill up array with null values
until given idx is reached
// fill up array with null values
if given idx is outside range
assert
(
m_value
.
array
!=
nullptr
);
for
(
size_t
i
=
m_value
.
array
->
size
();
i
<=
idx
;
++
i
)
if
(
idx
>=
m_value
.
array
->
size
()
)
{
m_value
.
array
->
push_back
(
basic_json
());
m_value
.
array
->
insert
(
m_value
.
array
->
end
(),
idx
-
m_value
.
array
->
size
()
+
1
,
basic_json
());
}
return
m_value
.
array
->
operator
[](
idx
);
...
...
@@ -5832,9 +5826,8 @@ class basic_json
*/
static
std
::
size_t
extra_space
(
const
string_t
&
s
)
noexcept
{
std
::
size_t
result
=
0
;
for
(
const
auto
&
c
:
s
)
return
std
::
accumulate
(
s
.
begin
(),
s
.
end
(),
size_t
{},
[](
size_t
res
,
typename
string_t
::
value_type
c
)
{
switch
(
c
)
{
...
...
@@ -5847,8 +5840,7 @@ class basic_json
case
'\t'
:
{
// from c (1 byte) to \x (2 bytes)
result
+=
1
;
break
;
return
res
+
1
;
}
default:
...
...
@@ -5856,14 +5848,15 @@ class basic_json
if
(
c
>=
0x00
and
c
<=
0x1f
)
{
// from c (1 byte) to \uxxxx (6 bytes)
result
+=
5
;
return
res
+
5
;
}
else
{
return
res
;
}
break
;
}
}
}
return
result
;
});
}
/*!
...
...
@@ -8778,14 +8771,13 @@ basic_json_parser_63:
*/
std
::
string
to_string
()
const
noexcept
{
std
::
string
result
;
for
(
const
auto
&
reference_token
:
reference_tokens
)
return
std
::
accumulate
(
reference_tokens
.
begin
(),
reference_tokens
.
end
(),
std
::
string
{},
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
result
+=
"/"
+
escape
(
reference_token
);
}
return
result
;
return
a
+
"/"
+
escape
(
b
);
});
}
/// @copydoc to_string()
...
...
src/json.hpp.re2c
View file @
fc7f4b8f
...
...
@@ -46,6 +46,7 @@ SOFTWARE.
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <string>
...
...
@@ -1554,22 +1555,13 @@ class basic_json
bool type_deduction = true,
value_t manual_type = value_t::array)
{
// the initializer list could describe an object
bool is_an_object = true;
// check if each element is an array with two elements whose first
// element is a string
for (const auto& element : init)
bool is_an_object = std::all_of(init.begin(), init.end(),
[](const basic_json & element)
{
if (not element.is_array() or element.size() != 2
or not element[0].is_string())
{
// we found an element that makes it impossible to use the
// initializer list as object
is_an_object = false;
break;
}
}
return element.is_array() and element.size() == 2 and element[0].is_string();
});
// adjust type if type deduction is not wanted
if (not type_deduction)
...
...
@@ -1595,10 +1587,10 @@ class basic_json
assert(m_value.object != nullptr);
for (auto& element : ini
t)
std::for_each(init.begin(), init.end(), [this](const basic_json & elemen
t)
{
m_value.object->emplace(*(element[0].m_value.string), element[1]);
}
}
);
}
else
{
...
...
@@ -3235,11 +3227,13 @@ class basic_json
// operator[] only works for arrays
if (is_array())
{
// fill up array with null values
until given idx is reached
// fill up array with null values
if given idx is outside range
assert(m_value.array != nullptr);
for (size_t i = m_value.array->size(); i <= idx; ++i
)
if (idx >= m_value.array->size()
)
{
m_value.array->push_back(basic_json());
m_value.array->insert(m_value.array->end(),
idx - m_value.array->size() + 1,
basic_json());
}
return m_value.array->operator[](idx);
...
...
@@ -5832,9 +5826,8 @@ class basic_json
*/
static std::size_t extra_space(const string_t& s) noexcept
{
std::size_t result = 0;
for (const auto& c : s)
return std::accumulate(s.begin(), s.end(), size_t{},
[](size_t res, typename string_t::value_type c)
{
switch (c)
{
...
...
@@ -5847,8 +5840,7 @@ class basic_json
case '\t':
{
// from c (1 byte) to \x (2 bytes)
result += 1;
break;
return res + 1;
}
default:
...
...
@@ -5856,14 +5848,15 @@ class basic_json
if (c >= 0x00 and c <= 0x1f)
{
// from c (1 byte) to \uxxxx (6 bytes)
result += 5;
return res + 5;
}
else
{
return res;
}
break;
}
}
}
return result;
});
}
/*!
...
...
@@ -8088,14 +8081,13 @@ class basic_json
*/
std::string to_string() const noexcept
{
std::string result;
for (const auto& reference_token : reference_tokens)
return std::accumulate(reference_tokens.begin(),
reference_tokens.end(), std::string{},
[](const std::string & a,
const std::string & b)
{
result += "/" + escape(reference_token);
}
return result;
return a + "/" + escape(b);
});
}
/// @copydoc to_string()
...
...
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