PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. */
  20. package org.jedit.ruby.parser;
  21. import java.util.List;
  22. import java.util.ArrayList;
  23. /**
  24. * @author robmckinnon at users.sourceforge.net
  25. */
  26. public final class LineCounter {
  27. private final List<Integer> endOffsets;
  28. private final String text;
  29. public LineCounter(String text) {
  30. this.text = text;
  31. endOffsets = new ArrayList<Integer>();
  32. char[] chars = text.toCharArray();
  33. int line = 0;
  34. int index = 0;
  35. boolean lastWasNewLine = false;
  36. while (index < chars.length) {
  37. char character = chars[index];
  38. if (isNewLineCharacter(character)) {
  39. index = handleNewLine(line, index, chars, character);
  40. line++;
  41. lastWasNewLine = true;
  42. } else {
  43. index++;
  44. lastWasNewLine = false;
  45. }
  46. }
  47. if (!lastWasNewLine) {
  48. endOffsets.add(line, index - 1);
  49. }
  50. }
  51. public final int getLineCount() {
  52. return endOffsets.size();
  53. }
  54. /**
  55. * @return line starting at 0
  56. */
  57. public int getLineAtOffset(int startOffset) {
  58. int line = 0;
  59. for (int offset : endOffsets) {
  60. if (startOffset > offset) {
  61. line++;
  62. } else {
  63. return line;
  64. }
  65. }
  66. return line;
  67. }
  68. /**
  69. * @param index starting at 0
  70. */
  71. public final String getLine(int index) {
  72. return getLineUpTo(index, getEndOffset(index));
  73. }
  74. /**
  75. * Returns part of line at given index
  76. * up to the given upToOffset.
  77. */
  78. public final String getLineUpTo(int index, int upToOffset) {
  79. return getLine(getStartOffset(index), upToOffset);
  80. }
  81. /**
  82. * Returns start offset for line.
  83. * @param index starting at 0
  84. */
  85. public final int getStartOffset(int index) {
  86. return (index == 0) ? 0 : getEndOffset(index - 1) + 1;
  87. }
  88. /**
  89. * Returns end offset for index.
  90. * @param index starting at 0.
  91. */
  92. public final int getEndOffset(int index) {
  93. return endOffsets.get(index);
  94. }
  95. private String getLine(int beginIndex, int endIndex) {
  96. char endChar = charAt(endIndex);
  97. if (isNewLineCharacter(endChar)) {
  98. return text.substring(beginIndex, endIndex);
  99. } else {
  100. return text.substring(beginIndex, endIndex + 1);
  101. }
  102. }
  103. public char charAt(int index) {
  104. if (index < text.length()) {
  105. return text.charAt(index);
  106. } else {
  107. return (char)-1;
  108. }
  109. }
  110. private int handleNewLine(int line, int index, char[] chars, char character) {
  111. endOffsets.add(line, index);
  112. index++;
  113. if (character == '\r' && index < chars.length) {
  114. character = chars[index];
  115. if (character == '\n') {
  116. index++;
  117. }
  118. }
  119. return index;
  120. }
  121. private static boolean isNewLineCharacter(char character) {
  122. return character == '\n' || character == '\r';
  123. }
  124. public String getText() {
  125. return text;
  126. }
  127. }