Guest User

Untitled

a guest
Nov 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <list>
  5. #include "Document.h"
  6.  
  7. std::istream &operator>>(std::istream &is, Document &d)
  8. {
  9. char ch = ' ';
  10. for ( ; is.get(ch);)
  11. {
  12. d.line.back().push_back(ch); // add the character
  13. }
  14. if (ch == '\n')
  15. {
  16. d.line.push_back(Line{}); // add another line
  17. }
  18. if (d.line.back().size())
  19. {
  20. d.line.push_back(Line{}); // add final empty line
  21. }
  22. return is;
  23. }
  24.  
  25. Document::Text_iterator &Document::Text_iterator::operator++()
  26. {
  27. ++pos; // proceed to the next character
  28. if (pos == (*ln).end())
  29. {
  30. ++ln; // proceed to the next line
  31. pos = (*ln).begin(); // bad if ln == line.end(); so make sure it isn't
  32. }
  33. return *this;
  34. }
  35.  
  36. Document::Text_iterator Document::Text_iterator::operator++(int n)
  37. {
  38. auto ret = *this;
  39. pos += n;
  40. if (pos == (*ln).end())
  41. {
  42. ++ln;
  43. pos = (*ln).begin();
  44. }
  45. ret = *this;
  46. return ret;
  47. }
  48.  
  49. Document::Text_iterator Document::Text_iterator::find_txt(Document::Text_iterator first, Document::Text_iterator last, const std::string &s)
  50. {
  51. if (s.size() == 0)
  52. {
  53. return last;
  54. }
  55.  
  56. char first_char = s[0];
  57. while (true)
  58. {
  59. auto p = std::find(first, last, first_char);
  60. if (p == last || match(p, last, s))
  61. {
  62. return p;
  63. }
  64. first = ++p;
  65. }
  66. }
  67.  
  68. bool Document::Text_iterator::match(Document::Text_iterator first, Document::Text_iterator last, const std::string &str)
  69. {
  70. if (str.empty())
  71. {
  72. return false;
  73. }
  74.  
  75. for (char i = 0, j = *first; i < str.length(), j != *last; ++i, ++j)
  76. {
  77. if (i != j)
  78. {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
Add Comment
Please, Sign In to add comment