Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cdo
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mpim-sw
cdo
Commits
12fa6fcb
Commit
12fa6fcb
authored
2 months ago
by
Oliver Heidmann
Committed by
Uwe Schulzweida
2 months ago
Browse files
Options
Downloads
Patches
Plain Diff
removed mandatory double cast and added static type restrictions
parent
95ff1026
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!326
convert_to_number not called string_to_number, fixed usage of .fail enabling...
,
!322
Fixing and cleaning up handling of operator argument sourrounding the use of scientific notations
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/util_string.h
+7
-12
7 additions, 12 deletions
src/util_string.h
with
7 additions
and
12 deletions
src/util_string.h
+
7
−
12
View file @
12fa6fcb
...
@@ -14,6 +14,8 @@
...
@@ -14,6 +14,8 @@
#include
<memory>
#include
<memory>
#include
<stdexcept>
#include
<stdexcept>
#include
<algorithm>
#include
<algorithm>
#include
<sstream>
#include
<concepts>
#define ADD_PLURAL(n) ((n) != 1 ? "s" : "")
#define ADD_PLURAL(n) ((n) != 1 ? "s" : "")
...
@@ -61,19 +63,14 @@ string_contains(const std::string &s, unsigned char ch)
...
@@ -61,19 +63,14 @@ string_contains(const std::string &s, unsigned char ch)
return
(
s
.
find
(
ch
)
!=
std
::
string
::
npos
);
return
(
s
.
find
(
ch
)
!=
std
::
string
::
npos
);
}
}
/** Tokenizes a string with delimiter ',' and returns a touple where first
* signals success and the second the result.
* If any of the tokens is not a integer the result is negative.
* With a negative result a empty
* list is returned with the result boolean.
**/
#include
<sstream>
template
<
typename
T
>
template
<
typename
T
>
std
::
pair
<
bool
,
T
>
std
::
pair
<
bool
,
T
>
convert_to_number
(
const
std
::
string
&
str
)
convert_to_number
(
const
std
::
string
&
str
)
{
{
static_assert
(
std
::
is_same
<
T
,
double
>::
value
||
std
::
is_same
<
T
,
float
>::
value
,
"tokenize_comma_seperated_number_list can only produce float or double lists"
);
std
::
stringstream
ss
(
str
);
std
::
stringstream
ss
(
str
);
double
number
=
0
;
T
number
=
0
;
ss
>>
number
;
ss
>>
number
;
if
(
ss
.
fail
())
{
return
{
false
,
0
};
}
if
(
ss
.
fail
())
{
return
{
false
,
0
};
}
...
@@ -81,20 +78,18 @@ convert_to_number(const std::string &str)
...
@@ -81,20 +78,18 @@ convert_to_number(const std::string &str)
return
{
true
,
number
};
return
{
true
,
number
};
}
}
/* This function seperates a string into multiple tokens converted to T
* CAUTION if T=int and scientific notation is used a 1.01e30 would be converted
* to a simple 1 */
template
<
typename
T
>
template
<
typename
T
>
std
::
pair
<
bool
,
std
::
vector
<
T
>>
std
::
pair
<
bool
,
std
::
vector
<
T
>>
tokenize_comma_seperated_number_list
(
const
std
::
string
&
args
)
tokenize_comma_seperated_number_list
(
const
std
::
string
&
args
)
{
{
static_assert
(
std
::
is_same
<
T
,
double
>::
value
||
std
::
is_same
<
T
,
float
>::
value
,
"tokenize_comma_seperated_number_list can only produce float or double lists"
);
std
::
stringstream
ss
(
args
);
std
::
stringstream
ss
(
args
);
std
::
string
token
;
std
::
string
token
;
std
::
vector
<
T
>
numbers
;
std
::
vector
<
T
>
numbers
;
while
(
std
::
getline
(
ss
,
token
,
','
))
while
(
std
::
getline
(
ss
,
token
,
','
))
{
{
std
::
pair
<
bool
,
T
>
res
=
convert_to_number
<
T
>
(
token
);
std
::
pair
<
bool
,
T
>
res
=
convert_to_number
<
T
>
(
token
);
std
::
cout
<<
token
<<
" "
<<
res
.
first
<<
" "
<<
res
.
second
<<
std
::
endl
;
if
(
!
res
.
first
)
{
return
{
false
,
{}
};
}
if
(
!
res
.
first
)
{
return
{
false
,
{}
};
}
numbers
.
push_back
(
res
.
second
);
numbers
.
push_back
(
res
.
second
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment