diff options
Diffstat (limited to 'src/Document.cxx')
-rw-r--r-- | src/Document.cxx | 33 |
1 files changed, 18 insertions, 15 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 { |