Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <list>
- #include "Document.h"
- std::istream &operator>>(std::istream &is, Document &d)
- {
- char ch = ' ';
- for ( ; is.get(ch);)
- {
- d.line.back().push_back(ch); // add the character
- }
- if (ch == '\n')
- {
- d.line.push_back(Line{}); // add another line
- }
- if (d.line.back().size())
- {
- d.line.push_back(Line{}); // add final empty line
- }
- return is;
- }
- Document::Text_iterator &Document::Text_iterator::operator++()
- {
- ++pos; // proceed to the next character
- if (pos == (*ln).end())
- {
- ++ln; // proceed to the next line
- pos = (*ln).begin(); // bad if ln == line.end(); so make sure it isn't
- }
- return *this;
- }
- Document::Text_iterator Document::Text_iterator::operator++(int n)
- {
- auto ret = *this;
- pos += n;
- if (pos == (*ln).end())
- {
- ++ln;
- pos = (*ln).begin();
- }
- ret = *this;
- return ret;
- }
- Document::Text_iterator Document::Text_iterator::find_txt(Document::Text_iterator first, Document::Text_iterator last, const std::string &s)
- {
- if (s.size() == 0)
- {
- return last;
- }
- char first_char = s[0];
- while (true)
- {
- auto p = std::find(first, last, first_char);
- if (p == last || match(p, last, s))
- {
- return p;
- }
- first = ++p;
- }
- }
- bool Document::Text_iterator::match(Document::Text_iterator first, Document::Text_iterator last, const std::string &str)
- {
- if (str.empty())
- {
- return false;
- }
- for (char i = 0, j = *first; i < str.length(), j != *last; ++i, ++j)
- {
- if (i != j)
- {
- return false;
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment