1 /*
2 * Copyright 2019 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
17 package androidx.compose.ui.text
18
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.ui.text.internal.requirePrecondition
21 import androidx.compose.ui.util.fastCoerceIn
22 import androidx.compose.ui.util.packInts
23 import androidx.compose.ui.util.unpackInt1
24 import androidx.compose.ui.util.unpackInt2
25 import kotlin.math.max
26 import kotlin.math.min
27
CharSequencenull28 fun CharSequence.substring(range: TextRange): String = this.substring(range.min, range.max)
29
30 /**
31 * An immutable text range class, represents a text range from [start] (inclusive) to [end]
32 * (exclusive). [end] can be smaller than [start] and in those cases [min] and [max] can be used in
33 * order to fetch the values.
34 *
35 * @param start the inclusive start offset of the range. Must be non-negative, otherwise an
36 * exception will be thrown.
37 * @param end the exclusive end offset of the range. Must be non-negative, otherwise an exception
38 * will be thrown.
39 */
40 fun TextRange(/*@IntRange(from = 0)*/ start: Int, /*@IntRange(from = 0)*/ end: Int) =
41 TextRange(packWithCheck(start, end))
42
43 /**
44 * An immutable text range class, represents a text range from [start] (inclusive) to [end]
45 * (exclusive). [end] can be smaller than [start] and in those cases [min] and [max] can be used in
46 * order to fetch the values.
47 */
48 @kotlin.jvm.JvmInline
49 @Immutable
50 value class TextRange internal constructor(private val packedValue: Long) {
51
52 val start: Int
53 get() = unpackInt1(packedValue)
54
55 val end: Int
56 get() = unpackInt2(packedValue)
57
58 /** The minimum offset of the range. */
59 val min: Int
60 get() = min(start, end)
61
62 /** The maximum offset of the range. */
63 val max: Int
64 get() = max(start, end)
65
66 /** Returns true if the range is collapsed */
67 val collapsed: Boolean
68 get() = start == end
69
70 /** Returns true if the start offset is larger than the end offset. */
71 val reversed: Boolean
72 get() = start > end
73
74 /** Returns the length of the range. */
75 val length: Int
76 get() = max - min
77
78 /** Returns true if the given range has intersection with this range */
79 fun intersects(other: TextRange): Boolean = (min < other.max) and (other.min < max)
80
81 /** Returns true if this range covers including equals with the given range. */
82 operator fun contains(other: TextRange): Boolean = (min <= other.min) and (other.max <= max)
83
84 /** Returns true if the given offset is a part of this range. */
85 operator fun contains(offset: Int): Boolean = offset in min until max
86
87 override fun toString(): String {
88 return "TextRange($start, $end)"
89 }
90
91 companion object {
92 val Zero = TextRange(0)
93 }
94 }
95
96 /** Creates a [TextRange] where start is equal to end, and the value of those are [index]. */
TextRangenull97 fun TextRange(index: Int): TextRange = TextRange(start = index, end = index)
98
99 /**
100 * Ensures that [TextRange.start] and [TextRange.end] values lies in the specified range
101 * [minimumValue] and [maximumValue]. For each [TextRange.start] and [TextRange.end] values:
102 * - if value is smaller than [minimumValue], value is replaced by [minimumValue]
103 * - if value is greater than [maximumValue], value is replaced by [maximumValue]
104 *
105 * @param minimumValue the minimum value that [TextRange.start] or [TextRange.end] can be.
106 * @param maximumValue the exclusive maximum value that [TextRange.start] or [TextRange.end] can be.
107 */
108 fun TextRange.coerceIn(minimumValue: Int, maximumValue: Int): TextRange {
109 val newStart = start.fastCoerceIn(minimumValue, maximumValue)
110 val newEnd = end.fastCoerceIn(minimumValue, maximumValue)
111 if (newStart != start || newEnd != end) {
112 return TextRange(newStart, newEnd)
113 }
114 return this
115 }
116
packWithChecknull117 private fun packWithCheck(start: Int, end: Int): Long {
118 requirePrecondition(start >= 0 && end >= 0) {
119 "start and end cannot be negative. [start: $start, end: $end]"
120 }
121 return packInts(start, end)
122 }
123