Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <list>
- #include <iterator>
- #include <cassert>
- #include <algorithm>
- class Editor {
- public:
- // по умолчанию текста нет, курсор в нулевой позиции
- Editor() = default;
- // сдвинуть курсор влево
- void Left() {
- if (this->cursor_address_ == this->text_.begin()) {
- return;
- }
- std::list<char>::iterator begore_cursor_it = std::prev(this->cursor_address_);
- char begore_cursor_letter = *begore_cursor_it;
- this->text_.erase(begore_cursor_it);
- // вставка предшевствующего курсору элемента за курсором
- this->text_.insert(std::next(this->cursor_address_), begore_cursor_letter);
- }
- // сдвинуть курсор вправо
- void Right() {
- if (this->cursor_address_ == std::prev(this->text_.end())) {
- return;
- }
- std::list<char>::iterator after_cursor_it = std::next(this->cursor_address_);
- char after_cursor_letter = *after_cursor_it;
- this->text_.erase(after_cursor_it);
- // вставка элемента, который был после курсора перед ним
- this->text_.insert(this->cursor_address_, after_cursor_letter);
- }
- // вставить символ token
- void Insert(char token) {
- // при добавлении символа cursor_address смещается вправо на 1 позицию
- this->text_.insert(this->cursor_address_, token);
- }
- // вырезать не более tokens символов, начиная с текущей позиции курсора
- void Cut(size_t tokens = 1) {
- this->Copy(tokens);
- 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);
- this->text_.erase(std::next(this->cursor_address_), end_cut);
- }
- // cкопировать не более tokens символов, начиная с текущей позиции курсора
- void Copy(size_t tokens = 1) {
- this->buffer_.erase(this->buffer_.begin(), this->buffer_.end());
- std::list<char>::iterator end_copy;
- if (static_cast<size_t>(std::distance(this->cursor_address_, this->text_.end())) <= tokens) {
- end_copy = this->text_.end();
- this->buffer_.resize(std::distance(this->cursor_address_, this->text_.end()));
- } else {
- // начиная со следующего элемента за курсором
- end_copy = std::next(this->cursor_address_, ++tokens);
- this->buffer_.resize(tokens);
- }
- std::copy(std::next(this->cursor_address_), end_copy, this->buffer_.begin());
- }
- // вставить содержимое буфера в текущую позицию курсора
- void Paste() {
- this->text_.insert(this->cursor_address_, this->buffer_.begin(), std::prev(this->buffer_.end()));
- }
- // получить текущее содержимое текстового редактора
- std::string GetText() const {
- std::string content;
- for (const char& letter : this->text_) {
- if (letter != '|') {
- content += letter;
- }
- }
- return content;
- }
- private:
- std::list<char> text_ = { '|' }; // текст с курсором
- std::list<char>::iterator cursor_address_ = text_.begin();
- std::list<char> buffer_; // буфер вставки (без курсора)
- };
- int main() {
- using namespace std;
- Editor editor;
- const string text = "hello, world"s;
- for (char c : text) {
- editor.Insert(c);
- }
- // Текущее состояние редактора: `hello, world|`
- for (size_t i = 0; i < text.size(); ++i) {
- editor.Left();
- }
- // Текущее состояние редактора: `|hello, world`
- editor.Cut(7);
- // Текущее состояние редактора: `|world`
- // в буфере обмена находится текст `hello, `
- for (size_t i = 0; i < 5; ++i) {
- editor.Right();
- }
- // Текущее состояние редактора: `world|`
- editor.Insert(',');
- editor.Insert(' ');
- // Текущее состояние редактора: `world, |`
- editor.Paste();
- // Текущее состояние редактора: `world, hello, |`
- editor.Left();
- editor.Left();
- //Текущее состояние редактора: `world, hello|, `
- editor.Cut(3); // Будут вырезаны 2 символа
- // Текущее состояние редактора: `world, hello|`
- cout << editor.GetText();
- return 0;
- }
Add Comment
Please, Sign In to add comment