From 482c5ba7f1d711760248cda04fd446c8d075222c Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 25 May 2016 16:11:15 +0300 Subject: [PATCH] take care of surrogates in encoding-independent way --- .../LossyEncodingInspection.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInspection/LossyEncodingInspection.java b/platform/lang-impl/src/com/intellij/codeInspection/LossyEncodingInspection.java index 3270612c3a82..3666be983e23 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/LossyEncodingInspection.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/LossyEncodingInspection.java @@ -14,12 +14,6 @@ * limitations under the License. */ -/* - * Created by IntelliJ IDEA. - * User: cdr - * Date: Aug 6, 2007 - * Time: 3:09:55 PM - */ package com.intellij.codeInspection; import com.intellij.ide.DataManager; @@ -38,7 +32,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.ListPopup; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.encoding.ChangeFileEncodingAction; import com.intellij.openapi.vfs.encoding.EncodingUtil; @@ -104,7 +97,7 @@ public class LossyEncodingInspection extends LocalInspectionTool { // no sense in checking transparently decoded file: all characters there are already safely encoded if (charset instanceof Native2AsciiCharset) return null; - List descriptors = new SmartList(); + List descriptors = new SmartList<>(); boolean ok = checkFileLoadedInWrongEncoding(file, manager, isOnTheFly, virtualFile, charset, descriptors); if (ok) { checkIfCharactersWillBeLostAfterSave(file, manager, isOnTheFly, text, charset, descriptors); @@ -173,11 +166,11 @@ public class LossyEncodingInspection extends LocalInspectionTool { @NotNull List descriptors) { int errorCount = 0; int start = -1; + CharBuffer buffer = CharBuffer.wrap(text); // temp buffer for encoding/decoding back a char or a surrogate pair. for (int i = 0; i <= text.length(); i++) { char c = i >= text.length() ? 0 : text.charAt(i); - char next = i + 1 >= text.length() ? 0 : text.charAt(i + 1); - char prev = i == 0 ? 0 : text.charAt(i - 1); - if (i == text.length() || isRepresentable(c, next, prev, charset)) { + int end = Character.isHighSurrogate(c) && i