Seredenko-V

Текстовый редактор

Jul 14th, 2022 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <iterator>
  5. #include <cassert>
  6. #include <algorithm>
  7.  
  8. class Editor {
  9. public:
  10.     // по умолчанию текста нет, курсор в нулевой позиции
  11.     Editor() = default;
  12.  
  13.     // сдвинуть курсор влево
  14.     void Left() {
  15.         if (this->cursor_address_ == this->text_.begin()) {
  16.             return;
  17.         }
  18.         std::list<char>::iterator begore_cursor_it = std::prev(this->cursor_address_);
  19.         char begore_cursor_letter = *begore_cursor_it;
  20.         this->text_.erase(begore_cursor_it);
  21.         // вставка предшевствующего курсору элемента за курсором
  22.         this->text_.insert(std::next(this->cursor_address_), begore_cursor_letter);
  23.     }
  24.  
  25.     // сдвинуть курсор вправо
  26.     void Right() {
  27.         if (this->cursor_address_ == std::prev(this->text_.end())) {
  28.             return;
  29.         }
  30.         std::list<char>::iterator after_cursor_it = std::next(this->cursor_address_);
  31.         char after_cursor_letter = *after_cursor_it;
  32.         this->text_.erase(after_cursor_it);
  33.         // вставка элемента, который был после курсора перед ним
  34.         this->text_.insert(this->cursor_address_, after_cursor_letter);
  35.     }
  36.  
  37.     // вставить символ token
  38.     void Insert(char token) {
  39.         // при добавлении символа cursor_address смещается вправо на 1 позицию
  40.         this->text_.insert(this->cursor_address_, token);
  41.     }
  42.  
  43.     // вырезать не более tokens символов, начиная с текущей позиции курсора
  44.     void Cut(size_t tokens = 1) {
  45.         this->Copy(tokens);
  46.         std::list<char>::iterator end_cut = static_cast<size_t>(std::distance(this->cursor_address_, this->text_.end())) <= tokens ? this->text_.end() : std::next(this->cursor_address_, ++tokens);
  47.         this->text_.erase(std::next(this->cursor_address_), end_cut);
  48.     }
  49.  
  50.     // cкопировать не более tokens символов, начиная с текущей позиции курсора
  51.     void Copy(size_t tokens = 1) {
  52.         this->buffer_.erase(this->buffer_.begin(), this->buffer_.end());
  53.         std::list<char>::iterator end_copy;
  54.  
  55.         if (static_cast<size_t>(std::distance(this->cursor_address_, this->text_.end())) <= tokens) {
  56.             end_copy = this->text_.end();
  57.             this->buffer_.resize(std::distance(this->cursor_address_, this->text_.end()));
  58.         } else {
  59.             // начиная со следующего элемента за курсором
  60.             end_copy = std::next(this->cursor_address_, ++tokens);
  61.             this->buffer_.resize(tokens);
  62.         }
  63.  
  64.         std::copy(std::next(this->cursor_address_), end_copy, this->buffer_.begin());
  65.     }
  66.  
  67.     // вставить содержимое буфера в текущую позицию курсора
  68.     void Paste() {
  69.         this->text_.insert(this->cursor_address_, this->buffer_.begin(), std::prev(this->buffer_.end()));
  70.     }
  71.  
  72.     // получить текущее содержимое текстового редактора
  73.     std::string GetText() const {
  74.         std::string content;
  75.         for (const char& letter : this->text_) {
  76.             if (letter != '|') {
  77.                 content += letter;
  78.             }
  79.         }
  80.         return content;
  81.     }
  82.    
  83. private:
  84.     std::list<char> text_ = { '|' }; // текст с курсором
  85.     std::list<char>::iterator cursor_address_ = text_.begin();
  86.     std::list<char> buffer_; // буфер вставки (без курсора)
  87. };
  88.  
  89. int main() {
  90.     using namespace std;
  91.     Editor editor;
  92.     const string text = "hello, world"s;
  93.     for (char c : text) {
  94.         editor.Insert(c);
  95.     }
  96.     // Текущее состояние редактора: `hello, world|`
  97.     for (size_t i = 0; i < text.size(); ++i) {
  98.         editor.Left();
  99.     }
  100.     // Текущее состояние редактора: `|hello, world`
  101.     editor.Cut(7);
  102.     // Текущее состояние редактора: `|world`
  103.     // в буфере обмена находится текст `hello, `
  104.     for (size_t i = 0; i < 5; ++i) {
  105.         editor.Right();
  106.     }
  107.     // Текущее состояние редактора: `world|`
  108.     editor.Insert(',');
  109.     editor.Insert(' ');
  110.     // Текущее состояние редактора: `world, |`
  111.     editor.Paste();
  112.     // Текущее состояние редактора: `world, hello, |`
  113.     editor.Left();
  114.     editor.Left();
  115.     //Текущее состояние редактора: `world, hello|, `
  116.     editor.Cut(3);  // Будут вырезаны 2 символа
  117.     // Текущее состояние редактора: `world, hello|`
  118.     cout << editor.GetText();
  119.     return 0;
  120. }
Add Comment
Please, Sign In to add comment