How to split a string to two other variable? (for time parsing) #11786
Answered
by
eli-schwartz
mobin-2008
asked this question in
Q&A
-
Hi. I don't think it's a good place for my question but seems like it's.
Also See my example in C++: #include <iostream>
#include <limits>
#include <string>
// Parse a time, specified as a decimal number of seconds (with optional fractional component after decimal
// point or decimal comma).
int parse_time(const std::string ¶mval, const char * paramname, bool nano)
{
unsigned int isec = 0;
unsigned int insec = 0;
auto max_secs = std::numeric_limits<decltype(isec)>::max() / 10; // I think it's can't implemented in meson
auto len = paramval.length();
decltype(len) i;
for (i = 0; i < len; i++) {
char ch = paramval[i];
if (ch == '.' || ch == ',') {
i++;
break;
}
if (ch < '0' || ch > '9') {
std::cerr << "mconfig-gen: " << std::string("bad value for ") + paramname << "\n";
exit(1);
}
// check for overflow
if (isec >= max_secs) {
std::cerr << "mconfig-gen: " << std::string("too-large value for ") + paramname << "\n";
exit(1);
}
isec *= 10;
isec += ch - '0';
}
decltype(insec) insec_m = 100000000; // 10^8
for ( ; i < len; i++) {
char ch = paramval[i];
if (ch < '0' || ch > '9') {
std::cerr << "mconfig-gen: " << std::string("bad value for ") + paramname << "\n";
exit(1);
}
insec += (ch - '0') * insec_m;
insec_m /= 10;
}
if (!nano) return isec;
return insec;
} I didn't find any function to helps me to do that. I need to add external script for do that? Regards |
Beta Was this translation helpful? Give feedback.
Answered by
eli-schwartz
May 15, 2023
Replies: 1 comment 7 replies
-
I guess what you want here is string splitting? https://mesonbuild.com/Syntax.html#split-join |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Objects are immutable, the split method doesn't operate on "a" and turn "a" into an array -- the split method returns a value that needs to be assigned.