2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.util.text;
18 import com.intellij.util.ArrayUtil;
19 import com.intellij.util.text.CharArrayCharSequence;
21 import java.util.ArrayList;
22 import java.util.List;
27 public class LineTokenizer {
28 private int myOffset = 0;
29 private int myLength = 0;
30 private int myLineSeparatorLength = 0;
31 private boolean atEnd = false;
32 private CharSequence myText;
34 public static String[] tokenize(CharSequence chars, boolean includeSeparators) {
35 return tokenize(chars, includeSeparators, true);
38 private static String[] tokenize(final CharSequence chars, final boolean includeSeparators, final boolean skipLastEmptyLine) {
39 if (chars == null || chars.length() == 0){
40 return ArrayUtil.EMPTY_STRING_ARRAY;
43 LineTokenizer tokenizer = new LineTokenizer(chars);
44 List<String> lines = new ArrayList<String>();
45 while(!tokenizer.atEnd()){
46 int offset = tokenizer.getOffset();
48 if (includeSeparators){
49 line = chars.subSequence(offset, offset + tokenizer.getLength() + tokenizer.getLineSeparatorLength()).toString();
52 line = chars.subSequence(offset, offset + tokenizer.getLength()).toString();
58 if (!skipLastEmptyLine && stringEdnsWithSeparator(tokenizer)) lines.add("");
60 return lines.toArray(new String[lines.size()]);
63 public static int calcLineCount(final CharSequence chars, final boolean skipLastEmptyLine) {
65 if (chars != null && chars.length() != 0) {
66 final LineTokenizer tokenizer = new LineTokenizer(chars);
67 while (!tokenizer.atEnd()) {
71 if (!skipLastEmptyLine && stringEdnsWithSeparator(tokenizer)) {
78 public static String[] tokenize(char[] chars, boolean includeSeparators) {
79 return tokenize(chars, includeSeparators, true);
82 public static String[] tokenize(char[] chars, boolean includeSeparators, boolean skipLastEmptyLine) {
83 return tokenize(chars, 0, chars.length, includeSeparators, skipLastEmptyLine);
86 public static String[] tokenize(char[] chars, int startOffset, int endOffset, boolean includeSeparators,
87 boolean skipLastEmptyLine) {
88 return tokenize(new CharArrayCharSequence(chars, startOffset, endOffset), includeSeparators, skipLastEmptyLine);
91 private static boolean stringEdnsWithSeparator(LineTokenizer tokenizer) {
92 return tokenizer.getLineSeparatorLength() > 0;
95 public static String[] tokenize(char[] chars, int startOffset, int endOffset, boolean includeSeparators) {
96 return tokenize(chars, startOffset, endOffset, includeSeparators, true);
99 public LineTokenizer(CharSequence text) {
105 public LineTokenizer(char[] text, int startOffset, int endOffset) {
106 this(new CharArrayCharSequence(text, startOffset, endOffset));
109 public final boolean atEnd() {
113 public final int getOffset() {
117 public final int getLength() {
121 public final int getLineSeparatorLength() {
122 return myLineSeparatorLength;
125 public void advance() {
126 int i = myOffset + myLength + myLineSeparatorLength;
127 final int textLength = myText.length();
128 if (i >= textLength){
132 while(i < textLength){
133 char c = myText.charAt(i);
134 if (c == '\r' || c == '\n') break;
138 myOffset = myOffset + myLength + myLineSeparatorLength;
139 myLength = i - myOffset;
141 myLineSeparatorLength = 0;
142 if (i == textLength) return;
144 char first = myText.charAt(i);
145 if (first == '\r' || first == '\n') {
146 myLineSeparatorLength = 1;
150 if (i == textLength) return;
152 char second = myText.charAt(i);
153 if (first == '\r' && second == '\n') {
154 myLineSeparatorLength = 2;