PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/parser/LineCounter.java

#
Java | 150 lines | 87 code | 23 blank | 40 comment | 20 complexity | 6ee7adee7a9a58c4154c3cff0155feee MD5 | raw file
  1/*
  2 * LineCounter.java - 
  3 *
  4 * Copyright 2005 Robert McKinnon
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package org.jedit.ruby.parser;
 21
 22import java.util.List;
 23import java.util.ArrayList;
 24
 25/**
 26 * @author robmckinnon at users.sourceforge.net
 27 */
 28public final class LineCounter {
 29
 30    private final List<Integer> endOffsets;
 31    private final String text;
 32
 33    public LineCounter(String text) {
 34        this.text = text;
 35        endOffsets = new ArrayList<Integer>();
 36
 37        char[] chars = text.toCharArray();
 38        int line = 0;
 39        int index = 0;
 40        boolean lastWasNewLine = false;
 41
 42        while (index < chars.length) {
 43            char character = chars[index];
 44
 45            if (isNewLineCharacter(character)) {
 46                index = handleNewLine(line, index, chars, character);
 47                line++;
 48                lastWasNewLine = true;
 49            } else {
 50                index++;
 51                lastWasNewLine = false;
 52            }
 53        }
 54
 55        if (!lastWasNewLine) {
 56            endOffsets.add(line, index - 1);
 57        }
 58    }
 59
 60    public final int getLineCount() {
 61        return endOffsets.size();
 62    }
 63
 64    /**
 65     * @return line starting at 0
 66     */
 67    public int getLineAtOffset(int startOffset) {
 68        int line = 0;
 69
 70        for (int offset : endOffsets) {
 71            if (startOffset > offset) {
 72                line++;
 73            } else {
 74                return line;
 75            }
 76        }
 77
 78        return line;
 79    }
 80
 81    /**
 82     * @param index starting at 0
 83     */
 84    public final String getLine(int index) {
 85        return getLineUpTo(index, getEndOffset(index));
 86    }
 87
 88    /**
 89     * Returns part of line at given index
 90     * up to the given upToOffset.
 91     */
 92    public final String getLineUpTo(int index, int upToOffset) {
 93        return getLine(getStartOffset(index), upToOffset);
 94    }
 95
 96    /**
 97     * Returns start offset for line.
 98     * @param index starting at 0
 99     */
100    public final int getStartOffset(int index) {
101        return (index == 0) ? 0 : getEndOffset(index - 1) + 1;
102    }
103
104    /**
105     * Returns end offset for index.
106     * @param index starting at 0.
107     */
108    public final int getEndOffset(int index) {
109        return endOffsets.get(index);
110    }
111
112    private String getLine(int beginIndex, int endIndex) {
113        char endChar = charAt(endIndex);
114        if (isNewLineCharacter(endChar)) {
115            return text.substring(beginIndex, endIndex);
116        } else {
117            return text.substring(beginIndex, endIndex + 1);
118        }
119    }
120
121    public char charAt(int index) {
122      if (index < text.length()) {
123          return text.charAt(index);
124      } else {
125          return (char)-1;
126      }
127    }
128
129    private int handleNewLine(int line, int index, char[] chars, char character) {
130        endOffsets.add(line, index);
131        index++;
132
133        if (character == '\r' && index < chars.length) {
134            character = chars[index];
135            if (character == '\n') {
136                index++;
137            }
138        }
139
140        return index;
141    }
142
143    private static boolean isNewLineCharacter(char character) {
144        return character == '\n' || character == '\r';
145    }
146
147    public String getText() {
148        return text;
149    }
150}