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
b71d96a6
Commit
b71d96a6
authored
3 months ago
by
Oliver Heidmann
Committed by
Uwe Schulzweida
2 months ago
Browse files
Options
Downloads
Patches
Plain Diff
added tokenizer from string to T in util string
parent
7bd5e97d
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
+46
-0
46 additions, 0 deletions
src/util_string.h
with
46 additions
and
0 deletions
src/util_string.h
+
46
−
0
View file @
b71d96a6
...
...
@@ -61,6 +61,52 @@ string_contains(const std::string &s, unsigned char ch)
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
>
std
::
pair
<
bool
,
T
>
convert_to_number
(
const
std
::
string
&
str
)
{
std
::
stringstream
ss
(
str
);
T
number
=
0
;
ss
>>
number
;
if
(
ss
.
fail
())
{
return
{
false
,
0
};
}
return
{
true
,
number
};
}
template
<
typename
T
>
std
::
pair
<
bool
,
std
::
vector
<
T
>>
tokenize_comma_seperated_number_list
(
const
std
::
string
&
args
)
{
auto
tokens
=
split_with_seperator
(
args
,
','
);
std
::
vector
<
T
>
numbers
;
bool
retval
=
true
;
for
(
const
auto
&
t
:
tokens
)
{
std
::
pair
<
bool
,
T
>
res
=
convert_to_number
<
T
>
(
t
);
if
(
!
res
.
first
)
{
retval
=
false
;
tokens
=
{};
break
;
}
numbers
.
push_back
(
res
.
second
);
}
return
std
::
make_pair
(
retval
,
numbers
);
}
namespace
Util
{
namespace
String
...
...
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