src/jdk/nashorn/internal/runtime/Source.java

Print this page




 255 
 256     /**
 257      * Find the end of the line containing position.
 258      * @param position Index to offending token.
 259      * @return Index of last character of line.
 260      */
 261     private int findEOLN(final int position) {
 262          for (int i = position; i < length; i++) {
 263             final char ch = content[i];
 264 
 265             if (ch == '\n' || ch == '\r') {
 266                 return i - 1;
 267             }
 268         }
 269 
 270         return length - 1;
 271     }
 272 
 273     /**
 274      * Return line number of character position.




 275      * @param position Position of character in source content.
 276      * @return Line number.
 277      */
 278     public int getLine(final int position) {
 279         // Line count starts at 1.
 280         int line = 1;
 281 
 282         for (int i = 0; i < position; i++) {
 283             final char ch = content[i];
 284             // Works for both \n and \r\n.
 285             if (ch == '\n') {
 286                 line++;
 287             }
 288         }
 289 
 290         return line;
 291     }
 292 
 293     /**
 294      * Return column number of character position.




 255 
 256     /**
 257      * Find the end of the line containing position.
 258      * @param position Index to offending token.
 259      * @return Index of last character of line.
 260      */
 261     private int findEOLN(final int position) {
 262          for (int i = position; i < length; i++) {
 263             final char ch = content[i];
 264 
 265             if (ch == '\n' || ch == '\r') {
 266                 return i - 1;
 267             }
 268         }
 269 
 270         return length - 1;
 271     }
 272 
 273     /**
 274      * Return line number of character position.
 275      *
 276      * <p>This method can be expensive for large sources as it iterates through
 277      * all characters up to {@code position}.</p>
 278      *
 279      * @param position Position of character in source content.
 280      * @return Line number.
 281      */
 282     public int getLine(final int position) {
 283         // Line count starts at 1.
 284         int line = 1;
 285 
 286         for (int i = 0; i < position; i++) {
 287             final char ch = content[i];
 288             // Works for both \n and \r\n.
 289             if (ch == '\n') {
 290                 line++;
 291             }
 292         }
 293 
 294         return line;
 295     }
 296 
 297     /**
 298      * Return column number of character position.