2 * Copyright (C) 2008 The Android Open Source Project
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.
17 public class SourcePositionInfo implements Comparable
19 public SourcePositionInfo() {
20 this.file = "<unknown>";
25 public SourcePositionInfo(String file, int line, int column)
32 public SourcePositionInfo(SourcePositionInfo that)
34 this.file = that.file;
35 this.line = that.line;
36 this.column = that.column;
40 * Given this position and str which occurs at that position, as well as str an index into str,
41 * find the SourcePositionInfo.
43 * @throw StringIndexOutOfBoundsException if index > str.length()
45 public static SourcePositionInfo add(SourcePositionInfo that, String str, int index)
52 for (int i=0; i<index; i++) {
53 char c = str.charAt(i);
54 if (c == '\r' || (c == '\n' && prev != '\r')) {
59 return new SourcePositionInfo(that.file, line, 0);
62 public static SourcePositionInfo findBeginning(SourcePositionInfo that, String str)
67 int line = that.line-1; // -1 because, well, it seems to work
69 for (int i=str.length()-1; i>=0; i--) {
70 char c = str.charAt(i);
71 if ((c == '\r' && prev != '\n') || (c == '\n')) {
76 return new SourcePositionInfo(that.file, line, 0);
79 public String toString()
81 return file + ':' + line;
84 public int compareTo(Object o) {
85 SourcePositionInfo that = (SourcePositionInfo)o;
86 int r = this.file.compareTo(that.file);
88 return this.line - that.line;