Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
libcifpp
Overview
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
libcifpp
Commits
951ff9b9
Unverified
Commit
951ff9b9
authored
Sep 26, 2023
by
Maarten L. Hekkelman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better conversion from string to int
parent
641f06a7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
4 deletions
+43
-4
include/cif++/item.hpp
+18
-4
test/unit-v2-test.cpp
+25
-0
No files found.
include/cif++/item.hpp
View file @
951ff9b9
...
...
@@ -459,9 +459,14 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
{
auto
txt
=
ref
.
text
();
std
::
from_chars_result
r
=
selected_charconv
<
value_type
>::
from_chars
(
txt
.
data
(),
txt
.
data
()
+
txt
.
size
(),
result
);
auto
b
=
txt
.
data
();
auto
e
=
txt
.
data
()
+
txt
.
size
();
if
(
r
.
ec
!=
std
::
errc
())
std
::
from_chars_result
r
=
(
b
+
1
<
e
and
*
b
==
'+'
and
std
::
isdigit
(
b
[
1
]))
?
selected_charconv
<
value_type
>::
from_chars
(
b
+
1
,
e
,
result
)
:
selected_charconv
<
value_type
>::
from_chars
(
b
,
e
,
result
);
if
(
r
.
ec
!=
std
::
errc
()
or
r
.
ptr
!=
e
)
{
result
=
{};
if
(
cif
::
VERBOSE
)
...
...
@@ -470,6 +475,8 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
std
::
cerr
<<
"Attempt to convert "
<<
std
::
quoted
(
txt
)
<<
" into a number
\n
"
;
else
if
(
r
.
ec
==
std
::
errc
::
result_out_of_range
)
std
::
cerr
<<
"Conversion of "
<<
std
::
quoted
(
txt
)
<<
" into a type that is too small
\n
"
;
else
std
::
cerr
<<
"Not a valid number "
<<
std
::
quoted
(
txt
)
<<
'\n'
;
}
}
}
...
...
@@ -489,9 +496,14 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
{
value_type
v
=
{};
std
::
from_chars_result
r
=
selected_charconv
<
value_type
>::
from_chars
(
txt
.
data
(),
txt
.
data
()
+
txt
.
size
(),
v
);
auto
b
=
txt
.
data
();
auto
e
=
txt
.
data
()
+
txt
.
size
();
std
::
from_chars_result
r
=
(
b
+
1
<
e
and
*
b
==
'+'
and
std
::
isdigit
(
b
[
1
]))
?
selected_charconv
<
value_type
>::
from_chars
(
b
+
1
,
e
,
v
)
:
selected_charconv
<
value_type
>::
from_chars
(
b
,
e
,
v
);
if
(
r
.
ec
!=
std
::
errc
())
if
(
r
.
ec
!=
std
::
errc
()
or
r
.
ptr
!=
e
)
{
if
(
cif
::
VERBOSE
)
{
...
...
@@ -499,6 +511,8 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_arithmetic_v<T> an
std
::
cerr
<<
"Attempt to convert "
<<
std
::
quoted
(
txt
)
<<
" into a number
\n
"
;
else
if
(
r
.
ec
==
std
::
errc
::
result_out_of_range
)
std
::
cerr
<<
"Conversion of "
<<
std
::
quoted
(
txt
)
<<
" into a type that is too small
\n
"
;
else
std
::
cerr
<<
"Not a valid number "
<<
std
::
quoted
(
txt
)
<<
'\n'
;
}
result
=
1
;
}
...
...
test/unit-v2-test.cpp
View file @
951ff9b9
...
...
@@ -150,6 +150,31 @@ BOOST_AUTO_TEST_CASE(cc_2)
}
}
BOOST_AUTO_TEST_CASE
(
cc_3
)
{
cif
::
category
c
(
"foo"
);
c
.
emplace
({
{
"f-1"
,
1
},
{
"f-2"
,
"-1"
},
{
"f-3"
,
"+1"
},
{
"f-4"
,
" 1"
},
{
"f-5"
,
" +1"
},
{
"f-6"
,
"1 "
},
});
auto
row
=
c
.
front
();
BOOST_CHECK_EQUAL
(
row
[
"f-1"
].
as
<
int
>
(),
1
);
BOOST_CHECK_EQUAL
(
row
[
"f-2"
].
as
<
int
>
(),
-
1
);
BOOST_CHECK_EQUAL
(
row
[
"f-3"
].
as
<
int
>
(),
1
);
// BOOST_CHECK_THROW(row["f-4"].as<int>(), std::exception);
// BOOST_CHECK_THROW(row["f-5"].as<int>(), std::exception);
// BOOST_CHECK_THROW(row["f-6"].as<int>(), std::exception);
BOOST_CHECK_EQUAL
(
row
[
"f-4"
].
as
<
int
>
(),
0
);
BOOST_CHECK_EQUAL
(
row
[
"f-5"
].
as
<
int
>
(),
0
);
BOOST_CHECK_EQUAL
(
row
[
"f-6"
].
as
<
int
>
(),
0
);
}
BOOST_AUTO_TEST_CASE
(
item_1
)
{
using
namespace
cif
;
...
...
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