1 /* 2 * Copyright (C) 2020 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.model.text 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( 33 "(unknown)", 34 0 35 ) 36 37 /** 38 * Given this position and str which occurs at that position, as well as str an index into str, 39 * find the SourcePositionInfo. 40 * 41 * @throws StringIndexOutOfBoundsException if index > str.length() 42 */ addnull43 fun add( 44 that: SourcePositionInfo?, 45 str: String, 46 index: Int 47 ): SourcePositionInfo? { 48 if (that == null) { 49 return null 50 } 51 var line = that.line 52 var prev = 0.toChar() 53 for (i in 0 until index) { 54 val c = str[i] 55 if (c == '\r' || c == '\n' && prev != '\r') { 56 line++ 57 } 58 prev = c 59 } 60 return SourcePositionInfo( 61 that.file, 62 line 63 ) 64 } 65 } 66 }