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
83a9c60d
Unverified
Commit
83a9c60d
authored
Feb 20, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
refactored code to avoid using strcpy/strlen/strcat (#463)
parent
716485a9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
22 deletions
+74
-22
src/json.hpp
src/json.hpp
+37
-11
src/json.hpp.re2c
src/json.hpp.re2c
+37
-11
No files found.
src/json.hpp
View file @
83a9c60d
...
...
@@ -38,7 +38,7 @@ SOFTWARE.
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // int64_t, uint64_t
#include <cstdlib> // abort, strtod, strtof, strtold, strtoul, strtoll, strtoull
#include <cstring> // str
cpy, str
len
#include <cstring> // strlen
#include <forward_list> // forward_list
#include <functional> // function, hash, less
#include <initializer_list> // initializer_list
...
...
@@ -8259,6 +8259,7 @@ class basic_json
template
<
typename
NumberType
>
void
x_write
(
NumberType
x
,
/*is_integral=*/
std
::
true_type
)
{
// special case for "0"
if
(
x
==
0
)
{
m_buf
[
0
]
=
'0'
;
...
...
@@ -8291,9 +8292,17 @@ class basic_json
template
<
typename
NumberType
>
void
x_write
(
NumberType
x
,
/*is_integral=*/
std
::
false_type
)
{
// special case for 0.0 and -0.0
if
(
x
==
0
)
{
std
::
strcpy
(
m_buf
.
data
(),
std
::
signbit
(
x
)
?
"-0.0"
:
"0.0"
);
size_t
i
=
0
;
if
(
std
::
signbit
(
x
))
{
m_buf
[
i
++
]
=
'-'
;
}
m_buf
[
i
++
]
=
'0'
;
m_buf
[
i
++
]
=
'.'
;
m_buf
[
i
]
=
'0'
;
return
;
}
...
...
@@ -8342,18 +8351,35 @@ class basic_json
}
// determine if need to append ".0"
const
auto
data_end
=
m_buf
.
begin
()
+
strlen
(
m_buf
.
data
());
const
bool
value_is_int_like
=
std
::
none_of
(
m_buf
.
begin
(),
data_end
,
[](
const
char
c
)
size_t
i
=
0
;
bool
value_is_int_like
=
true
;
for
(
i
=
0
;
i
<
m_buf
.
size
();
++
i
)
{
return
(
c
==
'.'
or
c
==
'e'
or
c
==
'E'
);
});
assert
(
data_end
+
2
<
m_buf
.
end
());
// break when end of number is reached
if
(
m_buf
[
i
]
==
'\0'
)
{
break
;
}
// check if we find non-int character
value_is_int_like
=
value_is_int_like
and
m_buf
[
i
]
!=
'.'
and
m_buf
[
i
]
!=
'e'
and
m_buf
[
i
]
!=
'E'
;
}
if
(
value_is_int_like
)
{
strcat
(
m_buf
.
data
(),
".0"
);
// there must be 2 bytes left for ".0"
assert
((
i
+
2
)
<
m_buf
.
size
());
// we write to the end of the number
assert
(
m_buf
[
i
]
==
'\0'
);
assert
(
m_buf
[
i
-
1
]
!=
'\0'
);
// add ".0"
m_buf
[
i
]
=
'.'
;
m_buf
[
i
+
1
]
=
'0'
;
// the resulting string is properly terminated
assert
(
m_buf
[
i
+
2
]
==
'\0'
);
}
}
};
...
...
@@ -11136,7 +11162,7 @@ basic_json_parser_74:
// update data to point to the modified bytes
if
((
len
+
1
)
<
buf
.
size
())
{
std
::
copy
(
m_start
,
m_end
,
buf
.
data
());
std
::
copy
(
m_start
,
m_end
,
buf
.
begin
());
buf
[
len
]
=
0
;
buf
[
ds_pos
]
=
decimal_point_char
;
data
=
buf
.
data
();
...
...
src/json.hpp.re2c
View file @
83a9c60d
...
...
@@ -38,7 +38,7 @@ SOFTWARE.
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // int64_t, uint64_t
#include <cstdlib> // abort, strtod, strtof, strtold, strtoul, strtoll, strtoull
#include <cstring> // str
cpy, str
len
#include <cstring> // strlen
#include <forward_list> // forward_list
#include <functional> // function, hash, less
#include <initializer_list> // initializer_list
...
...
@@ -8259,6 +8259,7 @@ class basic_json
template<typename NumberType>
void x_write(NumberType x, /*is_integral=*/std::true_type)
{
// special case for "0"
if (x == 0)
{
m_buf[0] = '0';
...
...
@@ -8291,9 +8292,17 @@ class basic_json
template<typename NumberType>
void x_write(NumberType x, /*is_integral=*/std::false_type)
{
// special case for 0.0 and -0.0
if (x == 0)
{
std::strcpy(m_buf.data(), std::signbit(x) ? "-0.0" : "0.0");
size_t i = 0;
if (std::signbit(x))
{
m_buf[i++] = '-';
}
m_buf[i++] = '0';
m_buf[i++] = '.';
m_buf[i] = '0';
return;
}
...
...
@@ -8342,18 +8351,35 @@ class basic_json
}
// determine if need to append ".0"
const auto data_end = m_buf.begin() + strlen(m_buf.data());
const bool value_is_int_like =
std::none_of(m_buf.begin(), data_end, [](const char c)
size_t i = 0;
bool value_is_int_like = true;
for (i = 0; i < m_buf.size(); ++i)
{
return (c == '.' or c == 'e' or c == 'E');
});
assert(data_end + 2 < m_buf.end());
// break when end of number is reached
if (m_buf[i] == '\0')
{
break;
}
// check if we find non-int character
value_is_int_like = value_is_int_like and m_buf[i] != '.' and
m_buf[i] != 'e' and m_buf[i] != 'E';
}
if (value_is_int_like)
{
strcat(m_buf.data(), ".0");
// there must be 2 bytes left for ".0"
assert((i + 2) < m_buf.size());
// we write to the end of the number
assert(m_buf[i] == '\0');
assert(m_buf[i - 1] != '\0');
// add ".0"
m_buf[i] = '.';
m_buf[i + 1] = '0';
// the resulting string is properly terminated
assert(m_buf[i + 2] == '\0');
}
}
};
...
...
@@ -10170,7 +10196,7 @@ class basic_json
// update data to point to the modified bytes
if ((len + 1) < buf.size())
{
std::copy(m_start, m_end, buf.
data
());
std::copy(m_start, m_end, buf.
begin
());
buf[len] = 0;
buf[ds_pos] = decimal_point_char;
data = buf.data();
...
...
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