return ix2 + original - adapted;
}
protected char[] buildBuffer2() {
StringBuilder sb = new StringBuilder();
IntArrayStack previousLevels = new IntArrayStack();
IntArrayStack newlines = new IntArrayStack();
IntArrayStack newlines2 = new IntArrayStack();
int length = buffer.length;
int currentLevel = 0;
int cursor = 0;
previousLevels.push(0);
// consume inital indent
loop1:
while (cursor < length) {
switch (buffer[cursor]) {
case ' ':
cursor++;
currentLevel++;
break;
case '\t':
cursor++;
currentLevel = ((currentLevel / tabStop) + 1) * tabStop;
break;
default:
break loop1;
}
}
int indexDelta = currentLevel;
// transform all other input
while (cursor < length) {
char c = buffer[cursor++];
sb.append(c);
if (c != '\n') continue;
newlines.push(cursor - 1);
newlines2.push(cursor - indexDelta - 1);
// consume line indent
int indent = 0;
loop2:
while (cursor < length) {
switch (buffer[cursor]) {
case ' ':
cursor++;
indexDelta++;
indent++;
break;
case '\t':
cursor++;
indexDelta++;
indent = ((indent / tabStop) + 1) * tabStop;
break;
default:
break loop2;
}
}
// generate INDENTS/DEDENTS
if (indent > currentLevel) {
previousLevels.push(currentLevel);
currentLevel = indent;
sb.append(Chars.INDENT);
indexDelta--;
} else {
while (indent < currentLevel && indent <= previousLevels.peek()) {
currentLevel = previousLevels.pop();
sb.append(Chars.DEDENT);
indexDelta--;
}
}
}
// make sure to close all remaining indentation scopes
if (previousLevels.size() > 1) {
sb.append('\n');
newlines.push(cursor);
newlines2.push(cursor - indexDelta);
while (previousLevels.size() > 1) {
previousLevels.pop();
sb.append(Chars.DEDENT);
}
}
this.newlines = new int[newlines.size()];
newlines.getElements(this.newlines, 0);
this.newlines2 = new int[newlines2.size()];
newlines2.getElements(this.newlines2, 0);
char[] buffer = new char[sb.length()];
sb.getChars(0, sb.length(), buffer, 0);
return buffer;