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
9e7941b6
Commit
9e7941b6
authored
Feb 17, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more type adjustments
parent
e2dff46d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
22 deletions
+29
-22
json.hpp
src/json.hpp
+14
-11
json.hpp.re2c
src/json.hpp.re2c
+14
-11
unit.cpp
test/unit.cpp
+1
-0
No files found.
src/json.hpp
View file @
9e7941b6
...
@@ -2529,11 +2529,11 @@ class basic_json
...
@@ -2529,11 +2529,11 @@ class basic_json
};
};
/// the char type to use in the lexer
/// the char type to use in the lexer
using
lexer_char_t
=
typename
string_t
::
value_type
;
using
lexer_char_t
=
unsigned
char
;
/// constructor with a given buffer
/// constructor with a given buffer
inline
lexer
(
const
string_t
&
s
)
noexcept
inline
lexer
(
const
string_t
&
s
)
noexcept
:
m_content
(
s
.
c_str
(
))
:
m_content
(
reinterpret_cast
<
const
lexer_char_t
*>
(
s
.
c_str
()
))
{
{
m_start
=
m_cursor
=
m_content
;
m_start
=
m_cursor
=
m_content
;
m_limit
=
m_content
+
s
.
size
();
m_limit
=
m_content
+
s
.
size
();
...
@@ -2552,7 +2552,7 @@ class basic_json
...
@@ -2552,7 +2552,7 @@ class basic_json
@see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
@see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
*/
*/
inline
static
string_t
to_unicode
(
const
size_t
codepoint1
,
size_t
codepoint2
=
0
)
inline
static
string_t
to_unicode
(
const
size_t
codepoint1
,
const
size_t
codepoint2
=
0
)
{
{
string_t
result
;
string_t
result
;
...
@@ -3380,7 +3380,8 @@ basic_json_parser_59:
...
@@ -3380,7 +3380,8 @@ basic_json_parser_59:
/// return string representation of last read token
/// return string representation of last read token
inline
string_t
get_token
()
const
noexcept
inline
string_t
get_token
()
const
noexcept
{
{
return
string_t
(
m_start
,
static_cast
<
size_t
>
(
m_cursor
-
m_start
));
return
string_t
(
reinterpret_cast
<
typename
string_t
::
const_pointer
>
(
m_start
),
static_cast
<
size_t
>
(
m_cursor
-
m_start
));
}
}
/*!
/*!
...
@@ -3410,7 +3411,7 @@ basic_json_parser_59:
...
@@ -3410,7 +3411,7 @@ basic_json_parser_59:
result
.
reserve
(
static_cast
<
size_t
>
(
m_cursor
-
m_start
-
2
));
result
.
reserve
(
static_cast
<
size_t
>
(
m_cursor
-
m_start
-
2
));
// iterate the result between the quotes
// iterate the result between the quotes
for
(
const
typename
string_t
::
value_type
*
i
=
m_start
+
1
;
i
<
m_cursor
-
1
;
++
i
)
for
(
const
lexer_char_t
*
i
=
m_start
+
1
;
i
<
m_cursor
-
1
;
++
i
)
{
{
// process escaped characters
// process escaped characters
if
(
*
i
==
'\\'
)
if
(
*
i
==
'\\'
)
...
@@ -3468,7 +3469,8 @@ basic_json_parser_59:
...
@@ -3468,7 +3469,8 @@ basic_json_parser_59:
case
'u'
:
case
'u'
:
{
{
// get code xxxx from uxxxx
// get code xxxx from uxxxx
auto
codepoint
=
std
::
strtoul
(
std
::
string
(
i
+
1
,
4
).
c_str
(),
nullptr
,
16
);
auto
codepoint
=
std
::
strtoul
(
std
::
string
(
reinterpret_cast
<
typename
string_t
::
const_pointer
>
(
i
+
1
),
4
).
c_str
(),
nullptr
,
16
);
if
(
codepoint
>=
0xD800
and
codepoint
<=
0xDBFF
)
if
(
codepoint
>=
0xD800
and
codepoint
<=
0xDBFF
)
{
{
...
@@ -3479,7 +3481,8 @@ basic_json_parser_59:
...
@@ -3479,7 +3481,8 @@ basic_json_parser_59:
}
}
// get code yyyy from uxxxx\uyyyy
// get code yyyy from uxxxx\uyyyy
auto
codepoint2
=
std
::
strtoul
(
std
::
string
(
i
+
7
,
4
).
c_str
(),
nullptr
,
16
);
auto
codepoint2
=
std
::
strtoul
(
std
::
string
(
reinterpret_cast
<
typename
string_t
::
const_pointer
>
(
i
+
7
),
4
).
c_str
(),
nullptr
,
16
);
result
+=
to_unicode
(
codepoint
,
codepoint2
);
result
+=
to_unicode
(
codepoint
,
codepoint2
);
// skip the next 11 characters (xxxx\uyyyy)
// skip the next 11 characters (xxxx\uyyyy)
i
+=
11
;
i
+=
11
;
...
@@ -3499,7 +3502,7 @@ basic_json_parser_59:
...
@@ -3499,7 +3502,7 @@ basic_json_parser_59:
{
{
// all other characters are just copied to the end of the
// all other characters are just copied to the end of the
// string
// string
result
.
append
(
1
,
*
i
);
result
.
append
(
1
,
static_cast
<
typename
string_t
::
value_type
>
(
*
i
)
);
}
}
}
}
...
@@ -3526,13 +3529,13 @@ basic_json_parser_59:
...
@@ -3526,13 +3529,13 @@ basic_json_parser_59:
inline
number_float_t
get_number
()
const
inline
number_float_t
get_number
()
const
{
{
// conversion
// conversion
lexer_char_t
*
endptr
;
typename
string_t
::
value_type
*
endptr
;
const
auto
float_val
=
std
::
strtod
(
reinterpret_cast
<
const
lexer_char_t
*
>
(
m_start
),
const
auto
float_val
=
std
::
strtod
(
reinterpret_cast
<
typename
string_t
::
const_pointer
>
(
m_start
),
&
endptr
);
&
endptr
);
// return float_val if the whole number was translated and NAN
// return float_val if the whole number was translated and NAN
// otherwise
// otherwise
return
(
endptr
==
m_cursor
)
?
float_val
:
NAN
;
return
(
reinterpret_cast
<
lexer_char_t
*>
(
endptr
)
==
m_cursor
)
?
float_val
:
NAN
;
}
}
private
:
private
:
...
...
src/json.hpp.re2c
View file @
9e7941b6
...
@@ -2529,11 +2529,11 @@ class basic_json
...
@@ -2529,11 +2529,11 @@ class basic_json
};
};
/// the char type to use in the lexer
/// the char type to use in the lexer
using lexer_char_t =
typename string_t::value_type
;
using lexer_char_t =
unsigned char
;
/// constructor with a given buffer
/// constructor with a given buffer
inline lexer(const string_t& s) noexcept
inline lexer(const string_t& s) noexcept
: m_content(
s.c_str(
))
: m_content(
reinterpret_cast<const lexer_char_t*>(s.c_str()
))
{
{
m_start = m_cursor = m_content;
m_start = m_cursor = m_content;
m_limit = m_content + s.size();
m_limit = m_content + s.size();
...
@@ -2552,7 +2552,7 @@ class basic_json
...
@@ -2552,7 +2552,7 @@ class basic_json
@see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
@see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
*/
*/
inline static string_t to_unicode(const size_t codepoint1, size_t codepoint2 = 0)
inline static string_t to_unicode(const size_t codepoint1,
const
size_t codepoint2 = 0)
{
{
string_t result;
string_t result;
...
@@ -2729,7 +2729,8 @@ class basic_json
...
@@ -2729,7 +2729,8 @@ class basic_json
/// return string representation of last read token
/// return string representation of last read token
inline string_t get_token() const noexcept
inline string_t get_token() const noexcept
{
{
return string_t(m_start, static_cast<size_t>(m_cursor - m_start));
return string_t(reinterpret_cast<typename string_t::const_pointer>(m_start),
static_cast<size_t>(m_cursor - m_start));
}
}
/*!
/*!
...
@@ -2759,7 +2760,7 @@ class basic_json
...
@@ -2759,7 +2760,7 @@ class basic_json
result.reserve(static_cast<size_t>(m_cursor - m_start - 2));
result.reserve(static_cast<size_t>(m_cursor - m_start - 2));
// iterate the result between the quotes
// iterate the result between the quotes
for (const
typename string_t::value_type
* i = m_start + 1; i < m_cursor - 1; ++i)
for (const
lexer_char_t
* i = m_start + 1; i < m_cursor - 1; ++i)
{
{
// process escaped characters
// process escaped characters
if (*i == '\\')
if (*i == '\\')
...
@@ -2817,7 +2818,8 @@ class basic_json
...
@@ -2817,7 +2818,8 @@ class basic_json
case 'u':
case 'u':
{
{
// get code xxxx from uxxxx
// get code xxxx from uxxxx
auto codepoint = std::strtoul(std::string(i + 1, 4).c_str(), nullptr, 16);
auto codepoint = std::strtoul(std::string(reinterpret_cast<typename string_t::const_pointer>(i + 1),
4).c_str(), nullptr, 16);
if (codepoint >= 0xD800 and codepoint <= 0xDBFF)
if (codepoint >= 0xD800 and codepoint <= 0xDBFF)
{
{
...
@@ -2828,7 +2830,8 @@ class basic_json
...
@@ -2828,7 +2830,8 @@ class basic_json
}
}
// get code yyyy from uxxxx\uyyyy
// get code yyyy from uxxxx\uyyyy
auto codepoint2 = std::strtoul(std::string(i + 7, 4).c_str(), nullptr, 16);
auto codepoint2 = std::strtoul(std::string(reinterpret_cast<typename string_t::const_pointer>
(i + 7), 4).c_str(), nullptr, 16);
result += to_unicode(codepoint, codepoint2);
result += to_unicode(codepoint, codepoint2);
// skip the next 11 characters (xxxx\uyyyy)
// skip the next 11 characters (xxxx\uyyyy)
i += 11;
i += 11;
...
@@ -2848,7 +2851,7 @@ class basic_json
...
@@ -2848,7 +2851,7 @@ class basic_json
{
{
// all other characters are just copied to the end of the
// all other characters are just copied to the end of the
// string
// string
result.append(1,
*i
);
result.append(1,
static_cast<typename string_t::value_type>(*i)
);
}
}
}
}
...
@@ -2875,13 +2878,13 @@ class basic_json
...
@@ -2875,13 +2878,13 @@ class basic_json
inline number_float_t get_number() const
inline number_float_t get_number() const
{
{
// conversion
// conversion
lexer_char_t
* endptr;
typename string_t::value_type
* endptr;
const auto float_val = std::strtod(reinterpret_cast<
const lexer_char_t*
>(m_start),
const auto float_val = std::strtod(reinterpret_cast<
typename string_t::const_pointer
>(m_start),
&endptr);
&endptr);
// return float_val if the whole number was translated and NAN
// return float_val if the whole number was translated and NAN
// otherwise
// otherwise
return (
endptr
== m_cursor) ? float_val : NAN;
return (
reinterpret_cast<lexer_char_t*>(endptr)
== m_cursor) ? float_val : NAN;
}
}
private:
private:
...
...
test/unit.cpp
View file @
9e7941b6
...
@@ -5652,6 +5652,7 @@ TEST_CASE("parser class")
...
@@ -5652,6 +5652,7 @@ TEST_CASE("parser class")
CHECK
(
json
::
parser
(
"
\"\\
u2000
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
" "
);
CHECK
(
json
::
parser
(
"
\"\\
u2000
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
" "
);
CHECK
(
json
::
parser
(
"
\"\\
uFFFF
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
""
);
CHECK
(
json
::
parser
(
"
\"\\
uFFFF
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
""
);
CHECK
(
json
::
parser
(
"
\"\\
u20AC
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
"€"
);
CHECK
(
json
::
parser
(
"
\"\\
u20AC
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
"€"
);
CHECK
(
json
::
parser
(
"
\"
€
\"
"
).
parse
().
get
<
json
::
string_t
>
()
==
"€"
);
CHECK
(
json
::
parse
(
"
\"\\
ud80c
\\
udc60
\"
"
).
get
<
json
::
string_t
>
()
==
u8"\U00013060"
);
CHECK
(
json
::
parse
(
"
\"\\
ud80c
\\
udc60
\"
"
).
get
<
json
::
string_t
>
()
==
u8"\U00013060"
);
CHECK
(
json
::
parse
(
"
\"\\
ud83c
\\
udf1e
\"
"
).
get
<
json
::
string_t
>
()
==
"🌞"
);
CHECK
(
json
::
parse
(
"
\"\\
ud83c
\\
udf1e
\"
"
).
get
<
json
::
string_t
>
()
==
"🌞"
);
...
...
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