Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions core/src/main/java/com/google/googlejavaformat/Newlines.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@

import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;

/** Platform-independent newline handling. */
public class Newlines {

private static Map<String, List<Integer>> cache = new ConcurrentHashMap<String, List<Integer>>();

/** Returns the number of line breaks in the input. */
public static int count(String input) {
return Iterators.size(lineOffsetIterator(input)) - 1;
return lineOffsetList(input).size() - 1;
}

/** Returns the index of the first break in the input, or {@code -1}. */
public static int firstBreak(String input) {
Iterator<Integer> it = lineOffsetIterator(input);
it.next();
return it.hasNext() ? it.next() : -1;
List<Integer> offsets = lineOffsetList(input);
return offsets.isEmpty() ? -1 : offsets.get(0);
}

private static final ImmutableSet<String> BREAKS = ImmutableSet.of("\r\n", "\n", "\r");
Expand Down Expand Up @@ -94,7 +98,16 @@ public static boolean containsBreaks(String text) {

/** Returns an iterator over the start offsets of lines in the input. */
public static Iterator<Integer> lineOffsetIterator(String input) {
return new LineOffsetIterator(input);
return lineOffsetList(input).iterator();
}

private static List<Integer> lineOffsetList(String input) {
List<Integer> offsets = cache.get(input);
if (offsets == null) {
offsets = Lists.newArrayList(new LineOffsetIterator(input));
cache.put(input, offsets);
}
return offsets;
}

/** Returns an iterator over lines in the input, including trailing whitespace. */
Expand Down