#include <algorithm>
#include "textprocessor.h"
using namespace std;
Textprocessor::TSortSearch::TSortSearch(istream& is)
{ is >> *this; }
void Textprocessor::TSortSearch::sortlines(unsigned n)
{ sort(lines.begin(), lines.end(),
[n](const string& a, const string& b)
{return a.length() > n && b.length() > n ?
a.compare(n, string::npos, b, n, string::npos) < 0 :
(a.length() <= n && b.length() > n ? true : false);
});
}
void Textprocessor::TSortSearch::searchlines (unsigned n, const string& s, ostream& output) const
{
for_each(lines.begin(), lines.end(),
[&](const string& a)
{if (a.length() > n && !a.compare(n, s.length(), s)) output << a << endl;} );
}
istream& Textprocessor::operator>>(istream& in, TSortSearch& ss)
{
string s;
ss.lines.clear(); while (getline(in,s))
ss.lines.push_back(move(s)); return in;
}
ostream& Textprocessor::operator<<(ostream& out, const TSortSearch& ss)
{
for (auto &s : ss.lines)
out << s << endl;
return out;
}