James Kanze wrote:
Quote:
On Jun 25, 2:30 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote:
Quote:
> bool TimeParser(const std::string& time, TIMESTRUCT& st)
> {
> cmatch m;
> if(regex_match(time.c_str(), m, regex("^(\\d{2}):(\\d{2})\\.(\\d{2})$"))) {
> st.Hour = atoi(m[1].first);
> st.Minute = atoi(m[2].first);
> st.Second = atoi(m[3].first);
> return true;
> }
> return false;
> }
>
Two small nits: first, regex_match can take iterators, so you
can just write:
if ( regex_match( time.begin(), time.end(), m, expr ) ) ...
This would require the smatch overloaded 'regex_match',
which returns std::string objects in matches and does,
in this case, also provide a version that uses the
plain std::string:
bool TimeParser(const std::string& time, TIMESTRUCT& st)
{
using namespace boost;
smatch m;
static regex r("^(\\d{2}):(\\d{2})\\.(\\d{2})$");
if(regex_match(time, m, r)) {
st.Hour = atoi(m[1].str().c_str());
st.Minute = atoi(m[2].str().c_str());
st.Second = atoi(m[3].str().c_str());
return true;
}
return false;
}
The use of the smatch-overloaded regex_... requires
an additional step to extract the resulting values,
compare it to the original version:
bool TimeParser(const std::string& time, TIMESTRUCT& st)
{
using namespace boost;
cmatch m;
if(regex_match(time.c_str(), m, regex("^(\\d{2}):(\\d{2})\\.(\\d{2})$"))) {
st.Hour = atoi(m[1].first);
st.Minute = atoi(m[2].first);
st.Second = atoi(m[3].first);
return true;
}
return false;
}
Which looks (imho) less cluttered.
Quote:
And since the expression is a constant, that's how I'd write it:
static regex const expr( "^(\\d{2}):(\\d{2})\\.(\\d{2})$" ) ;
(at the start of the function, before the if).
This would, of course, be better from a technical point of view
but it costs one additional line ;-) But if you are paid for
LOCaday ...
Regards & thanks
M.