TextRange (@NotNull String text, int from, int to) { if (from > to || to < 0) return null; if (text.length() < 2 || !text.startsWith("\"") || !text.endsWith("\"")) { return null; } if (text.indexOf('\\') == -1) { return new TextRange(from + 1, to + 1); } text = text.substring(1, text.length() - 1); int charsSoFar = 0; int mappedFrom = -1; for (int i = 0; i != -1; i = getCharEndIndex(text, i)) { if (charsSoFar == from) { mappedFrom = i; } if (charsSoFar == to) { // +1 to count open quote return new TextRange(mappedFrom + 1, i + 1); } charsSoFar++; } return null; }