diff options
-rw-r--r-- | src/Document.cxx | 33 | ||||
-rw-r--r-- | src/Editor.cxx | 12 |
2 files changed, 21 insertions, 24 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index b7b5b2427..3ae8964ba 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -1716,20 +1716,29 @@ void Document::Indent(bool forwards, Sci::Line lineBottom, Sci::Line lineTop) { } } +namespace { + +constexpr std::string_view EOLForMode(EndOfLine eolMode) noexcept { + switch (eolMode) { + case EndOfLine::CrLf: + return "\r\n"; + case EndOfLine::Cr: + return "\r"; + default: + return "\n"; + } +} + +} + // Convert line endings for a piece of text to a particular mode. // Stop at len or when a NUL is found. std::string Document::TransformLineEnds(const char *s, size_t len, EndOfLine eolModeWanted) { std::string dest; + const std::string_view eol = EOLForMode(eolModeWanted); for (size_t i = 0; (i < len) && (s[i]); i++) { if (s[i] == '\n' || s[i] == '\r') { - if (eolModeWanted == EndOfLine::Cr) { - dest.push_back('\r'); - } else if (eolModeWanted == EndOfLine::Lf) { - dest.push_back('\n'); - } else { // eolModeWanted == EndOfLine::CrLf - dest.push_back('\r'); - dest.push_back('\n'); - } + dest.append(eol); if ((s[i] == '\r') && (i+1 < len) && (s[i+1] == '\n')) { i++; } @@ -1780,13 +1789,7 @@ void Document::ConvertLineEnds(EndOfLine eolModeSet) { } std::string_view Document::EOLString() const noexcept { - if (eolMode == EndOfLine::CrLf) { - return "\r\n"; - } else if (eolMode == EndOfLine::Cr) { - return "\r"; - } else { - return "\n"; - } + return EOLForMode(eolMode); } DocumentOption Document::Options() const noexcept { diff --git a/src/Editor.cxx b/src/Editor.cxx index e39ab622c..681c0914a 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -4342,10 +4342,7 @@ bool Editor::CopyLineRange(SelectionText *ss, bool allowProtected) { if (allowProtected || !RangeContainsProtected(start, end)) { std::string text = RangeText(start, end); - if (pdoc->eolMode != EndOfLine::Lf) - text.push_back('\r'); - if (pdoc->eolMode != EndOfLine::Cr) - text.push_back('\n'); + text.append(pdoc->EOLString()); ss->Copy(text, pdoc->dbcsCodePage, vs.styles[StyleDefault].characterSet, false, true); return true; @@ -4365,12 +4362,9 @@ void Editor::CopySelectionRange(SelectionText *ss, bool allowLineCopy) { if (sel.selType == Selection::SelTypes::rectangle) std::sort(rangesInOrder.begin(), rangesInOrder.end()); for (const SelectionRange ¤t : rangesInOrder) { - text.append(RangeText(current.Start().Position(), current.End().Position())); + text.append(RangeText(current.Start().Position(), current.End().Position())); if (sel.selType == Selection::SelTypes::rectangle) { - if (pdoc->eolMode != EndOfLine::Lf) - text.push_back('\r'); - if (pdoc->eolMode != EndOfLine::Cr) - text.push_back('\n'); + text.append(pdoc->EOLString()); } } ss->Copy(text, pdoc->dbcsCodePage, |