/*
 * Copyright (C) 2014 Francis Galiegue <fgaliegue@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.fge.grappa.buffers;

import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import org.parboiled.support.Position;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.Tainted;
import java.util.List;

// TODO: get rid of edge cases
@ParametersAreNonnullByDefault
public final class LineCounter
{
    // TODO: replace with IntRange from largetext
    private final List<Range<Integer>> lines = Lists.newArrayList();
    private final int nrLines;
    private final int len;

    public LineCounter(final CharSequence input)
    {
        int lowerBound = 0;
        int index = 0;
        len = input.length();

        while (index < len) {
            if (input.charAt(index++) != '\n')
                continue;
            lines.add(Range.closedOpen(lowerBound, index));
            lowerBound = index;
        }
        lines.add(Range.closedOpen(lowerBound, index));
        nrLines = lines.size();
    }

    public int getNrLines()
    {
        return nrLines;
    }

    public Range<Integer> getLineRange(@Tainted final int lineNr)
    {
        // Edge case: unfortunately, we can get an illegal line number
        return lines.get(Math.min(lineNr, nrLines) - 1);
    }

    public Position toPosition(@Tainted final int index)
    {
        Range<Integer> range;
        // Edge case: unfortunately, we can get an illegal index
        if (index >= len) {
            range = lines.get(nrLines - 1);
            return new Position(nrLines, len - range.lowerEndpoint() + 1);
        }

        for (int i = 0; i < nrLines; i++) {
            range = lines.get(i);
            if (range.contains(index))
                return new Position(i + 1, index - range.lowerEndpoint() + 1);
        }

        throw new IllegalStateException();
    }
}

@@ -919,16 +919,68 @@
 e cases%0A
+@SuppressWarnings(%7B %22AutoBoxing%22, %22AutoUnboxing%22 %7D)%0A
 @Paramet
@@ -1993,16 +1993,93 @@
         
+if (index %3C 0)%0A            throw new IllegalStateException();%0A%0A        final 
 Range%3CIn
@@ -2092,16 +2092,17 @@
  range;%0A
+%0A
         
@@ -2329,42 +2329,47 @@
    f
-or (int i = 0; i %3C nrLines; i++) %7B
+inal int lineNr = binarySearch(index);%0A
 %0A   
@@ -2365,36 +2365,32 @@
 ndex);%0A%0A        
-    
 range = lines.ge
@@ -2391,17 +2391,22 @@
 nes.get(
-i
+lineNr
 );%0A     
@@ -2412,47 +2412,135 @@
     
-    if (range.contains(
+return new Position(lineNr + 1, index - range.lowerEndpoint() + 1);%0A    %7D%0A%0A    private int binarySearch(final int 
 index)
-)%0A 
+%0A
     
-   
+%7B%0A
     
@@ -2554,115 +2554,549 @@
 urn 
-new Position(i + 1, index - range.lowerEndpoint() + 1);%0A        %7D%0A%0A        throw new IllegalStateException(
+doBinarySearch(0, nrLines - 1, index);%0A    %7D%0A%0A    private int doBinarySearch(final int low, final int high, final int index)%0A    %7B%0A        // Guaranteed to always succeed at this point%0A        if (low == high)%0A            return low;%0A%0A        final int middle = (low + high) / 2;%0A        final Range%3CInteger%3E range = lines.get(middle);%0A        if (range.contains(index))%0A            return middle;%0A%0A        return index %3C range.lowerEndpoint()%0A            ? doBinarySearch(low, middle, index)%0A            : doBinarySearch(middle, high, index
 );%0A 

use a binary search to look for our line