Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
9598430a
Commit
9598430a
authored
Nov 12, 2015
by
Kamal Marhubi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Drop <regex> in number parsing
parent
c662e628
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
18 deletions
+36
-18
json-test.c++
c++/src/capnp/compat/json-test.c++
+3
-3
json.c++
c++/src/capnp/compat/json.c++
+33
-15
No files found.
c++/src/capnp/compat/json-test.c++
View file @
9598430a
...
...
@@ -382,7 +382,7 @@ KJ_TEST("basic json decoding") {
MallocMessageBuilder
message
;
auto
root
=
message
.
initRoot
<
JsonValue
>
();
KJ_EXPECT_THROW_MESSAGE
(
"
Expected number
"
,
json
.
decodeRaw
(
"-"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"
ends prematurely
"
,
json
.
decodeRaw
(
"-"
,
root
));
}
{
...
...
@@ -426,8 +426,8 @@ KJ_TEST("basic json decoding") {
KJ_EXPECT_THROW_MESSAGE
(
"Invalid escape"
,
json
.
decodeRaw
(
R"("\z")"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"ends prematurely"
,
json
.
decodeRaw
(
R"(["\n\", 3])"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Invalid hex"
,
json
.
decodeRaw
(
R"("\u12zz")"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"
Expected number
"
,
json
.
decodeRaw
(
"-"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"
Expected number
"
,
json
.
decodeRaw
(
"--"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"
ends prematurely
"
,
json
.
decodeRaw
(
"-"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"
Unexpected input
"
,
json
.
decodeRaw
(
"--"
,
root
));
}
}
...
...
c++/src/capnp/compat/json.c++
View file @
9598430a
...
...
@@ -20,9 +20,9 @@
// THE SOFTWARE.
#include "json.h"
#include <cmath> // for HUGEVAL to check for overflow in std::strtod
#include <cstdlib> // std::strtod
#include <errno.h> // for std::strtod errors
#include <regex> // for numbers
#include <unordered_map>
#include <capnp/orphan.h>
#include <kj/debug.h>
...
...
@@ -543,6 +543,21 @@ public:
advance
(
expected
.
size
());
}
bool
tryConsume
(
char
expected
)
{
bool
found
=
!
inputExhausted
()
&&
nextChar
()
==
expected
;
if
(
found
)
{
advance
();
}
return
found
;
}
template
<
typename
Predicate
>
void
consumeOne
(
Predicate
&&
predicate
)
{
char
current
=
nextChar
();
KJ_REQUIRE
(
predicate
(
current
),
"Unexpected input in JSON message."
);
advance
();
}
template
<
typename
Predicate
>
kj
::
ArrayPtr
<
const
char
>
consumeWhile
(
Predicate
&&
predicate
)
{
auto
originalPos
=
remaining_
.
begin
();
...
...
@@ -607,26 +622,29 @@ public:
}
kj
::
String
consumeNumber
()
{
// TODO(perf): <regex> could be eliminated.
static
std
::
regex
jsonNumber
(
R"(-?(0|[1-9][0-9]*)(\.[0-9]+)?((e|E)([+-])?[0-9]+)?)"
);
// jsonNumber matches numbers as seen in railroad diagram at http://json.org/.
auto
originalPos
=
remaining_
.
begin
();
tryConsume
(
'-'
);
if
(
!
tryConsume
(
'0'
))
{
consumeOne
([](
char
c
)
{
return
'1'
<=
c
&&
c
<=
'9'
;
});
consumeWhile
([](
char
c
)
{
return
'0'
<=
c
&&
c
<=
'9'
;
});
}
std
::
cmatch
match
;
bool
foundNumber
=
std
::
regex_search
(
remaining_
.
begin
(),
remaining_
.
end
(),
match
,
jsonNumber
,
std
::
regex_constants
::
match_continuous
);
if
(
tryConsume
(
'.'
))
{
consumeWhile
([](
char
c
)
{
return
'0'
<=
c
&&
c
<=
'9'
;
});
}
if
(
tryConsume
(
'e'
)
||
tryConsume
(
'E'
))
{
tryConsume
(
'+'
)
||
tryConsume
(
'-'
);
consumeWhile
([](
char
c
)
{
return
'0'
<=
c
&&
c
<=
'9'
;
});
}
KJ_REQUIRE
(
foundNumber
,
"Expected number in JSON input."
);
KJ_REQUIRE
(
remaining_
.
begin
()
!=
originalPos
,
"Expected number in JSON input."
);
kj
::
Vector
<
char
>
number
;
number
.
addAll
(
match
[
0
].
first
,
match
[
0
].
second
);
number
.
addAll
(
originalPos
,
remaining_
.
begin
()
);
number
.
add
(
'\0'
);
advanceTo
(
match
.
suffix
().
first
);
return
kj
::
String
(
number
.
releaseAsArray
());
}
...
...
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