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
222aacc2
Commit
222aacc2
authored
Jan 10, 2015
by
Raphael Isemann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Quick and dirty implementation for basic multilingual plane in the unicode escape mechanism
parent
13efc7a0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
0 deletions
+81
-0
src/json.cc
src/json.cc
+73
-0
src/json.h
src/json.h
+4
-0
test/json_unit.cc
test/json_unit.cc
+4
-0
No files found.
src/json.cc
View file @
222aacc2
...
...
@@ -2073,6 +2073,9 @@ std::string json::parser::parseString()
result
+=
'\n'
;
}
else
if
(
currentChar
==
'r'
)
{
result
+=
'\r'
;
}
else
if
(
currentChar
==
'u'
)
{
pos_
++
;
result
+=
parseUnicodeEscape
();
}
else
{
error
(
"expected one of
\\
,/,b,f,n,r,t behind backslash."
);
}
...
...
@@ -2118,6 +2121,76 @@ std::string json::parser::parseString()
error
(
"expected '
\"
'"
);
}
std
::
string
json
::
parser
::
unicodeToUTF8
(
unsigned
int
codepoint
)
{
// it's just a ASCII compatible codepoint,
// so we just interpret the point as a character
if
(
codepoint
<=
0x7f
)
{
return
std
::
string
(
1
,
static_cast
<
char
>
(
codepoint
));
}
else
if
(
codepoint
<=
0x7ff
)
{
std
::
string
result
(
2
,
static_cast
<
char
>
(
0xc0
|
((
codepoint
>>
6
)
&
0x1f
)));
result
[
1
]
=
static_cast
<
char
>
(
0x80
|
(
codepoint
&
0x3f
));
return
result
;
}
else
if
(
codepoint
<=
0xffff
)
{
std
::
string
result
(
3
,
static_cast
<
char
>
(
0xe0
|
((
codepoint
>>
12
)
&
0x0f
)));
result
[
1
]
=
static_cast
<
char
>
(
0x80
|
((
codepoint
>>
6
)
&
0x3f
));
result
[
2
]
=
static_cast
<
char
>
(
0x80
|
(
codepoint
&
0x3f
));
return
result
;
}
else
if
(
codepoint
<=
0x1fffff
)
{
std
::
string
result
(
4
,
static_cast
<
char
>
(
0xf0
|
((
codepoint
>>
18
)
&
0x07
)));
result
[
1
]
=
static_cast
<
char
>
(
0x80
|
((
codepoint
>>
12
)
&
0x3f
));
result
[
2
]
=
static_cast
<
char
>
(
0x80
|
((
codepoint
>>
6
)
&
0x3f
));
result
[
3
]
=
static_cast
<
char
>
(
0x80
|
(
codepoint
&
0x3f
));
return
result
;
}
else
{
std
::
string
errorMessage
=
"Invalid codepoint: "
;
errorMessage
+=
codepoint
;
error
(
errorMessage
);
}
}
/*!
Parses the JSON style unicode escape sequence (\uXXXX).
@return the utf-8 character the escape sequence escaped
@pre An opening quote \p " was read in the main parse function @ref parse.
pos_ is the position after the opening quote.
@post The character after the closing quote \p " is the current character @ref
current_. Whitespace is skipped.
*/
std
::
string
json
::
parser
::
parseUnicodeEscape
()
{
const
auto
startPos
=
pos_
;
if
(
pos_
+
3
>=
buffer_
.
size
())
{
error
(
"Got end of input while parsing unicode escape sequence
\\
uXXXX"
);
}
std
::
string
hexCode
(
4
,
' '
);
for
(;
pos_
<
startPos
+
4
;
pos_
++
)
{
char
currentChar
=
buffer_
[
pos_
];
if
(
(
currentChar
>=
'0'
&&
currentChar
<=
'9'
)
||
(
currentChar
>=
'a'
&&
currentChar
<=
'f'
)
||
(
currentChar
>=
'A'
&&
currentChar
<=
'F'
))
{
// all is well, we have valid hexadecimal chars
// so we copy that char into our string
hexCode
[
pos_
-
startPos
]
=
currentChar
;
}
else
{
error
(
"Found non-hexadecimal character in unicode escape sequence!"
);
}
}
pos_
--
;
// case is safe as 4 hex characters can't present more than 16 bits
return
unicodeToUTF8
(
static_cast
<
unsigned
int
>
(
std
::
stoul
(
hexCode
,
nullptr
,
16
)));
}
/*!
This function is called in case a \p "t" is read in the main parse function
@ref parse. In the standard, the \p "true" token is the only candidate, so the
...
...
src/json.h
View file @
222aacc2
...
...
@@ -418,6 +418,10 @@ class json
inline
void
error
(
const
std
::
string
&
)
__attribute__
((
noreturn
));
/// parse a quoted string
inline
std
::
string
parseString
();
/// transforms a unicode codepoint to it's UTF-8 presentation
inline
std
::
string
unicodeToUTF8
(
unsigned
int
codepoint
);
/// parses a unicode escape sequence
inline
std
::
string
parseUnicodeEscape
();
/// parse a Boolean "true"
inline
void
parseTrue
();
/// parse a Boolean "false"
...
...
test/json_unit.cc
View file @
222aacc2
...
...
@@ -1652,6 +1652,10 @@ TEST_CASE("Parser")
CHECK
(
json
::
parse
(
"
\"
a
\\
nz
\"
"
)
==
json
(
"a
\n
z"
));
CHECK
(
json
::
parse
(
"
\"\\
n
\"
"
)
==
json
(
"
\n
"
));
// escape unicode characters
CHECK
(
json
::
parse
(
"
\"\\
u002F
\"
"
)
==
json
(
"/"
));
CHECK
(
json
::
parse
(
"
\"\\
u00E4
\"
"
)
==
json
(
u8"\u00E4"
));
// escaping senseless stuff
CHECK_THROWS_AS
(
json
::
parse
(
"
\"\\
z
\"
"
),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parse
(
"
\"\\
\"
"
),
std
::
invalid_argument
);
...
...
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