1 24 25 package org.aspectj.compiler.base.parser; 26 27 import java.io.File ; 28 import java.util.*; 29 import org.aspectj.util.IntList; 30 31 public class SourceInfo { 32 private File file; 33 private int[] lineStarts; 34 private char[] text; 36 public SourceInfo(File _file, char[] _text) { 37 file = _file; 38 setText(_text); 39 } 40 41 public void setText(char[] text) { 42 this.text = text; 43 if (text != null) { 44 lineStarts = findNewLines(text); 45 } 46 } 47 48 private int[] findNewLines(char[] text) { 49 IntList lineStarts = new IntList(); 50 lineStarts.add(0); 51 final int N = text.length; 52 for(int i=0; i<N; i++) { 53 char ch = text[i]; 54 if (ch == '\r') { 55 if (i < N-1 && text[i+1] == '\n') { 56 text[i] = ' '; } else { 60 text[i] = '\n'; 61 ch = '\n'; 62 } 63 } 64 if (ch == '\n') { 65 lineStarts.add(i+1); 66 } 67 } 68 lineStarts.add(N+100); 69 return lineStarts.toIntArray(); 70 } 71 72 public File getFile() { 73 return file; 74 } 75 76 public int getLine(int position) { 78 if (lineStarts == null) return -1; 79 int pos = Arrays.binarySearch(lineStarts, position); 80 if (pos >= 0) return pos+1; 81 else return (-pos)-1; 82 } 83 84 public int getColumn(int position) { 86 if (lineStarts == null) return -1; 87 88 int line = 1; 89 for(; line < lineStarts.length; line++) { 90 if (lineStarts[line] > position) break; 91 } 92 93 return (position - lineStarts[line-1]) + 1; 94 } 95 } 96 | Popular Tags |