Replace the buffer with the text
| 712 | |
| 713 | // Replace the buffer with the text |
| 714 | void ZepBuffer::SetText(const std::string& text, bool initFromFile) |
| 715 | { |
| 716 | // First, clear it |
| 717 | Clear(); |
| 718 | |
| 719 | bool lastWasSpace = false; |
| 720 | if (!text.empty()) |
| 721 | { |
| 722 | // Since incremental insertion of a big file into a gap buffer gives us worst case performance, |
| 723 | // We build the buffer in a separate array and assign it. Much faster. |
| 724 | std::vector<uint8_t> input; |
| 725 | |
| 726 | m_lineEnds.clear(); |
| 727 | |
| 728 | // Update the gap buffer with the text |
| 729 | // We remove \r, we only care about \n |
| 730 | for (auto& ch : text) |
| 731 | { |
| 732 | if (ch == '\r') |
| 733 | { |
| 734 | m_fileFlags |= FileFlags::StrippedCR; |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | input.push_back(ch); |
| 739 | if (ch == '\n') |
| 740 | { |
| 741 | m_lineEnds.push_back(ByteIndex(input.size())); |
| 742 | lastWasSpace = false; |
| 743 | } |
| 744 | else if (ch == '\t') |
| 745 | { |
| 746 | m_fileFlags |= FileFlags::HasTabs; |
| 747 | lastWasSpace = false; |
| 748 | } |
| 749 | else if (ch == ' ') |
| 750 | { |
| 751 | if (lastWasSpace) |
| 752 | { |
| 753 | m_fileFlags |= FileFlags::HasSpaceTabs; |
| 754 | } |
| 755 | lastWasSpace = true; |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | lastWasSpace = false; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | m_workingBuffer.assign(input.begin(), input.end()); |
| 764 | } |
| 765 | |
| 766 | // If file is only tabs, then force tab mode |
| 767 | if (HasFileFlags(FileFlags::HasTabs) && !HasFileFlags(FileFlags::HasSpaceTabs)) |
| 768 | { |
| 769 | m_fileFlags = ZSetFlags(m_fileFlags, FileFlags::InsertTabs); |
| 770 | } |
| 771 |