Replace the buffer with the text
| 725 | |
| 726 | // Replace the buffer with the text |
| 727 | void ZepBuffer::SetText(const std::string& text, bool initFromFile) { |
| 728 | // First, clear it |
| 729 | Clear(); |
| 730 | |
| 731 | bool lastWasSpace = false; |
| 732 | if (!text.empty()) { |
| 733 | // Since incremental insertion of a big file into a gap buffer gives us worst case performance, |
| 734 | // We build the buffer in a separate array and assign it. Much faster. |
| 735 | std::vector<uint8_t> input; |
| 736 | |
| 737 | m_lineEnds.clear(); |
| 738 | |
| 739 | // Update the gap buffer with the text |
| 740 | // We remove \r, we only care about \n |
| 741 | for (auto& ch : text) { |
| 742 | if (ch == '\r') { |
| 743 | m_fileFlags |= FileFlags::StrippedCR; |
| 744 | } else { |
| 745 | input.push_back(ch); |
| 746 | if (ch == '\n') { |
| 747 | m_lineEnds.push_back(ByteIndex(input.size())); |
| 748 | lastWasSpace = false; |
| 749 | } else if (ch == '\t') { |
| 750 | m_fileFlags |= FileFlags::HasTabs; |
| 751 | lastWasSpace = false; |
| 752 | } else if (ch == ' ') { |
| 753 | if (lastWasSpace) { |
| 754 | m_fileFlags |= FileFlags::HasSpaceTabs; |
| 755 | } |
| 756 | lastWasSpace = true; |
| 757 | } else { |
| 758 | lastWasSpace = false; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | m_workingBuffer.assign(input.begin(), input.end()); |
| 763 | } |
| 764 | |
| 765 | // If file is only tabs, then force tab mode |
| 766 | if (HasFileFlags(FileFlags::HasTabs) && !HasFileFlags(FileFlags::HasSpaceTabs)) { |
| 767 | m_fileFlags = ZSetFlags(m_fileFlags, FileFlags::InsertTabs); |
| 768 | } |
| 769 | |
| 770 | if (m_workingBuffer[m_workingBuffer.size() - 1] != 0) { |
| 771 | m_fileFlags |= FileFlags::TerminatedWithZero; |
| 772 | m_workingBuffer.push_back(0); |
| 773 | } |
| 774 | |
| 775 | // TODO: Why is a line end needed always? |
| 776 | // TODO: Line ends 1 beyond, or just for end? Can't remember this detail: |
| 777 | // understand it, then write a unit test to ensure it. |
| 778 | m_lineEnds.push_back(End().Index() + 1); |
| 779 | |
| 780 | MarkUpdate(); |
| 781 | |
| 782 | // When loading a file, send the Loaded message to distinguish it from adding to a buffer, and remember that the buffer is not dirty in this case |
| 783 | if (initFromFile) { |
| 784 | GetEditor().Broadcast(std::make_shared<BufferMessage>(this, BufferMessageType::Loaded, Begin(), End())); |
no test coverage detected