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
0a4a6a83
Commit
0a4a6a83
authored
Dec 07, 2016
by
Alex Astashyn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored to avoid using exceptions, as there are plans to support exceptionless mode
parent
67744577
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
36 deletions
+116
-36
src/json.hpp
src/json.hpp
+58
-18
src/json.hpp.re2c
src/json.hpp.re2c
+58
-18
No files found.
src/json.hpp
View file @
0a4a6a83
...
@@ -9079,10 +9079,42 @@ basic_json_parser_66:
...
@@ -9079,10 +9079,42 @@ basic_json_parser_66:
struct
strtonum
struct
strtonum
{
{
public:
public:
strtonum
(
const
char
*
start
,
const
char
*
end
)
template
<
typename
T
>
struct
maybe
{
T
x
;
bool
valid
;
maybe
(
const
T
&
xx
)
:
x
(
xx
),
valid
(
true
)
{}
maybe
()
:
x
(),
valid
(
false
)
{}
operator
bool
()
const
{
return
valid
;
}
const
T
&
operator
*
()
const
{
return
x
;
}
};
strtonum
(
const
char
*
start
,
const
char
*
end
)
:
m_start
(
start
),
m_end
(
end
)
:
m_start
(
start
),
m_end
(
end
)
{}
{}
template
<
typename
T
,
typename
=
typename
std
::
enable_if
<
std
::
is_arithmetic
<
T
>
::
value
>::
type
>
bool
to
(
T
&
val
)
const
{
return
parse
(
val
,
std
::
is_integral
<
T
>
());
}
template
<
typename
T
,
template
<
typename
T
,
typename
=
typename
std
::
enable_if
<
typename
=
typename
std
::
enable_if
<
std
::
is_arithmetic
<
T
>
::
value
>::
type
>
std
::
is_arithmetic
<
T
>
::
value
>::
type
>
...
@@ -9361,31 +9393,39 @@ basic_json_parser_66:
...
@@ -9361,31 +9393,39 @@ basic_json_parser_66:
const
bool
is_negative
=
*
m_start
==
'-'
;
const
bool
is_negative
=
*
m_start
==
'-'
;
try
{
result
.
m_type
=
value_t
::
discarded
;
if
(
not
num
.
is_integral
())
{
if
(
not
num
.
is_integral
())
;
{
}
;
// will parse as float below
else
if
(
is_negative
)
}
{
else
if
(
is_negative
)
{
number_integer_t
val
{
0
};
if
(
num
.
to
(
val
))
{
result
.
m_type
=
value_t
::
number_integer
;
result
.
m_type
=
value_t
::
number_integer
;
result
.
m_value
=
static_cast
<
number_integer_t
>
(
num
);
result
.
m_value
=
val
;
return
;
}
}
}
else
else
{
{
number_unsigned_t
val
{
0
};
if
(
num
.
to
(
val
))
{
result
.
m_type
=
value_t
::
number_unsigned
;
result
.
m_type
=
value_t
::
number_unsigned
;
result
.
m_value
=
static_cast
<
number_unsigned_t
>
(
num
);
result
.
m_value
=
val
;
return
;
}
}
}
}
catch
(
std
::
invalid_argument
&
)
number_float_t
val
{
0
};
if
(
result
.
m_type
!=
value_t
::
discarded
or
!
num
.
to
(
val
))
{
{
;
// overflow - will parse as double
return
;
// already have a value from above
// or couldn't parse as float_t
}
}
result
.
m_type
=
value_t
::
number_float
;
result
.
m_type
=
value_t
::
number_float
;
result
.
m_value
=
static_cast
<
number_float_t
>
(
num
)
;
result
.
m_value
=
val
;
// replace infinity and NAN by null
// replace infinity and NAN by null
if
(
not
std
::
isfinite
(
result
.
m_value
.
number_float
))
if
(
not
std
::
isfinite
(
result
.
m_value
.
number_float
))
...
...
src/json.hpp.re2c
View file @
0a4a6a83
...
@@ -8228,10 +8228,42 @@ class basic_json
...
@@ -8228,10 +8228,42 @@ class basic_json
struct strtonum
struct strtonum
{
{
public:
public:
strtonum(const char* start, const char* end)
template<typename T>
struct maybe
{
T x;
bool valid;
maybe(const T& xx)
: x(xx), valid(true)
{}
maybe() : x(), valid(false)
{}
operator bool() const
{
return valid;
}
const T& operator*() const
{
return x;
}
};
strtonum(const char* start, const char* end)
: m_start(start), m_end(end)
: m_start(start), m_end(end)
{}
{}
template<typename T,
typename = typename std::enable_if<
std::is_arithmetic<T>::value>::type >
bool to(T& val) const
{
return parse(val, std::is_integral<T>());
}
template<typename T,
template<typename T,
typename = typename std::enable_if<
typename = typename std::enable_if<
std::is_arithmetic<T>::value>::type >
std::is_arithmetic<T>::value>::type >
...
@@ -8510,31 +8542,39 @@ class basic_json
...
@@ -8510,31 +8542,39 @@ class basic_json
const bool is_negative = *m_start == '-';
const bool is_negative = *m_start == '-';
try {
result.m_type = value_t::discarded;
if (not num.is_integral())
{
if (not num.is_integral())
;
{
}
; // will parse as float below
else if (is_negative)
}
{
else if (is_negative)
{
number_integer_t val{0};
if(num.to(val)) {
result.m_type = value_t::number_integer;
result.m_type = value_t::number_integer;
result.m_value = static_cast<number_integer_t>(num);
result.m_value = val;
return;
}
}
}
else
else
{
{
number_unsigned_t val{0};
if(num.to(val)) {
result.m_type = value_t::number_unsigned;
result.m_type = value_t::number_unsigned;
result.m_value = static_cast<number_unsigned_t>(num);
result.m_value = val;
return;
}
}
}
}
catch (std::invalid_argument&)
number_float_t val{0};
if (result.m_type != value_t::discarded
or !num.to(val))
{
{
; // overflow - will parse as double
return; // already have a value from above
// or couldn't parse as float_t
}
}
result.m_type = value_t::number_float;
result.m_type = value_t::number_float;
result.m_value =
static_cast<number_float_t>(num)
;
result.m_value =
val
;
// replace infinity and NAN by null
// replace infinity and NAN by null
if (not std::isfinite(result.m_value.number_float))
if (not std::isfinite(result.m_value.number_float))
...
...
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