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
7a32bd22
Commit
7a32bd22
authored
Apr 26, 2015
by
Niels
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #63 from Florianjw/master
Improve the implementations of the comparission-operators
parents
c7f95cc6
4fbff7d1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
366 deletions
+108
-366
src/json.hpp
src/json.hpp
+54
-183
src/json.hpp.re2c
src/json.hpp.re2c
+54
-183
No files found.
src/json.hpp
View file @
7a32bd22
...
...
@@ -11,6 +11,7 @@
#define NLOHMANN_JSON_HPP
#include <algorithm>
#include <array>
#include <ciso646>
#include <cmath>
#include <cstdio>
...
...
@@ -202,76 +203,17 @@ class basic_json
*/
friend
bool
operator
<
(
const
value_t
lhs
,
const
value_t
rhs
)
{
// no type is smaller than itself
if
(
lhs
==
rhs
)
{
return
false
;
}
switch
(
lhs
)
{
case
(
value_t
:
:
null
)
:
{
// nulls are smaller than all other types
return
true
;
}
case
(
value_t
:
:
boolean
)
:
{
// only nulls are smaller than booleans
return
(
rhs
!=
value_t
::
null
);
}
case
(
value_t
:
:
number_float
)
:
case
(
value_t
:
:
number_integer
)
:
{
switch
(
rhs
)
{
// numbers are smaller than objects, arrays, and string
case
(
value_t
:
:
object
)
:
case
(
value_t
:
:
array
)
:
case
(
value_t
:
:
string
)
:
{
return
true
;
}
default:
{
return
false
;
}
}
}
case
(
value_t
:
:
object
)
:
{
switch
(
rhs
)
{
// objects are smaller than arrays and string
case
(
value_t
:
:
array
)
:
case
(
value_t
:
:
string
)
:
{
return
true
;
}
default:
{
return
false
;
}
}
std
::
array
<
uint8_t
,
7
>
order
=
{{
0
,
// null
3
,
// object
4
,
// array
5
,
// string
1
,
// boolean
2
,
// integer
2
// float
}
case
(
value_t
:
:
array
)
:
{
// arrays are smaller than strings
return
(
rhs
==
value_t
::
string
);
}
default:
{
// a string is not smaller than any other types
return
false
;
}
}
};
return
order
[
static_cast
<
std
::
size_t
>
(
lhs
)]
<
order
[
static_cast
<
std
::
size_t
>
(
rhs
)];
}
...
...
@@ -1868,74 +1810,38 @@ class basic_json
*/
friend
bool
operator
==
(
const_reference
lhs
,
const_reference
rhs
)
noexcept
{
switch
(
lhs
.
type
())
const
auto
lhs_type
=
lhs
.
type
();
const
auto
rhs_type
=
rhs
.
type
();
if
(
lhs_type
==
rhs_type
)
{
case
(
value_t
:
:
array
)
:
switch
(
lhs_type
)
{
if
(
rhs
.
type
()
==
value_t
::
array
)
{
case
(
value_t
:
:
array
)
:
return
*
lhs
.
m_value
.
array
==
*
rhs
.
m_value
.
array
;
}
break
;
}
case
(
value_t
:
:
object
)
:
{
if
(
rhs
.
type
()
==
value_t
::
object
)
{
case
(
value_t
:
:
object
)
:
return
*
lhs
.
m_value
.
object
==
*
rhs
.
m_value
.
object
;
}
break
;
}
case
(
value_t
:
:
null
)
:
{
if
(
rhs
.
type
()
==
value_t
::
null
)
{
case
(
value_t
:
:
null
)
:
return
true
;
}
break
;
}
case
(
value_t
:
:
string
)
:
{
if
(
rhs
.
type
()
==
value_t
::
string
)
{
case
(
value_t
:
:
string
)
:
return
*
lhs
.
m_value
.
string
==
*
rhs
.
m_value
.
string
;
}
break
;
}
case
(
value_t
:
:
boolean
)
:
{
if
(
rhs
.
type
()
==
value_t
::
boolean
)
{
case
(
value_t
:
:
boolean
)
:
return
lhs
.
m_value
.
boolean
==
rhs
.
m_value
.
boolean
;
}
break
;
}
case
(
value_t
:
:
number_integer
)
:
{
if
(
rhs
.
type
()
==
value_t
::
number_integer
)
{
case
(
value_t
:
:
number_integer
)
:
return
lhs
.
m_value
.
number_integer
==
rhs
.
m_value
.
number_integer
;
}
if
(
rhs
.
type
()
==
value_t
::
number_float
)
{
return
lhs
.
m_value
.
number_integer
==
static_cast
<
number_integer_t
>
(
rhs
.
m_value
.
number_float
);
}
break
;
}
case
(
value_t
:
:
number_float
)
:
{
if
(
rhs
.
type
()
==
value_t
::
number_integer
)
{
return
approx
(
lhs
.
m_value
.
number_float
,
static_cast
<
number_float_t
>
(
rhs
.
m_value
.
number_integer
));
}
if
(
rhs
.
type
()
==
value_t
::
number_float
)
{
case
(
value_t
:
:
number_float
)
:
return
approx
(
lhs
.
m_value
.
number_float
,
rhs
.
m_value
.
number_float
);
}
break
;
}
}
else
if
(
lhs_type
==
value_t
::
number_integer
and
rhs_type
==
value_t
::
number_float
)
{
return
approx
(
static_cast
<
number_float_t
>
(
lhs
.
m_value
.
number_integer
),
rhs
.
m_value
.
number_float
);
}
else
if
(
lhs_type
==
value_t
::
number_float
and
rhs_type
==
value_t
::
number_integer
)
{
return
approx
(
lhs
.
m_value
.
number_float
,
static_cast
<
number_float_t
>
(
rhs
.
m_value
.
number_integer
));
}
return
false
;
}
...
...
@@ -1951,77 +1857,42 @@ class basic_json
/// comparison: less than
friend
bool
operator
<
(
const_reference
lhs
,
const_reference
rhs
)
noexcept
{
switch
(
lhs
.
type
())
const
auto
lhs_type
=
lhs
.
type
();
const
auto
rhs_type
=
rhs
.
type
();
if
(
lhs_type
==
rhs_type
)
{
case
(
value_t
:
:
array
)
:
switch
(
lhs_type
)
{
if
(
rhs
.
type
()
==
value_t
::
array
)
{
case
(
value_t
:
:
array
)
:
return
*
lhs
.
m_value
.
array
<
*
rhs
.
m_value
.
array
;
}
break
;
}
case
(
value_t
:
:
object
)
:
{
if
(
rhs
.
type
()
==
value_t
::
object
)
{
case
(
value_t
:
:
object
)
:
return
*
lhs
.
m_value
.
object
<
*
rhs
.
m_value
.
object
;
}
break
;
}
case
(
value_t
:
:
null
)
:
{
if
(
rhs
.
type
()
==
value_t
::
null
)
{
case
(
value_t
:
:
null
)
:
return
false
;
}
break
;
}
case
(
value_t
:
:
string
)
:
{
if
(
rhs
.
type
()
==
value_t
::
string
)
{
case
(
value_t
:
:
string
)
:
return
*
lhs
.
m_value
.
string
<
*
rhs
.
m_value
.
string
;
}
break
;
}
case
(
value_t
:
:
boolean
)
:
{
if
(
rhs
.
type
()
==
value_t
::
boolean
)
{
case
(
value_t
:
:
boolean
)
:
return
lhs
.
m_value
.
boolean
<
rhs
.
m_value
.
boolean
;
}
break
;
}
case
(
value_t
:
:
number_integer
)
:
{
if
(
rhs
.
type
()
==
value_t
::
number_integer
)
{
case
(
value_t
:
:
number_integer
)
:
return
lhs
.
m_value
.
number_integer
<
rhs
.
m_value
.
number_integer
;
}
if
(
rhs
.
type
()
==
value_t
::
number_float
)
{
return
lhs
.
m_value
.
number_integer
<
static_cast
<
number_integer_t
>
(
rhs
.
m_value
.
number_float
);
}
break
;
}
case
(
value_t
:
:
number_float
)
:
{
if
(
rhs
.
type
()
==
value_t
::
number_integer
)
{
return
lhs
.
m_value
.
number_float
<
static_cast
<
number_float_t
>
(
rhs
.
m_value
.
number_integer
);
}
if
(
rhs
.
type
()
==
value_t
::
number_float
)
{
case
(
value_t
:
:
number_float
)
:
return
lhs
.
m_value
.
number_float
<
rhs
.
m_value
.
number_float
;
}
break
;
}
}
else
if
(
lhs_type
==
value_t
::
number_integer
and
rhs_type
==
value_t
::
number_float
)
{
return
static_cast
<
number_float_t
>
(
lhs
.
m_value
.
number_integer
)
<
rhs
.
m_value
.
number_float
;
}
else
if
(
lhs_type
==
value_t
::
number_float
and
rhs_type
==
value_t
::
number_integer
)
{
return
lhs
.
m_value
.
number_float
<
static_cast
<
number_float_t
>
(
rhs
.
m_value
.
number_integer
);
}
// We only reach this line if we cannot compare values. In that case,
// we compare types.
return
lhs
.
type
()
<
rhs
.
type
()
;
return
lhs
_type
<
rhs_type
;
}
/// comparison: less than or equal
...
...
src/json.hpp.re2c
View file @
7a32bd22
...
...
@@ -11,6 +11,7 @@
#define NLOHMANN_JSON_HPP
#include <algorithm>
#include <array>
#include <ciso646>
#include <cmath>
#include <cstdio>
...
...
@@ -202,76 +203,17 @@ class basic_json
*/
friend bool operator<(const value_t lhs, const value_t rhs)
{
// no type is smaller than itself
if (lhs == rhs)
{
return false;
}
switch (lhs)
{
case (value_t::null):
{
// nulls are smaller than all other types
return true;
}
case (value_t::boolean):
{
// only nulls are smaller than booleans
return (rhs != value_t::null);
}
case (value_t::number_float):
case (value_t::number_integer):
{
switch (rhs)
{
// numbers are smaller than objects, arrays, and string
case (value_t::object):
case (value_t::array):
case (value_t::string):
{
return true;
}
default:
{
return false;
}
}
}
case (value_t::object):
{
switch (rhs)
{
// objects are smaller than arrays and string
case (value_t::array):
case (value_t::string):
{
return true;
}
default:
{
return false;
}
}
std::array<uint8_t, 7> order = {{
0, // null
3, // object
4, // array
5, // string
1, // boolean
2, // integer
2 // float
}
case (value_t::array):
{
// arrays are smaller than strings
return (rhs == value_t::string);
}
default:
{
// a string is not smaller than any other types
return false;
}
}
};
return order[static_cast<std::size_t>(lhs)] < order[static_cast<std::size_t>(rhs)];
}
...
...
@@ -1868,74 +1810,38 @@ class basic_json
*/
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{
switch (lhs.type())
const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type();
if (lhs_type == rhs_type)
{
case (value_t::array):
switch (lhs_type)
{
if (rhs.type() == value_t::array)
{
case (value_t::array):
return *lhs.m_value.array == *rhs.m_value.array;
}
break;
}
case (value_t::object):
{
if (rhs.type() == value_t::object)
{
case (value_t::object):
return *lhs.m_value.object == *rhs.m_value.object;
}
break;
}
case (value_t::null):
{
if (rhs.type() == value_t::null)
{
case (value_t::null):
return true;
}
break;
}
case (value_t::string):
{
if (rhs.type() == value_t::string)
{
case (value_t::string):
return *lhs.m_value.string == *rhs.m_value.string;
}
break;
}
case (value_t::boolean):
{
if (rhs.type() == value_t::boolean)
{
case (value_t::boolean):
return lhs.m_value.boolean == rhs.m_value.boolean;
}
break;
}
case (value_t::number_integer):
{
if (rhs.type() == value_t::number_integer)
{
case (value_t::number_integer):
return lhs.m_value.number_integer == rhs.m_value.number_integer;
}
if (rhs.type() == value_t::number_float)
{
return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_float);
}
break;
}
case (value_t::number_float):
{
if (rhs.type() == value_t::number_integer)
{
return approx(lhs.m_value.number_float, static_cast<number_float_t>(rhs.m_value.number_integer));
}
if (rhs.type() == value_t::number_float)
{
case (value_t::number_float):
return approx(lhs.m_value.number_float, rhs.m_value.number_float);
}
break;
}
}
else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
{
return approx(static_cast<number_float_t>(lhs.m_value.number_integer),
rhs.m_value.number_float);
}
else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
{
return approx(lhs.m_value.number_float,
static_cast<number_float_t>(rhs.m_value.number_integer));
}
return false;
}
...
...
@@ -1951,77 +1857,42 @@ class basic_json
/// comparison: less than
friend bool operator<(const_reference lhs, const_reference rhs) noexcept
{
switch (lhs.type())
const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type();
if (lhs_type == rhs_type)
{
case (value_t::array):
switch (lhs_type)
{
if (rhs.type() == value_t::array)
{
case (value_t::array):
return *lhs.m_value.array < *rhs.m_value.array;
}
break;
}
case (value_t::object):
{
if (rhs.type() == value_t::object)
{
case (value_t::object):
return *lhs.m_value.object < *rhs.m_value.object;
}
break;
}
case (value_t::null):
{
if (rhs.type() == value_t::null)
{
case (value_t::null):
return false;
}
break;
}
case (value_t::string):
{
if (rhs.type() == value_t::string)
{
case (value_t::string):
return *lhs.m_value.string < *rhs.m_value.string;
}
break;
}
case (value_t::boolean):
{
if (rhs.type() == value_t::boolean)
{
case (value_t::boolean):
return lhs.m_value.boolean < rhs.m_value.boolean;
}
break;
}
case (value_t::number_integer):
{
if (rhs.type() == value_t::number_integer)
{
case (value_t::number_integer):
return lhs.m_value.number_integer < rhs.m_value.number_integer;
}
if (rhs.type() == value_t::number_float)
{
return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_float);
}
break;
}
case (value_t::number_float):
{
if (rhs.type() == value_t::number_integer)
{
return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
}
if (rhs.type() == value_t::number_float)
{
case (value_t::number_float):
return lhs.m_value.number_float < rhs.m_value.number_float;
}
break;
}
}
else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
{
return static_cast<number_float_t>(lhs.m_value.number_integer) <
rhs.m_value.number_float;
}
else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
{
return lhs.m_value.number_float <
static_cast<number_float_t>(rhs.m_value.number_integer);
}
// We only reach this line if we cannot compare values. In that case,
// we compare types.
return lhs
.type() < rhs.type()
;
return lhs
_type < rhs_type
;
}
/// comparison: less than or equal
...
...
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