All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.clover.instr.aspectj.text.LinesToTextRange Maven / Gradle / Ivy

The newest version!
package com.atlassian.clover.instr.aspectj.text;

import com.atlassian.clover.instr.aspectj.text.TextRange;

import java.util.ArrayList;

public class LinesToTextRange {
    private final ArrayList linesEndIndex;

    public LinesToTextRange(String input) {
        linesEndIndex = new ArrayList();

        String[] lines = input.split("[\n\r]");
        int endIndex = 0;
        for (int i = 0; i < lines.length; i++) {
            endIndex += lines[i].length() + (i < lines.length - 1 ? 1 : 0);
            linesEndIndex.add(endIndex);
        }
    }

    /**
     * @param lineNumber counted from 0
     * @return TextRange
     */
    public TextRange getTextRangeForLine(int lineNumber) {
        if (lineNumber >= 0 && lineNumber < linesEndIndex.size()) {
            int min = lineNumber == 0 ? 0 : linesEndIndex.get(lineNumber - 1) + 1;
            int max = linesEndIndex.get(lineNumber);
            return new TextRange(min, max);
        } else {
            return new TextRange(0, 0);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy