Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
22 deletions
+74
-22
json.hpp
src/json.hpp
+37
-11
json.hpp.re2c
src/json.hpp.re2c
+37
-11
No files found.
src/json.hpp
View file @
83a9c60d
...
@@ -38,7 +38,7 @@ SOFTWARE.
...
@@ -38,7 +38,7 @@ SOFTWARE.
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // int64_t, uint64_t
#include <cstdint> // int64_t, uint64_t
#include <cstdlib> // abort, strtod, strtof, strtold, strtoul, strtoll, strtoull
#include <cstdlib> // abort, strtod, strtof, strtold, strtoul, strtoll, strtoull
#include <cstring> // str
cpy, str
len
#include <cstring> // strlen
#include <forward_list> // forward_list
#include <forward_list> // forward_list
#include <functional> // function, hash, less
#include <functional> // function, hash, less
#include <initializer_list> // initializer_list
#include <initializer_list> // initializer_list
...
@@ -8259,6 +8259,7 @@ class basic_json
...
@@ -8259,6 +8259,7 @@ class basic_json
template
<
typename
NumberType
>
template
<
typename
NumberType
>
void
x_write
(
NumberType
x
,
/*is_integral=*/
std
::
true_type
)
void
x_write
(
NumberType
x
,
/*is_integral=*/
std
::
true_type
)
{
{
// special case for "0"
if
(
x
==
0
)
if
(
x
==
0
)
{
{
m_buf
[
0
]
=
'0'
;
m_buf
[
0
]
=
'0'
;
...
@@ -8291,9 +8292,17 @@ class basic_json
...
@@ -8291,9 +8292,17 @@ class basic_json
template
<
typename
NumberType
>
template
<
typename
NumberType
>
void
x_write
(
NumberType
x
,
/*is_integral=*/
std
::
false_type
)
void
x_write
(
NumberType
x
,
/*is_integral=*/
std
::
false_type
)
{
{
// special case for 0.0 and -0.0
if
(
x
==
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
;
return
;
}
}
...
@@ -8342,18 +8351,35 @@ class basic_json
...
@@ -8342,18 +8351,35 @@ class basic_json
}
}
// determine if need to append ".0"
// determine if need to append ".0"
const
auto
data_end
=
m_buf
.
begin
()
+
strlen
(
m_buf
.
data
());
size_t
i
=
0
;
bool
value_is_int_like
=
true
;
const
bool
value_is_int_like
=
for
(
i
=
0
;
i
<
m_buf
.
size
();
++
i
)
std
::
none_of
(
m_buf
.
begin
(),
data_end
,
[](
const
char
c
)
{
{
return
(
c
==
'.'
or
c
==
'e'
or
c
==
'E'
);
// break when end of number is reached
});
if
(
m_buf
[
i
]
==
'\0'
)
assert
(
data_end
+
2
<
m_buf
.
end
());
{
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
)
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:
...
@@ -11136,7 +11162,7 @@ basic_json_parser_74:
// update data to point to the modified bytes
// update data to point to the modified bytes
if
((
len
+
1
)
<
buf
.
size
())
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
[
len
]
=
0
;
buf
[
ds_pos
]
=
decimal_point_char
;
buf
[
ds_pos
]
=
decimal_point_char
;
data
=
buf
.
data
();
data
=
buf
.
data
();
...
...
src/json.hpp.re2c
View file @
83a9c60d
...
@@ -38,7 +38,7 @@ SOFTWARE.
...
@@ -38,7 +38,7 @@ SOFTWARE.
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // int64_t, uint64_t
#include <cstdint> // int64_t, uint64_t
#include <cstdlib> // abort, strtod, strtof, strtold, strtoul, strtoll, strtoull
#include <cstdlib> // abort, strtod, strtof, strtold, strtoul, strtoll, strtoull
#include <cstring> // str
cpy, str
len
#include <cstring> // strlen
#include <forward_list> // forward_list
#include <forward_list> // forward_list
#include <functional> // function, hash, less
#include <functional> // function, hash, less
#include <initializer_list> // initializer_list
#include <initializer_list> // initializer_list
...
@@ -8259,6 +8259,7 @@ class basic_json
...
@@ -8259,6 +8259,7 @@ class basic_json
template<typename NumberType>
template<typename NumberType>
void x_write(NumberType x, /*is_integral=*/std::true_type)
void x_write(NumberType x, /*is_integral=*/std::true_type)
{
{
// special case for "0"
if (x == 0)
if (x == 0)
{
{
m_buf[0] = '0';
m_buf[0] = '0';
...
@@ -8291,9 +8292,17 @@ class basic_json
...
@@ -8291,9 +8292,17 @@ class basic_json
template<typename NumberType>
template<typename NumberType>
void x_write(NumberType x, /*is_integral=*/std::false_type)
void x_write(NumberType x, /*is_integral=*/std::false_type)
{
{
// special case for 0.0 and -0.0
if (x == 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;
return;
}
}
...
@@ -8342,18 +8351,35 @@ class basic_json
...
@@ -8342,18 +8351,35 @@ class basic_json
}
}
// determine if need to append ".0"
// determine if need to append ".0"
const auto data_end = m_buf.begin() + strlen(m_buf.data());
size_t i = 0;
bool value_is_int_like = true;
const bool value_is_int_like =
for (i = 0; i < m_buf.size(); ++i)
std::none_of(m_buf.begin(), data_end, [](const char c)
{
{
return (c == '.' or c == 'e' or c == 'E');
// break when end of number is reached
});
if (m_buf[i] == '\0')
assert(data_end + 2 < m_buf.end());
{
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)
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
...
@@ -10170,7 +10196,7 @@ class basic_json
// update data to point to the modified bytes
// update data to point to the modified bytes
if ((len + 1) < buf.size())
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[len] = 0;
buf[ds_pos] = decimal_point_char;
buf[ds_pos] = decimal_point_char;
data = buf.data();
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