C++ (Qt)int x, y;QString phone, mail;static const QString prefixX = "+X";static const QString prefixY = "-Y";static const QString str_Phone = "Phone";static const QString str_Mail = "Mail"; try { ExpectWord(src, prefixX); x = ExpectInt(src); ExpectWord(src, prefixY); y = ExpectInt(src); ExpectWord(src, str_Phone); phone = NextWord(src); ExpectWord(src, str_Mail); mail = NextWord(src);}catch (...) {..}
if (!str.startsWith(prefixY)) { qDebug() << "1 ;("; return false; } int n = prefixY.size(); if (str[n] != space) { qDebug() << "2 ;("; return false; } // Очень порадовала проверка : ) if (++n == str.size()) { qDebug() << "3 ;("; return false; } int m = str.indexOf(space, n); if (m == -1) { qDebug() << "4 ;("; return false; }
if (!line.startsWith("-Y ")) continue;
bool ok = false; y = str.mid(n, m-n).toInt(&ok); if (!ok) { qDebug() << "5 ;("; return false; }
if (str[m] != space) { qDebug() << "6 ;("; return false; } n = str.indexOf(prefixX, ++m); if (n != m) { qDebug() << "7 ;("; return false; } n += prefixX.size(); if (n == str.size()) { qDebug() << "8 ;("; return false; }
// Handle error. if ((posX = line.indexOf(" +X ")) == -1) continue;
C++ (Qt)bool super_parser( int &x, int &y, string &phone, string &email );
Bashtest Veres Manner, time (ms) : 1830 test QRegEx Manner, time (ms) : 4501 test XPressive Manner, time (ms) : 1246 ------------------------------------------------------------------------ test Veres Manner, time (ms) : 1715 test QRegEx Manner, time (ms) : 4512 test XPressive Manner, time (ms) : 1263 ------------------------------------------------------------------------ test Veres Manner, time (ms) : 1819 test QRegEx Manner, time (ms) : 4504 test XPressive Manner, time (ms) : 1298
C++ (Qt)#include <iostream>#include <fstream>#include <sstream>#include <chrono>#include <list> #include <boost/xpressive/xpressive.hpp> #include <QString>#include <QDebug>#include <QFile>#include <QTextStream>#include <QVector>#include <QPoint>#include <QRegExp>#include <QList> bool extractData(const QString & str, int & x, int & y){ static const QString prefixX = "+X"; static const QString prefixY = "-Y"; static const QChar space = ' '; if (!str.startsWith(prefixY)) { qDebug() << "1 ;("; return false; } int n = prefixY.size(); if (str[n] != space) { qDebug() << "2 ;("; return false; } if (++n == str.size()) { qDebug() << "3 ;("; return false; } int m = str.indexOf(space, n); if (m == -1) { qDebug() << "4 ;("; return false; } bool ok = false; y = str.mid(n, m-n).toInt(&ok); if (!ok) { qDebug() << "5 ;("; return false; } if (str[m] != space) { qDebug() << "6 ;("; return false; } n = str.indexOf(prefixX, ++m); if (n != m) { qDebug() << "7 ;("; return false; } n += prefixX.size(); if (n == str.size()) { qDebug() << "8 ;("; return false; } x = str.mid(++n, str.size()).toInt(&ok); if (!ok) { qDebug() << "9 ;("; return false; } return true; } template <class Container>void readData_VeresManner(const QString & fileName, Container & v){ QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Error opening file"; return; } QTextStream stream(&file); while (!stream.atEnd()) { QString str = stream.readLine(); int x, y; if (extractData(str, x, y)) { v.append(QPoint(x, y)); } }} template <class Container>void readData_QRegExManner(const QString & fileName, Container & v){ QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Error opening file"; return; } QTextStream stream(&file); QString str = stream.readAll(); QRegExp regex("\\-Y (\\d+) \\+X (\\d+)"); regex.setMinimal(true); int pos = 0; while ((pos = regex.indexIn(str, pos)) != -1) { int y = regex.cap(1).toInt(); int x = regex.cap(2).toInt(); v.append(QPoint(x, y)); pos += regex.matchedLength(); } } template <class Container>void readData_XPressiveManner(const char * fileName, Container & v){ using namespace boost::xpressive; std::ifstream in(fileName); if (!in.is_open()) { qDebug() << "Error opening file"; return; } std::ostringstream oss; oss << in.rdbuf(); std::string buff = oss.str(); sregex expr = as_xpr("-Y") >> +space >> (s1 = +_d) >> +space >> as_xpr("+X") >> +space >> (s2 = +_d); sregex_iterator it(buff.cbegin(), buff.cend(), expr); sregex_iterator end; for (; it != end; ++it) { int x = std::stoi((*it)[2]); int y = std::stoi((*it)[1]); v.push_back(std::pair<int, int>(x, y)); }} void generateData(const QString & fileName, int size){ QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) { qDebug() << "Error opening file"; return; } QTextStream stream(&file); static const QString prefixX = "+X"; static const QString prefixY = "-Y"; static const QChar space = ' '; static const int max = 10000; for (int i = 0; i < size; ++i) { int x = qrand() % max; int y = qrand() % max; stream << prefixY << space << y << space << prefixX << space << x << endl; }} int main(){ static const int dataSize = 1000000; QString fileName = "test_data.txt"; generateData(fileName, dataSize); QList<QPoint> v1; QList<QPoint> v2; auto start = std::chrono::high_resolution_clock::now(); readData_VeresManner(fileName, v1); auto stop = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count(); qDebug() << "test Veres Manner, time (ms) : " << duration; qDebug() << v1.size(); start = std::chrono::high_resolution_clock::now(); readData_QRegExManner(fileName, v2); stop = std::chrono::high_resolution_clock::now(); duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count(); qDebug() << "test QRegEx Manner, time (ms) : " << duration; qDebug() << v2.size(); std::list<std::pair<int, int>> v3; start = std::chrono::high_resolution_clock::now(); readData_XPressiveManner(fileName.toStdString().c_str(), v3); stop = std::chrono::high_resolution_clock::now(); duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count(); qDebug() << "test XPressive Manner, time (ms) : " << duration; qDebug() << v3.size(); return 0;}