//helper function
std
::list<std
::pair<int, int> > get_positions
(QRegExp const &rx,
QString const &contents
){
int pos = 0;
std::list<std::pair<int, int> > result;
while((pos = rx.indexIn(contents, pos)) != -1)
{
//qDebug() << pos2 << ", "<< rx.matchedLength();
result.push_back(std::make_pair(pos, rx.matchedLength()));
pos += rx.matchedLength();
}
return result;
}
//helper function
template<typename T>
{
for(std::pair<int, int> const &data : position)
{
results << contents.mid(data.first, data.second);
}
return results;
}
//split the contents into the QStringList
void test_get_positions()
{
QString contents
= "lll ### ### ###";
typedef std::pair<int, int> DInt;
std
::list<DInt> positions
= get_positions
(QRegExp("#+"), contents
);
std
::list<DInt> positions2
= get_positions
(QRegExp("[^#]+"), contents
);
struct DIntCompare
{
bool operator()(DInt const &one, DInt const &two) const
{ return one.first < two.first; }
};
positions.merge(positions2, DIntCompare());
qDebug() << "---------------------";
for(DInt const &data : positions) qDebug() << data.first <<", "<<data.second;
QStringList results
= split_str_by_positions
(positions, contents
);
for(QString const &data
: results
) qDebug
() << data;
}
//helper function
std::list<std::pair<int, int> > get_positions(QRegExp const &rx, QString const &contents)
{
int pos = 0;
std::list<std::pair<int, int> > result;
while((pos = rx.indexIn(contents, pos)) != -1)
{
//qDebug() << pos2 << ", "<< rx.matchedLength();
result.push_back(std::make_pair(pos, rx.matchedLength()));
pos += rx.matchedLength();
}
return result;
}
//helper function
template<typename T>
QStringList const split_str_by_positions(T const &position, QString const &contents)
{
QStringList results;
for(std::pair<int, int> const &data : position)
{
results << contents.mid(data.first, data.second);
}
return results;
}
//split the contents into the QStringList
void test_get_positions()
{
QString contents = "lll ### ### ###";
typedef std::pair<int, int> DInt;
std::list<DInt> positions = get_positions(QRegExp("#+"), contents);
std::list<DInt> positions2 = get_positions(QRegExp("[^#]+"), contents);
struct DIntCompare
{
bool operator()(DInt const &one, DInt const &two) const
{ return one.first < two.first; }
};
positions.merge(positions2, DIntCompare());
qDebug() << "---------------------";
for(DInt const &data : positions) qDebug() << data.first <<", "<<data.second;
QStringList results = split_str_by_positions(positions, contents);
for(QString const &data : results) qDebug() << data;
}
To copy to clipboard, switch view to plain text mode
Bookmarks