use of org.eclipse.jgit.util.IntList in project egit by eclipse.
the class SpellcheckableMessageArea method calculateWrapOffsets.
/**
* Calculates wrap offsets for the given line, so that resulting lines are
* no longer than <code>maxLineLength</code> if possible.
*
* @param line
* the line to wrap (can contain '\n', but no other line delimiters)
* @param maxLineLength
* the maximum line length
* @return an array of offsets where hard-wraps should be inserted, or
* <code>null</code> if the line does not need to be wrapped
*/
public static int[] calculateWrapOffsets(final String line, final int maxLineLength) {
if (line.length() == 0)
return null;
IntList wrapOffsets = new IntList();
int wordStart = 0;
int lineStart = 0;
int length = line.length();
int nofPreviousWordChars = 0;
int nofCurrentWordChars = 0;
for (int i = 0; i < length; i++) {
char ch = line.charAt(i);
if (ch == ' ') {
nofPreviousWordChars += nofCurrentWordChars;
nofCurrentWordChars = 0;
} else if (ch == '\n') {
lineStart = i + 1;
wordStart = i + 1;
nofPreviousWordChars = 0;
nofCurrentWordChars = 0;
} else {
// a word character
if (nofCurrentWordChars == 0) {
// such as " * <very_long_word>".
if (nofPreviousWordChars > maxLineLength / 10 || nofPreviousWordChars > 0 && (i - lineStart) > maxLineLength / 2) {
wordStart = i;
}
}
nofCurrentWordChars++;
if (i >= lineStart + maxLineLength) {
if (wordStart != lineStart) {
// don't break before a single long word
wrapOffsets.add(wordStart);
lineStart = wordStart;
nofPreviousWordChars = 0;
nofCurrentWordChars = 0;
}
}
}
}
int size = wrapOffsets.size();
if (size == 0) {
return null;
} else {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = wrapOffsets.get(i);
}
return result;
}
}
use of org.eclipse.jgit.util.IntList in project gerrit by GerritCodeReview.
the class FileHeaderUtil method getHeaderLines.
static ImmutableList<String> getHeaderLines(byte[] header) {
final IntList lineStartOffsets = RawParseUtils.lineMap(header, 0, header.length);
final ImmutableList.Builder<String> headerLines = ImmutableList.builderWithExpectedSize(lineStartOffsets.size() - 1);
for (int i = 1; i < lineStartOffsets.size() - 1; i++) {
final int b = lineStartOffsets.get(i);
int e = lineStartOffsets.get(i + 1);
if (header[e - 1] == '\n') {
e--;
}
headerLines.add(RawParseUtils.decode(UTF_8, header, b, e));
}
return headerLines.build();
}
use of org.eclipse.jgit.util.IntList in project gerrit by GerritCodeReview.
the class PatchListEntry method getHeaderLines.
public ImmutableList<String> getHeaderLines() {
final IntList m = RawParseUtils.lineMap(header, 0, header.length);
final ImmutableList.Builder<String> headerLines = ImmutableList.builderWithExpectedSize(m.size() - 1);
for (int i = 1; i < m.size() - 1; i++) {
final int b = m.get(i);
int e = m.get(i + 1);
if (header[e - 1] == '\n') {
e--;
}
headerLines.add(RawParseUtils.decode(UTF_8, header, b, e));
}
return headerLines.build();
}
Aggregations