Optimize indent() implementation
This commit is contained in:
@@ -36,10 +36,8 @@ import org.jetbrains.jet.lang.types.Variance;
|
|||||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringReader;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -732,23 +730,28 @@ public class NamespaceComparator {
|
|||||||
|
|
||||||
|
|
||||||
private static String indent(String string) {
|
private static String indent(String string) {
|
||||||
try {
|
// This method gets called a lot, so the performance is critical
|
||||||
StringBuilder r = new StringBuilder();
|
// That's why the hand-written code
|
||||||
BufferedReader reader = new BufferedReader(new StringReader(string));
|
|
||||||
while (true) {
|
String indent = " ";
|
||||||
String line = reader.readLine();
|
StringBuilder r = new StringBuilder(string.length());
|
||||||
if (line == null) {
|
r.append(indent);
|
||||||
break;
|
boolean lastCharIsNewLine = false;
|
||||||
|
for (int i = 0; i < string.length(); i++) {
|
||||||
|
char c = string.charAt(i);
|
||||||
|
r.append(c);
|
||||||
|
if (c == '\n') {
|
||||||
|
if (i != string.length() - 1) {
|
||||||
|
r.append(indent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lastCharIsNewLine = true;
|
||||||
}
|
}
|
||||||
r.append(" ");
|
|
||||||
r.append(line);
|
|
||||||
r.append("\n");
|
|
||||||
}
|
}
|
||||||
return r.toString();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
if (!lastCharIsNewLine) {
|
||||||
|
r.append("\n");
|
||||||
|
}
|
||||||
|
return r.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user