From df3bf6ecc2ab7a0b149e6451ccd1dbce2be02164 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 16 Nov 2010 19:43:56 +0300 Subject: [PATCH] use most common line separator, not the one from the last line (PY-2249) --- .../openapi/fileEditor/impl/LoadTextUtil.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java index aba7340be56d..800d857bd1f0 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java @@ -42,27 +42,28 @@ public final class LoadTextUtil { } private static Pair convertLineSeparators(final CharBuffer buffer) { - final int LF = 1; - final int CR = 2; - int line_separator = 0; - int dst = 0; char prev = ' '; + int crCount = 0; + int lfCount = 0; + int crlfCount = 0; + final int length = buffer.length(); for (int src = 0; src < length; src++) { char c = buffer.charAt(src); switch (c) { case '\r': buffer.put(dst++, '\n'); - line_separator = CR; + crCount++; break; case '\n': if (prev == '\r') { - line_separator = CR + LF; + crCount--; + crlfCount++; } else { buffer.put(dst++, '\n'); - line_separator = LF; + lfCount++; } break; default: @@ -73,16 +74,14 @@ public final class LoadTextUtil { } String detectedLineSeparator = null; - switch (line_separator) { - case CR: - detectedLineSeparator = "\r"; - break; - case LF: - detectedLineSeparator = "\n"; - break; - case CR + LF: - detectedLineSeparator = "\r\n"; - break; + if (crlfCount > crCount && crlfCount > lfCount) { + detectedLineSeparator = "\r\n"; + } + else if (crCount > lfCount) { + detectedLineSeparator = "\r"; + } + else if (lfCount > 0) { + detectedLineSeparator = "\n"; } CharSequence result;