void (@NotNull StringBuilder text) { int ind = StringUtil.indexOf(text, BACKSPACE); if (ind < 0) { return; } int guardLength = 0; int newLength = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); boolean append; if (ch == BACKSPACE) { assert guardLength <= newLength; if (guardLength == newLength) { // Backspace is the first char in a new line: // Keep backspace at the first line (guardLength == 0) as it might be in the middle of the actual line, // handle it later (see getBackspacePrefixLength). // Otherwise (for non-first lines), skip backspace as it can't be interpreted if located right after line ending. append = guardLength == 0; } else { append = text.charAt(newLength - 1) == BACKSPACE; if (!append) { newLength--; // interpret \b: delete prev char } } } else { append = true; } if (append) { text.setCharAt(newLength, ch); newLength++; if (ch == '\r' || ch == '\n') guardLength = newLength; } } text.setLength(newLength); }