1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 package com.android.tools.metalava.doclava1 17 18 class SourcePositionInfo( 19 private val file: String, 20 private val line: Int 21 ) : Comparable<SourcePositionInfo> { toStringnull22 override fun toString(): String { 23 return "$file:$line" 24 } 25 compareTonull26 override fun compareTo(other: SourcePositionInfo): Int { 27 val r = file.compareTo(other.file) 28 return if (r != 0) r else line - other.line 29 } 30 31 companion object { 32 val UNKNOWN = SourcePositionInfo("(unknown)", 0) 33 34 /** 35 * Given this position and str which occurs at that position, as well as str an index into str, 36 * find the SourcePositionInfo. 37 * 38 * @throws StringIndexOutOfBoundsException if index > str.length() 39 */ addnull40 fun add( 41 that: SourcePositionInfo?, 42 str: String, 43 index: Int 44 ): SourcePositionInfo? { 45 if (that == null) { 46 return null 47 } 48 var line = that.line 49 var prev = 0.toChar() 50 for (i in 0 until index) { 51 val c = str[i] 52 if (c == '\r' || c == '\n' && prev != '\r') { 53 line++ 54 } 55 prev = c 56 } 57 return SourcePositionInfo(that.file, line) 58 } 59 } 60 }