Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ottimizzare parsing linee con regex

2 views
Skip to first unread message

wiso

unread,
Nov 28, 2009, 12:15:16 PM11/28/09
to
vorrei ottimizzare un po' questa funzione:

struct data
{
int month, day, hour, minute, second, number;
float Vfinal, HV, I;
};

data parse_line(std::string line)
{
data d;
static const boost::regex re("([A-Za-z]{3}) +(\\d+) +(\\d{2}):(\\d{2}):
(\\d{2}).*: +ch +(\\d{1,2}).*?(\\d+).*?([0-9\\.]+).*?([0-9\\.]+).*?");
static std::map<std::string,int> month_name;
month_name["Jan"] = 1;
month_name["Feb"] = 2;
month_name["Mar"] = 3;
month_name["Apr"] = 4;
month_name["May"] = 5;
month_name["Jun"] = 6;
month_name["Jul"] = 7;
month_name["Aug"] = 8;
month_name["Sep"] = 9;
month_name["Oct"] = 10;
month_name["Nov"] = 11;
month_name["Dec"] = 12;
boost::cmatch matches;
if (boost::regex_match(line.c_str(), matches, re))
{
d.month = month_name[std::string(matches[1].first, matches[1].second)];
d.day = atoi(string(matches[2].first, matches[2].second).c_str());
d.hour = atoi(string(matches[3].first, matches[3].second).c_str());
d.minute = atoi(string(matches[4].first, matches[4].second).c_str());
d.second = atoi(string(matches[5].first, matches[5].second).c_str());
d.number = atoi(string(matches[6].first, matches[6].second).c_str());
d.Vfinal = atof(string(matches[7].first, matches[7].second).c_str());
d.HV = atof(string(matches[8].first, matches[8].second).c_str());
d.I = atof(string(matches[9].first, matches[9].second).c_str());
}
else
{
// ...
}
return d;
}

In particolare non mi piace la parte di conversione dei tipi.

Kap

unread,
May 17, 2010, 5:31:58 PM5/17/10
to

e perch� mai? mi pare sia la cosa pi� veloce e sana e atoi dovrebbe
essere standard..

0 new messages