Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
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
Operations
Operations
Metrics
Environments
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
cpp-libs
Actor Framework
Commits
2cfa6ee6
Commit
2cfa6ee6
authored
Aug 07, 2019
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add parse function for floating points
parent
27bf1b1e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
33 deletions
+72
-33
libcaf_core/caf/detail/parser/read_floating_point.hpp
libcaf_core/caf/detail/parser/read_floating_point.hpp
+6
-3
libcaf_core/src/parse.cpp
libcaf_core/src/parse.cpp
+5
-0
libcaf_core/test/parse.cpp
libcaf_core/test/parse.cpp
+61
-30
No files found.
libcaf_core/caf/detail/parser/read_floating_point.hpp
View file @
2cfa6ee6
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#pragma once
#pragma once
#include <cstdint>
#include <cstdint>
#include <type_traits>
#include "caf/config.hpp"
#include "caf/config.hpp"
#include "caf/detail/parser/add_ascii.hpp"
#include "caf/detail/parser/add_ascii.hpp"
...
@@ -28,6 +29,7 @@
...
@@ -28,6 +29,7 @@
#include "caf/detail/parser/state.hpp"
#include "caf/detail/parser/state.hpp"
#include "caf/detail/parser/sub_ascii.hpp"
#include "caf/detail/parser/sub_ascii.hpp"
#include "caf/detail/scope_guard.hpp"
#include "caf/detail/scope_guard.hpp"
#include "caf/optional.hpp"
#include "caf/pec.hpp"
#include "caf/pec.hpp"
CAF_PUSH_UNUSED_LABEL_WARNING
CAF_PUSH_UNUSED_LABEL_WARNING
...
@@ -44,7 +46,7 @@ namespace parser {
...
@@ -44,7 +46,7 @@ namespace parser {
/// @param start_value Allows another parser to pre-initialize this parser with
/// @param start_value Allows another parser to pre-initialize this parser with
/// the pre-decimal value.
/// the pre-decimal value.
template
<
class
Iterator
,
class
Sentinel
,
class
Consumer
,
class
ValueType
>
template
<
class
Iterator
,
class
Sentinel
,
class
Consumer
,
class
ValueType
>
void
read_floating_point
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&
consumer
,
void
read_floating_point
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&
&
consumer
,
optional
<
ValueType
>
start_value
,
optional
<
ValueType
>
start_value
,
bool
negative
=
false
)
{
bool
negative
=
false
)
{
// Any exponent larger than 511 always overflows.
// Any exponent larger than 511 always overflows.
...
@@ -184,8 +186,9 @@ void read_floating_point(state<Iterator, Sentinel>& ps, Consumer& consumer,
...
@@ -184,8 +186,9 @@ void read_floating_point(state<Iterator, Sentinel>& ps, Consumer& consumer,
}
}
template
<
class
Iterator
,
class
Sentinel
,
class
Consumer
>
template
<
class
Iterator
,
class
Sentinel
,
class
Consumer
>
void
read_floating_point
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&
consumer
)
{
void
read_floating_point
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&&
consumer
)
{
using
value_type
=
typename
Consumer
::
value_type
;
using
consumer_type
=
typename
std
::
decay
<
Consumer
>::
type
;
using
value_type
=
typename
consumer_type
::
value_type
;
return
read_floating_point
(
ps
,
consumer
,
optional
<
value_type
>
{});
return
read_floating_point
(
ps
,
consumer
,
optional
<
value_type
>
{});
}
}
...
...
libcaf_core/src/parse.cpp
View file @
2cfa6ee6
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include "caf/detail/parse.hpp"
#include "caf/detail/parse.hpp"
#include "caf/detail/consumer.hpp"
#include "caf/detail/consumer.hpp"
#include "caf/detail/parser/read_floating_point.hpp"
#include "caf/detail/parser/read_signed_integer.hpp"
#include "caf/detail/parser/read_signed_integer.hpp"
#include "caf/detail/parser/read_unsigned_integer.hpp"
#include "caf/detail/parser/read_unsigned_integer.hpp"
...
@@ -46,5 +47,9 @@ PARSE_IMPL(uint32_t, unsigned_integer)
...
@@ -46,5 +47,9 @@ PARSE_IMPL(uint32_t, unsigned_integer)
PARSE_IMPL
(
uint64_t
,
unsigned_integer
)
PARSE_IMPL
(
uint64_t
,
unsigned_integer
)
PARSE_IMPL
(
float
,
floating_point
)
PARSE_IMPL
(
double
,
floating_point
)
}
// namespace detail
}
// namespace detail
}
// namespace caf
}
// namespace caf
libcaf_core/test/parse.cpp
View file @
2cfa6ee6
...
@@ -41,45 +41,57 @@ expected<T> read(string_view str) {
...
@@ -41,45 +41,57 @@ expected<T> read(string_view str) {
}
// namespace
}
// namespace
#define CHECK_INT(type, value) CAF_CHECK_EQUAL(read<type>(#value), type(value))
#define CHECK_NUMBER(type, value) \
CAF_CHECK_EQUAL(read<type>(#value), type(value))
#define CHECK_
INT_3(type, value, cpp_value)
\
#define CHECK_
NUMBER_3(type, value, cpp_value)
\
CAF_CHECK_EQUAL(read<type>(#value), type(cpp_value))
CAF_CHECK_EQUAL(read<type>(#value), type(cpp_value))
#define CHECK_INVALID(type, str, code) CAF_CHECK_EQUAL(read<type>(str), code)
#define CHECK_INVALID(type, str, code) CAF_CHECK_EQUAL(read<type>(str), code)
CAF_TEST
(
valid
signed
integers
)
{
CAF_TEST
(
valid
signed
integers
)
{
CHECK_INT
(
int8_t
,
-
128
);
CHECK_NUMBER
(
int8_t
,
-
128
);
CHECK_INT
(
int8_t
,
127
);
CHECK_NUMBER
(
int8_t
,
127
);
CHECK_INT
(
int8_t
,
+
127
);
CHECK_NUMBER
(
int8_t
,
+
127
);
CHECK_INT
(
int16_t
,
-
32768
);
CHECK_NUMBER
(
int16_t
,
-
32768
);
CHECK_INT
(
int16_t
,
32767
);
CHECK_NUMBER
(
int16_t
,
32767
);
CHECK_INT
(
int16_t
,
+
32767
);
CHECK_NUMBER
(
int16_t
,
+
32767
);
CHECK_INT
(
int32_t
,
-
2147483648
);
CHECK_NUMBER
(
int32_t
,
-
2147483648
);
CHECK_INT
(
int32_t
,
2147483647
);
CHECK_NUMBER
(
int32_t
,
2147483647
);
CHECK_INT
(
int32_t
,
+
2147483647
);
CHECK_NUMBER
(
int32_t
,
+
2147483647
);
CHECK_INT
(
int64_t
,
-
9223372036854775807
);
CHECK_NUMBER
(
int64_t
,
-
9223372036854775807
);
CHECK_INT
(
int64_t
,
9223372036854775807
);
CHECK_NUMBER
(
int64_t
,
9223372036854775807
);
CHECK_INT
(
int64_t
,
+
9223372036854775807
);
CHECK_NUMBER
(
int64_t
,
+
9223372036854775807
);
}
CAF_TEST
(
invalid
signed
integers
)
{
CHECK_INVALID
(
int8_t
,
"--1"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
int8_t
,
"++1"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
int8_t
,
"-129"
,
pec
::
integer_underflow
);
CHECK_INVALID
(
int8_t
,
"128"
,
pec
::
integer_overflow
);
CHECK_INVALID
(
int8_t
,
"~1"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
int8_t
,
"1!"
,
pec
::
trailing_character
);
CHECK_INVALID
(
int8_t
,
"+"
,
pec
::
unexpected_eof
);
CHECK_INVALID
(
int8_t
,
"-"
,
pec
::
unexpected_eof
);
}
}
CAF_TEST
(
valid
unsigned
integers
)
{
CAF_TEST
(
valid
unsigned
integers
)
{
CHECK_
INT
(
uint8_t
,
0
);
CHECK_
NUMBER
(
uint8_t
,
0
);
CHECK_
INT
(
uint8_t
,
+
0
);
CHECK_
NUMBER
(
uint8_t
,
+
0
);
CHECK_
INT
(
uint8_t
,
255
);
CHECK_
NUMBER
(
uint8_t
,
255
);
CHECK_
INT
(
uint8_t
,
+
255
);
CHECK_
NUMBER
(
uint8_t
,
+
255
);
CHECK_
INT
(
uint16_t
,
0
);
CHECK_
NUMBER
(
uint16_t
,
0
);
CHECK_
INT
(
uint16_t
,
+
0
);
CHECK_
NUMBER
(
uint16_t
,
+
0
);
CHECK_
INT
(
uint16_t
,
65535
);
CHECK_
NUMBER
(
uint16_t
,
65535
);
CHECK_
INT
(
uint16_t
,
+
65535
);
CHECK_
NUMBER
(
uint16_t
,
+
65535
);
CHECK_
INT
(
uint32_t
,
0
);
CHECK_
NUMBER
(
uint32_t
,
0
);
CHECK_
INT
(
uint32_t
,
+
0
);
CHECK_
NUMBER
(
uint32_t
,
+
0
);
CHECK_
INT
(
uint32_t
,
4294967295
);
CHECK_
NUMBER
(
uint32_t
,
4294967295
);
CHECK_
INT
(
uint32_t
,
+
4294967295
);
CHECK_
NUMBER
(
uint32_t
,
+
4294967295
);
CHECK_
INT
(
uint64_t
,
0
);
CHECK_
NUMBER
(
uint64_t
,
0
);
CHECK_
INT
(
uint64_t
,
+
0
);
CHECK_
NUMBER
(
uint64_t
,
+
0
);
CHECK_
INT
_3
(
uint64_t
,
18446744073709551615
,
18446744073709551615ULL
);
CHECK_
NUMBER
_3
(
uint64_t
,
18446744073709551615
,
18446744073709551615ULL
);
CHECK_
INT
_3
(
uint64_t
,
+
18446744073709551615
,
18446744073709551615ULL
);
CHECK_
NUMBER
_3
(
uint64_t
,
+
18446744073709551615
,
18446744073709551615ULL
);
}
}
CAF_TEST
(
invalid
unsigned
integers
)
{
CAF_TEST
(
invalid
unsigned
integers
)
{
...
@@ -88,4 +100,23 @@ CAF_TEST(invalid unsigned integers) {
...
@@ -88,4 +100,23 @@ CAF_TEST(invalid unsigned integers) {
CHECK_INVALID
(
uint8_t
,
"256"
,
pec
::
integer_overflow
);
CHECK_INVALID
(
uint8_t
,
"256"
,
pec
::
integer_overflow
);
CHECK_INVALID
(
uint8_t
,
"~1"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
uint8_t
,
"~1"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
uint8_t
,
"1!"
,
pec
::
trailing_character
);
CHECK_INVALID
(
uint8_t
,
"1!"
,
pec
::
trailing_character
);
CHECK_INVALID
(
uint8_t
,
"+"
,
pec
::
unexpected_eof
);
}
CAF_TEST
(
valid
floating
point
numbers
)
{
CHECK_NUMBER
(
float
,
1
);
CHECK_NUMBER
(
double
,
1
);
CHECK_NUMBER
(
double
,
0.01e10
);
CHECK_NUMBER
(
double
,
10e-10
);
CHECK_NUMBER
(
double
,
-
10e-10
);
}
CAF_TEST
(
invalid
floating
point
numbers
)
{
CHECK_INVALID
(
float
,
"1.."
,
pec
::
trailing_character
);
CHECK_INVALID
(
double
,
"..1"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
double
,
"+"
,
pec
::
unexpected_eof
);
CHECK_INVALID
(
double
,
"-"
,
pec
::
unexpected_eof
);
CHECK_INVALID
(
double
,
"1e"
,
pec
::
unexpected_eof
);
CHECK_INVALID
(
double
,
"--0.01e10"
,
pec
::
unexpected_character
);
CHECK_INVALID
(
double
,
"++10e-10"
,
pec
::
unexpected_character
);
}
}
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