StdLib cleanup, deprecated symbol usage: size() and length()

This commit is contained in:
Ilya Gorbunov
2015-11-14 05:56:57 +03:00
parent 21e2e68ed4
commit 07654eb82b
59 changed files with 730 additions and 730 deletions
File diff suppressed because it is too large Load Diff
+25 -25
View File
@@ -464,7 +464,7 @@ public inline fun <T> List<T>.lastIndexOfRaw(element: Any?): Int {
*/
public fun <T> Iterable<T>.lastOrNull(): T? {
when (this) {
is List -> return if (isEmpty()) null else this[size() - 1]
is List -> return if (isEmpty()) null else this[size - 1]
else -> {
val iterator = iterator()
if (!iterator.hasNext())
@@ -481,7 +481,7 @@ public fun <T> Iterable<T>.lastOrNull(): T? {
* Returns the last element, or `null` if the list is empty.
*/
public fun <T> List<T>.lastOrNull(): T? {
return if (isEmpty()) null else this[size() - 1]
return if (isEmpty()) null else this[size - 1]
}
/**
@@ -515,7 +515,7 @@ public inline fun <T> List<T>.lastOrNull(predicate: (T) -> Boolean): T? {
*/
public fun <T> Iterable<T>.single(): T {
when (this) {
is List -> return when (size()) {
is List -> return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
@@ -536,7 +536,7 @@ public fun <T> Iterable<T>.single(): T {
* Returns the single element, or throws an exception if the list is empty or has more than one element.
*/
public fun <T> List<T>.single(): T {
return when (size()) {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
@@ -565,7 +565,7 @@ public inline fun <T> Iterable<T>.single(predicate: (T) -> Boolean): T {
*/
public fun <T> Iterable<T>.singleOrNull(): T? {
when (this) {
is List -> return if (size() == 1) this[0] else null
is List -> return if (size == 1) this[0] else null
else -> {
val iterator = iterator()
if (!iterator.hasNext())
@@ -582,7 +582,7 @@ public fun <T> Iterable<T>.singleOrNull(): T? {
* Returns single element, or `null` if the list is empty or has more than one element.
*/
public fun <T> List<T>.singleOrNull(): T? {
return if (size() == 1) this[0] else null
return if (size == 1) this[0] else null
}
/**
@@ -610,12 +610,12 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
if (n == 0) return toList()
val list: ArrayList<T>
if (this is Collection<*>) {
val resultSize = size() - n
val resultSize = size - n
if (resultSize <= 0)
return emptyList()
list = ArrayList<T>(resultSize)
if (this is List<T>) {
for (index in n..size() - 1) {
for (index in n..size - 1) {
list.add(this[index])
}
return list
@@ -636,7 +636,7 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
*/
public fun <T> List<T>.dropLast(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
return take((size() - n).coerceAtLeast(0))
return take((size - n).coerceAtLeast(0))
}
/**
@@ -756,7 +756,7 @@ public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
public fun <T> Iterable<T>.take(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
if (n == 0) return emptyList()
if (this is Collection<T> && n >= size()) return toList()
if (this is Collection<T> && n >= size) return toList()
var count = 0
val list = ArrayList<T>(n)
for (item in this) {
@@ -773,7 +773,7 @@ public fun <T> Iterable<T>.take(n: Int): List<T> {
public fun <T> List<T>.takeLast(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
if (n == 0) return emptyList()
val size = size()
val size = size
if (n >= size) return toList()
val list = ArrayList<T>(n)
for (index in size - n .. size - 1)
@@ -891,7 +891,7 @@ public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> {
* Returns an array of Boolean containing all of the elements of this collection.
*/
public fun Collection<Boolean>.toBooleanArray(): BooleanArray {
val result = BooleanArray(size())
val result = BooleanArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -902,7 +902,7 @@ public fun Collection<Boolean>.toBooleanArray(): BooleanArray {
* Returns an array of Byte containing all of the elements of this collection.
*/
public fun Collection<Byte>.toByteArray(): ByteArray {
val result = ByteArray(size())
val result = ByteArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -913,7 +913,7 @@ public fun Collection<Byte>.toByteArray(): ByteArray {
* Returns an array of Char containing all of the elements of this collection.
*/
public fun Collection<Char>.toCharArray(): CharArray {
val result = CharArray(size())
val result = CharArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -924,7 +924,7 @@ public fun Collection<Char>.toCharArray(): CharArray {
* Returns an array of Double containing all of the elements of this collection.
*/
public fun Collection<Double>.toDoubleArray(): DoubleArray {
val result = DoubleArray(size())
val result = DoubleArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -935,7 +935,7 @@ public fun Collection<Double>.toDoubleArray(): DoubleArray {
* Returns an array of Float containing all of the elements of this collection.
*/
public fun Collection<Float>.toFloatArray(): FloatArray {
val result = FloatArray(size())
val result = FloatArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -946,7 +946,7 @@ public fun Collection<Float>.toFloatArray(): FloatArray {
* Returns an array of Int containing all of the elements of this collection.
*/
public fun Collection<Int>.toIntArray(): IntArray {
val result = IntArray(size())
val result = IntArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -957,7 +957,7 @@ public fun Collection<Int>.toIntArray(): IntArray {
* Returns an array of Long containing all of the elements of this collection.
*/
public fun Collection<Long>.toLongArray(): LongArray {
val result = LongArray(size())
val result = LongArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -968,7 +968,7 @@ public fun Collection<Long>.toLongArray(): LongArray {
* Returns an array of Short containing all of the elements of this collection.
*/
public fun Collection<Short>.toShortArray(): ShortArray {
val result = ShortArray(size())
val result = ShortArray(size)
var index = 0
for (element in this)
result[index++] = element
@@ -1272,7 +1272,7 @@ public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
* Returns the number of elements in this collection.
*/
public fun <T> Collection<T>.count(): Int {
return size()
return size
}
/**
@@ -1543,7 +1543,7 @@ public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<Lis
* Returns a list containing all elements of the original collection and then all elements of the given [array].
*/
public operator fun <T> Collection<T>.plus(array: Array<out T>): List<T> {
val result = ArrayList<T>(this.size() + array.size())
val result = ArrayList<T>(this.size + array.size)
result.addAll(this)
result.addAll(array)
return result
@@ -1565,7 +1565,7 @@ public operator fun <T> Iterable<T>.plus(array: Array<out T>): List<T> {
*/
public operator fun <T> Collection<T>.plus(collection: Iterable<T>): List<T> {
if (collection is Collection) {
val result = ArrayList<T>(this.size() + collection.size())
val result = ArrayList<T>(this.size + collection.size)
result.addAll(this)
result.addAll(collection)
return result
@@ -1591,7 +1591,7 @@ public operator fun <T> Iterable<T>.plus(collection: Iterable<T>): List<T> {
* Returns a list containing all elements of the original collection and then the given [element].
*/
public operator fun <T> Collection<T>.plus(element: T): List<T> {
val result = ArrayList<T>(size() + 1)
val result = ArrayList<T>(size + 1)
result.addAll(this)
result.add(element)
return result
@@ -1612,7 +1612,7 @@ public operator fun <T> Iterable<T>.plus(element: T): List<T> {
* Returns a list containing all elements of the original collection and then all elements of the given [sequence].
*/
public operator fun <T> Collection<T>.plus(sequence: Sequence<T>): List<T> {
val result = ArrayList<T>(this.size() + 10)
val result = ArrayList<T>(this.size + 10)
result.addAll(this)
result.addAll(sequence)
return result
@@ -1639,7 +1639,7 @@ public fun <T, R> Iterable<T>.zip(array: Array<out R>): List<Pair<T, R>> {
* Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
*/
public inline fun <T, R, V> Iterable<T>.zip(array: Array<out R>, transform: (T, R) -> V): List<V> {
val arraySize = array.size()
val arraySize = array.size
val list = ArrayList<V>(Math.min(collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in this) {
+3 -3
View File
@@ -16,7 +16,7 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col
* Returns a [List] containing all key-value pairs.
*/
public fun <K, V> Map<K, V>.toList(): List<Pair<K, V>> {
val result = ArrayList<Pair<K, V>>(size())
val result = ArrayList<Pair<K, V>>(size)
for (item in this)
result.add(item.key to item.value)
return result
@@ -45,7 +45,7 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.flatMapTo(des
* to each entry in the original map.
*/
public inline fun <K, V, R> Map<K, V>.map(transform: (Map.Entry<K, V>) -> R): List<R> {
return mapTo(ArrayList<R>(size()), transform)
return mapTo(ArrayList<R>(size), transform)
}
/**
@@ -115,7 +115,7 @@ public inline fun <K, V> Map<K, V>.any(predicate: (Map.Entry<K, V>) -> Boolean):
* Returns the number of entrys in this map.
*/
public fun <K, V> Map<K, V>.count(): Int {
return size()
return size
}
/**
+5 -5
View File
@@ -39,7 +39,7 @@ public operator fun <T> Set<T>.minus(collection: Iterable<T>): Set<T> {
* Returns a set containing all elements of the original set except the given [element].
*/
public operator fun <T> Set<T>.minus(element: T): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(size()))
val result = LinkedHashSet<T>(mapCapacity(size))
var removed = false
return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true }
}
@@ -57,7 +57,7 @@ public operator fun <T> Set<T>.minus(sequence: Sequence<T>): Set<T> {
* Returns a set containing all elements both of the original set and the given [array].
*/
public operator fun <T> Set<T>.plus(array: Array<out T>): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(this.size() + array.size()))
val result = LinkedHashSet<T>(mapCapacity(this.size + array.size))
result.addAll(this)
result.addAll(array)
return result
@@ -67,7 +67,7 @@ public operator fun <T> Set<T>.plus(array: Array<out T>): Set<T> {
* Returns a set containing all elements both of the original set and the given [collection].
*/
public operator fun <T> Set<T>.plus(collection: Iterable<T>): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2))
val result = LinkedHashSet<T>(mapCapacity(collection.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2))
result.addAll(this)
result.addAll(collection)
return result
@@ -77,7 +77,7 @@ public operator fun <T> Set<T>.plus(collection: Iterable<T>): Set<T> {
* Returns a set containing all elements of the original set and then the given [element].
*/
public operator fun <T> Set<T>.plus(element: T): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(size() + 1))
val result = LinkedHashSet<T>(mapCapacity(size + 1))
result.addAll(this)
result.add(element)
return result
@@ -87,7 +87,7 @@ public operator fun <T> Set<T>.plus(element: T): Set<T> {
* Returns a set containing all elements both of the original set and the given [sequence].
*/
public operator fun <T> Set<T>.plus(sequence: Sequence<T>): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(this.size() * 2))
val result = LinkedHashSet<T>(mapCapacity(this.size * 2))
result.addAll(this)
result.addAll(sequence)
return result
+26 -26
View File
@@ -289,7 +289,7 @@ public inline fun String.last(predicate: (Char) -> Boolean): Char {
* Returns the last character, or `null` if the char sequence is empty.
*/
public fun CharSequence.lastOrNull(): Char? {
return if (isEmpty()) null else this[length() - 1]
return if (isEmpty()) null else this[length - 1]
}
/**
@@ -297,7 +297,7 @@ public fun CharSequence.lastOrNull(): Char? {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.lastOrNull(): Char? {
return if (isEmpty()) null else this[length() - 1]
return if (isEmpty()) null else this[length - 1]
}
/**
@@ -327,7 +327,7 @@ public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? {
* Returns the single character, or throws an exception if the char sequence is empty or has more than one character.
*/
public fun CharSequence.single(): Char {
return when (length()) {
return when (length) {
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
@@ -339,7 +339,7 @@ public fun CharSequence.single(): Char {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.single(): Char {
return when (length()) {
return when (length) {
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
@@ -385,7 +385,7 @@ public inline fun String.single(predicate: (Char) -> Boolean): Char {
* Returns single character, or `null` if the char sequence is empty or has more than one character.
*/
public fun CharSequence.singleOrNull(): Char? {
return if (length() == 1) this[0] else null
return if (length == 1) this[0] else null
}
/**
@@ -393,7 +393,7 @@ public fun CharSequence.singleOrNull(): Char? {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.singleOrNull(): Char? {
return if (length() == 1) this[0] else null
return if (length == 1) this[0] else null
}
/**
@@ -576,7 +576,7 @@ public inline fun <C : Appendable> String.filterNotTo(destination: C, predicate:
* Appends all characters matching the given [predicate] to the given [destination].
*/
public inline fun <C : Appendable> CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0..length() - 1) {
for (index in 0..length - 1) {
val element = get(index)
if (predicate(element)) destination.append(element)
}
@@ -588,7 +588,7 @@ public inline fun <C : Appendable> CharSequence.filterTo(destination: C, predica
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun <C : Appendable> String.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0..length() - 1) {
for (index in 0..length - 1) {
val element = get(index)
if (predicate(element)) destination.append(element)
}
@@ -735,7 +735,7 @@ public fun String.reversed(): String {
* Returns an [ArrayList] of all characters.
*/
public fun CharSequence.toArrayList(): ArrayList<Char> {
return toCollection(ArrayList<Char>(length()))
return toCollection(ArrayList<Char>(length))
}
/**
@@ -743,7 +743,7 @@ public fun CharSequence.toArrayList(): ArrayList<Char> {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toArrayList(): ArrayList<Char> {
return toCollection(ArrayList<Char>(length()))
return toCollection(ArrayList<Char>(length))
}
/**
@@ -771,7 +771,7 @@ public fun <C : MutableCollection<in Char>> String.toCollection(collection: C):
* Returns a [HashSet] of all characters.
*/
public fun CharSequence.toHashSet(): HashSet<Char> {
return toCollection(HashSet<Char>(mapCapacity(length())))
return toCollection(HashSet<Char>(mapCapacity(length)))
}
/**
@@ -779,7 +779,7 @@ public fun CharSequence.toHashSet(): HashSet<Char> {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toHashSet(): HashSet<Char> {
return toCollection(HashSet<Char>(mapCapacity(length())))
return toCollection(HashSet<Char>(mapCapacity(length)))
}
/**
@@ -820,7 +820,7 @@ public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
* If any two characters would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K, V> CharSequence.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (length()/.75f) + 1
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
@@ -834,7 +834,7 @@ public inline fun <K, V> CharSequence.toMap(selector: (Char) -> K, transform: (C
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (length()/.75f) + 1
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
@@ -848,7 +848,7 @@ public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -
* If any two characters would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <K> CharSequence.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
@@ -863,7 +863,7 @@ public inline fun <K> CharSequence.toMapBy(selector: (Char) -> K): Map<K, Char>
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), element)
@@ -875,7 +875,7 @@ public inline fun <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> {
* Returns a [Set] of all characters.
*/
public fun CharSequence.toSet(): Set<Char> {
return toCollection(LinkedHashSet<Char>(mapCapacity(length())))
return toCollection(LinkedHashSet<Char>(mapCapacity(length)))
}
/**
@@ -883,7 +883,7 @@ public fun CharSequence.toSet(): Set<Char> {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toSet(): Set<Char> {
return toCollection(LinkedHashSet<Char>(mapCapacity(length())))
return toCollection(LinkedHashSet<Char>(mapCapacity(length)))
}
/**
@@ -984,7 +984,7 @@ public inline fun <K> String.groupByTo(map: MutableMap<K, MutableList<Char>>, to
* to each character in the original char sequence.
*/
public inline fun <R> CharSequence.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(length()), transform)
return mapTo(ArrayList<R>(length), transform)
}
/**
@@ -993,7 +993,7 @@ public inline fun <R> CharSequence.map(transform: (Char) -> R): List<R> {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun <R> String.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(length()), transform)
return mapTo(ArrayList<R>(length), transform)
}
/**
@@ -1001,7 +1001,7 @@ public inline fun <R> String.map(transform: (Char) -> R): List<R> {
* to each character and its index in the original char sequence.
*/
public inline fun <R> CharSequence.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(length()), transform)
return mapIndexedTo(ArrayList<R>(length), transform)
}
/**
@@ -1010,7 +1010,7 @@ public inline fun <R> CharSequence.mapIndexed(transform: (Int, Char) -> R): List
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(length()), transform)
return mapIndexedTo(ArrayList<R>(length), transform)
}
/**
@@ -1161,7 +1161,7 @@ public inline fun String.any(predicate: (Char) -> Boolean): Boolean {
* Returns the length of this char sequence.
*/
public fun CharSequence.count(): Int {
return length()
return length
}
/**
@@ -1169,7 +1169,7 @@ public fun CharSequence.count(): Int {
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.count(): Int {
return length()
return length
}
/**
@@ -1584,7 +1584,7 @@ public fun String.zip(other: String): List<Pair<Char, Char>> {
* Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence.
*/
public inline fun <V> CharSequence.zip(other: String, transform: (Char, Char) -> V): List<V> {
val length = Math.min(this.length(), other.length())
val length = Math.min(this.length, other.length)
val list = ArrayList<V>(length)
for (i in 0..length-1) {
list.add(transform(this[i], other[i]))
@@ -1597,7 +1597,7 @@ public inline fun <V> CharSequence.zip(other: String, transform: (Char, Char) ->
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun <V> String.zip(other: String, transform: (Char, Char) -> V): List<V> {
val length = Math.min(this.length(), other.length())
val length = Math.min(this.length, other.length)
val list = ArrayList<V>(length)
for (i in 0..length-1) {
list.add(transform(this[i], other[i]))
@@ -29,7 +29,7 @@ public inline fun <reified T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Arr
* Returns a single list of all elements from all arrays in the given array.
*/
public fun <T> Array<Array<out T>>.flatten(): List<T> {
val result = ArrayList<T>(sumBy { it.size() })
val result = ArrayList<T>(sumBy { it.size })
for (element in this) {
result.addAll(element)
}
@@ -42,8 +42,8 @@ public fun <T> Array<Array<out T>>.flatten(): List<T> {
* *second* list is built from the second values of each pair from this array.
*/
public fun <T, R> Array<out Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
val listT = ArrayList<T>(size())
val listR = ArrayList<R>(size())
val listT = ArrayList<T>(size)
val listR = ArrayList<R>(size)
for (pair in this) {
listT.add(pair.first)
listR.add(pair.second)
@@ -47,7 +47,7 @@ internal object EmptyList : List<Nothing>, Serializable {
internal fun <T> Array<out T>.asCollection(): Collection<T> = ArrayAsCollection(this)
private class ArrayAsCollection<T>(val values: Array<out T>): Collection<T> {
override val size: Int get() = values.size()
override val size: Int get() = values.size
override fun isEmpty(): Boolean = values.isEmpty()
override fun contains(o: T): Boolean = values.contains(o)
override fun containsAll(c: Collection<T>): Boolean = c.all { contains(it) }
@@ -60,7 +60,7 @@ private class ArrayAsCollection<T>(val values: Array<out T>): Collection<T> {
public fun <T> emptyList(): List<T> = EmptyList
/** Returns a new read-only list of given elements. The returned list is serializable (JVM). */
public fun <T> listOf(vararg values: T): List<T> = if (values.size() > 0) values.asList() else emptyList()
public fun <T> listOf(vararg values: T): List<T> = if (values.size > 0) values.asList() else emptyList()
/** Returns an empty read-only list. The returned list is serializable (JVM). */
public fun <T> listOf(): List<T> = emptyList()
@@ -75,11 +75,11 @@ public fun <T> listOf(value: T): List<T> = Collections.singletonList(value)
/** Returns a new [LinkedList] with the given elements. */
@JvmVersion
public fun <T> linkedListOf(vararg values: T): LinkedList<T>
= if (values.size() == 0) LinkedList() else LinkedList(ArrayAsCollection(values))
= if (values.size == 0) LinkedList() else LinkedList(ArrayAsCollection(values))
/** Returns a new [ArrayList] with the given elements. */
public fun <T> arrayListOf(vararg values: T): ArrayList<T>
= if (values.size() == 0) ArrayList() else ArrayList(ArrayAsCollection(values))
= if (values.size == 0) ArrayList() else ArrayList(ArrayAsCollection(values))
/** Returns a new read-only list either of single given element, if it is not null, or empty list it the element is null. The returned list is serializable (JVM). */
public fun <T : Any> listOfNotNull(value: T?): List<T> = if (value != null) listOf(value) else emptyList()
@@ -91,7 +91,7 @@ public fun <T : Any> listOfNotNull(vararg values: T?): List<T> = values.filterNo
* Returns an [IntRange] of the valid indices for this collection.
*/
public val Collection<*>.indices: IntRange
get() = 0..size() - 1
get() = 0..size - 1
/**
* Returns the index of the last item in the list or -1 if the list is empty.
@@ -99,7 +99,7 @@ public val Collection<*>.indices: IntRange
* @sample test.collections.ListSpecificTest.lastIndex
*/
public val <T> List<T>.lastIndex: Int
get() = this.size() - 1
get() = this.size - 1
/** Returns `true` if the collection is not empty. */
public fun <T> Collection<T>.isNotEmpty(): Boolean = !isEmpty()
@@ -120,15 +120,15 @@ public fun <T> Enumeration<T>.toList(): List<T> = Collections.list(this)
/**
* Returns the size of this iterable if it is known, or `null` otherwise.
*/
public fun <T> Iterable<T>.collectionSizeOrNull(): Int? = if (this is Collection<*>) size() else null
public fun <T> Iterable<T>.collectionSizeOrNull(): Int? = if (this is Collection<*>) this.size else null
/**
* Returns the size of this iterable if it is known, or the specified [default] value otherwise.
*/
public fun <T> Iterable<T>.collectionSizeOrDefault(default: Int): Int = if (this is Collection<*>) size() else default
public fun <T> Iterable<T>.collectionSizeOrDefault(default: Int): Int = if (this is Collection<*>) this.size else default
/** Returns true when it's safe to convert this collection to a set without changing contains method behavior. */
private fun <T> Collection<T>.safeToConvertToSet() = size() > 2 && this is ArrayList
private fun <T> Collection<T>.safeToConvertToSet() = size > 2 && this is ArrayList
/** Converts this collection to a set, when it's worth so and it doesn't change contains method behavior. */
internal fun <T> Iterable<T>.convertToSetForSetOperationWith(source: Iterable<T>): Collection<T> =
@@ -136,7 +136,7 @@ internal fun <T> Iterable<T>.convertToSetForSetOperationWith(source: Iterable<T>
is Set -> this
is Collection ->
when {
source is Collection && source.size() < 2 -> this
source is Collection && source.size < 2 -> this
else -> if (this.safeToConvertToSet()) toHashSet() else this
}
else -> toHashSet()
@@ -153,7 +153,7 @@ internal fun <T> Iterable<T>.convertToSetForSetOperation(): Collection<T> =
// copies typed varargs array to array of objects
@JvmVersion
private fun <T> Array<out T>.varargToArrayOfAny(): Array<Any?>
= Arrays.copyOf(this, this.size(), Array<Any?>::class.java)
= Arrays.copyOf(this, this.size, Array<Any?>::class.java)
/**
* Searches this list or its range for the provided [element] index using binary search algorithm.
@@ -161,8 +161,8 @@ private fun <T> Array<out T>.varargToArrayOfAny(): Array<Any?>
*
* If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found.
*/
public fun <T: Comparable<T>> List<T?>.binarySearch(element: T?, fromIndex: Int = 0, toIndex: Int = size()): Int {
rangeCheck(size(), fromIndex, toIndex)
public fun <T: Comparable<T>> List<T?>.binarySearch(element: T?, fromIndex: Int = 0, toIndex: Int = size): Int {
rangeCheck(size, fromIndex, toIndex)
var low = fromIndex
var high = toIndex - 1
@@ -188,8 +188,8 @@ public fun <T: Comparable<T>> List<T?>.binarySearch(element: T?, fromIndex: Int
*
* If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found.
*/
public fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size()): Int {
rangeCheck(size(), fromIndex, toIndex)
public fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Int {
rangeCheck(size, fromIndex, toIndex)
var low = fromIndex
var high = toIndex - 1
@@ -215,7 +215,7 @@ public fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>, fr
*
* If the list contains multiple elements with the specified [key], there is no guarantee which one will be found.
*/
public inline fun <T, K : Comparable<K>> List<T>.binarySearchBy(key: K?, fromIndex: Int = 0, toIndex: Int = size(), crossinline selector: (T) -> K?): Int =
public inline fun <T, K : Comparable<K>> List<T>.binarySearchBy(key: K?, fromIndex: Int = 0, toIndex: Int = size, crossinline selector: (T) -> K?): Int =
binarySearch(fromIndex, toIndex) { compareValues(selector(it), key) }
// do not introduce this overload --- too rare
@@ -228,8 +228,8 @@ public inline fun <T, K : Comparable<K>> List<T>.binarySearchBy(key: K?, fromInd
*
* @param comparison function that compares an element of the list with the element being searched.
*/
public fun <T> List<T>.binarySearch(fromIndex: Int = 0, toIndex: Int = size(), comparison: (T) -> Int): Int {
rangeCheck(size(), fromIndex, toIndex)
public fun <T> List<T>.binarySearch(fromIndex: Int = 0, toIndex: Int = size, comparison: (T) -> Int): Int {
rangeCheck(size, fromIndex, toIndex)
var low = fromIndex
var high = toIndex - 1
@@ -64,7 +64,7 @@ private class MapWithDefaultImpl<K, out V>(public override val map: Map<K,V>, pr
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
override val size: Int get() = map.size()
override val size: Int get() = map.size
override fun isEmpty(): Boolean = map.isEmpty()
override fun containsKey(key: K): Boolean = map.containsKey(key)
override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value)
@@ -80,7 +80,7 @@ private class MutableMapWithDefaultImpl<K, V>(public override val map: MutableMa
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
override val size: Int get() = map.size()
override val size: Int get() = map.size
override fun isEmpty(): Boolean = map.isEmpty()
override fun containsKey(key: K): Boolean = map.containsKey(key)
override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value)
@@ -34,7 +34,7 @@ public fun <K, V> emptyMap(): Map<K, V> = EmptyMap as Map<K, V>
*
* The returned map is serializable (JVM).
*/
public fun <K, V> mapOf(vararg values: Pair<K, V>): Map<K, V> = if (values.size() > 0) linkedMapOf(*values) else emptyMap()
public fun <K, V> mapOf(vararg values: Pair<K, V>): Map<K, V> = if (values.size > 0) linkedMapOf(*values) else emptyMap()
/** Returns an empty read-only map. The returned map is serializable (JVM). */
public fun <K, V> mapOf(): Map<K, V> = emptyMap()
@@ -53,7 +53,7 @@ public fun <K, V> mapOf(keyValuePair: Pair<K, V>): Map<K, V> = Collections.singl
* @sample test.collections.MapTest.createUsingPairs
*/
public fun <K, V> hashMapOf(vararg values: Pair<K, V>): HashMap<K, V> {
val answer = HashMap<K, V>(mapCapacity(values.size()))
val answer = HashMap<K, V>(mapCapacity(values.size))
answer.putAll(*values)
return answer
}
@@ -66,7 +66,7 @@ public fun <K, V> hashMapOf(vararg values: Pair<K, V>): HashMap<K, V> {
* @sample test.collections.MapTest.createLinkedMap
*/
public fun <K, V> linkedMapOf(vararg values: Pair<K, V>): LinkedHashMap<K, V> {
val answer = LinkedHashMap<K, V>(mapCapacity(values.size()))
val answer = LinkedHashMap<K, V>(mapCapacity(values.size))
answer.putAll(*values)
return answer
}
@@ -350,7 +350,7 @@ public fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V> {
* Returns a new map containing all key-value pairs from the given array of pairs.
*/
public fun <K, V> Array<Pair<K, V>>.toMap(): Map<K, V> {
val result = LinkedHashMap<K, V>(mapCapacity(size()))
val result = LinkedHashMap<K, V>(mapCapacity(size))
for (element in this) {
result.put(element.first, element.second)
}
@@ -21,11 +21,11 @@ package kotlin
import java.util.AbstractList
private open class ReversedListReadOnly<T>(protected open val delegate: List<T>) : AbstractList<T>() {
override val size: Int get() = delegate.size()
override val size: Int get() = delegate.size
override fun get(index: Int): T = delegate[index.flipIndex()]
protected fun Int.flipIndex(): Int = if (this in 0..size() - 1) size() - this - 1 else throw IndexOutOfBoundsException("index $this should be in range [${0..size() - 1}]")
protected fun Int.flipIndexForward(): Int = if (this in 0..size()) size() - this else throw IndexOutOfBoundsException("index $this should be in range [${0..size()}]")
protected fun Int.flipIndex(): Int = if (this in 0..size - 1) size - this - 1 else throw IndexOutOfBoundsException("index $this should be in range [${0..size - 1}]")
protected fun Int.flipIndexForward(): Int = if (this in 0..size) size - this else throw IndexOutOfBoundsException("index $this should be in range [${0..size}]")
}
private class ReversedList<T>(protected override val delegate: MutableList<T>) : ReversedListReadOnly<T>(delegate) {
@@ -26,17 +26,17 @@ internal object EmptySet : Set<Nothing>, Serializable {
/** Returns an empty read-only set. The returned set is serializable (JVM). */
public fun emptySet<T>(): Set<T> = EmptySet
/** Returns a new read-only ordered set with the given elements. The returned set is serializable (JVM). */
public fun setOf<T>(vararg values: T): Set<T> = if (values.size() > 0) values.toSet() else emptySet()
public fun setOf<T>(vararg values: T): Set<T> = if (values.size > 0) values.toSet() else emptySet()
/** Returns an empty read-only set. The returned set is serializable (JVM). */
public fun setOf<T>(): Set<T> = emptySet()
/** Returns a new [HashSet] with the given elements. */
public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(HashSet(mapCapacity(values.size())))
public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(HashSet(mapCapacity(values.size)))
/** Returns a new [LinkedHashSet] with the given elements. */
public fun linkedSetOf<T>(vararg values: T): LinkedHashSet<T> = values.toCollection(LinkedHashSet(mapCapacity(values.size())))
public fun linkedSetOf<T>(vararg values: T): LinkedHashSet<T> = values.toCollection(LinkedHashSet(mapCapacity(values.size)))
/** Returns this Set if it's not `null` and the empty set otherwise. */
public fun <T> Set<T>?.orEmpty(): Set<T> = this ?: emptySet()
@@ -23,7 +23,7 @@ private fun String.getRootName(): String {
// Note: separators should be already replaced to system ones
var first = indexOf(File.separatorChar, 0)
if (first == 0) {
if (length() > 1 && this[1] == File.separatorChar) {
if (length > 1 && this[1] == File.separatorChar) {
// Network names like //my.host/home/something ? => //my.host/home/ should be root
// NB: does not work in Unix because //my.host/home is converted into /my.host/home there
// So in Windows we'll have root of //my.host/home but in Unix just /
@@ -75,7 +75,7 @@ public val File.rootName: String
public val File.root: File?
get() {
val name = rootName
return if (name.length() > 0) File(name) else null
return if (name.length > 0) File(name) else null
}
/**
@@ -89,7 +89,7 @@ public data class FilePathComponents(public val rootName: String, public val fil
/**
* Returns the number of elements in the path to the file.
*/
public fun size(): Int = fileList.size()
public fun size(): Int = fileList.size
/**
* Returns a sub-path of the path, starting with the directory at the specified [beginIndex] and up
@@ -110,10 +110,10 @@ public data class FilePathComponents(public val rootName: String, public val fil
public fun File.filePathComponents(): FilePathComponents {
val path = separatorsToSystem()
val rootName = path.getRootName()
val subPath = path.substring(rootName.length())
val subPath = path.substring(rootName.length)
// if: a special case when we have only root component
// Split not only by / or \, but also by //, ///, \\, \\\, etc.
val list = if (rootName.length() > 0 && subPath.isEmpty()) listOf() else
val list = if (rootName.length > 0 && subPath.isEmpty()) listOf() else
// Looks awful but we split just by /+ or \+ depending on OS
subPath.split(Regex.fromLiteral(File.separatorChar.toString())).toList().map { it -> File(it) }
return FilePathComponents(rootName, list)
@@ -77,7 +77,7 @@ public class FileTreeWalk(private val start: File,
failed = true
}
}
if (fileList != null && fileIndex < fileList!!.size()) {
if (fileList != null && fileIndex < fileList!!.size) {
// First visit all files
return fileList!![fileIndex++]
} else if (!rootVisited) {
@@ -108,14 +108,14 @@ public class FileTreeWalk(private val start: File,
enter(rootDir)
rootVisited = true
return rootDir
} else if (fileList == null || fileIndex < fileList!!.size()) {
} else if (fileList == null || fileIndex < fileList!!.size) {
if (fileList == null) {
// Then read an array of files, if any
fileList = rootDir.listFiles()
if (fileList == null) {
fail(rootDir, AccessDeniedException(file = rootDir, reason = "Cannot list files in a directory"))
}
if (fileList == null || fileList!!.size() == 0) {
if (fileList == null || fileList!!.size == 0) {
leave(rootDir)
return null
}
@@ -175,7 +175,7 @@ public class FileTreeWalk(private val start: File,
// Check that file/directory matches the filter
if (!filter(file))
return gotoNext()
if (file == topState.rootDir || !file.isDirectory() || state.size() >= maxDepth) {
if (file == topState.rootDir || !file.isDirectory() || state.size >= maxDepth) {
// Proceed to a root directory or a simple file
return file
} else {
@@ -159,8 +159,8 @@ public fun File.relativePath(descendant: File): String {
val prefix = directory.canonicalPath
val answer = descendant.canonicalPath
return if (answer.startsWith(prefix)) {
val prefixSize = prefix.length()
if (answer.length() > prefixSize) {
val prefixSize = prefix.length
if (answer.length > prefixSize) {
answer.substring(prefixSize + 1)
} else ""
} else {
@@ -368,7 +368,7 @@ public fun File.normalize(): File {
when (name) {
"." -> {
}
".." -> if (!list.isEmpty() && list.get(list.size() - 1) != "..") list.remove(list.size() - 1) else list.add(name)
".." -> if (!list.isEmpty() && list.get(list.size - 1) != "..") list.remove(list.size - 1) else list.add(name)
else -> list.add(name)
}
}
+5 -5
View File
@@ -34,12 +34,12 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St
require(marginPrefix.isNotBlank()) { "marginPrefix should be non blank string but it is '$marginPrefix'" }
val lines = lines()
return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent), { line ->
return lines.reindent(length + newIndent.length * lines.size, getIndentFunction(newIndent), { line ->
val firstNonWhitespaceIndex = line.indexOfFirst { !it.isWhitespace() }
when {
firstNonWhitespaceIndex == -1 -> null
line.startsWith(marginPrefix, firstNonWhitespaceIndex) -> line.substring(firstNonWhitespaceIndex + marginPrefix.length())
line.startsWith(marginPrefix, firstNonWhitespaceIndex) -> line.substring(firstNonWhitespaceIndex + marginPrefix.length)
else -> null
}
})
@@ -80,7 +80,7 @@ public fun String.replaceIndent(newIndent: String = ""): String {
.map { it.indentWidth() }
.min() ?: 0
return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent), { line -> line.drop(minCommonIndent) })
return lines.reindent(length + newIndent.length * lines.size, getIndentFunction(newIndent), { line -> line.drop(minCommonIndent) })
}
/**
@@ -92,7 +92,7 @@ public fun String.prependIndent(indent: String = " "): String =
when {
it.isBlank() -> {
when {
it.length() < indent.length() -> indent
it.length < indent.length -> indent
else -> it
}
}
@@ -101,7 +101,7 @@ public fun String.prependIndent(indent: String = " "): String =
}
.joinToString("\n")
private fun String.indentWidth(): Int = indexOfFirst { !it.isWhitespace() }.let { if (it == -1) length() else it }
private fun String.indentWidth(): Int = indexOfFirst { !it.isWhitespace() }.let { if (it == -1) length else it }
private fun getIndentFunction(indent: String) = when {
indent.isEmpty() -> { line: String -> line }
+39 -39
View File
@@ -13,7 +13,7 @@ import kotlin.text.Regex
*/
inline public fun CharSequence.trim(predicate: (Char) -> Boolean): CharSequence {
var startIndex = 0
var endIndex = length() - 1
var endIndex = length - 1
var startFound = false
while (startIndex <= endIndex) {
@@ -206,17 +206,17 @@ public fun String.padEnd(length: Int, padChar: Char = ' '): String
/**
* Returns `true` if this nullable char sequence is either `null` or empty.
*/
public fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length() == 0
public fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length == 0
/**
* Returns `true` if this char sequence is empty (contains no characters).
*/
public fun CharSequence.isEmpty(): Boolean = length() == 0
public fun CharSequence.isEmpty(): Boolean = length == 0
/**
* Returns `true` if this char sequence is not empty.
*/
public fun CharSequence.isNotEmpty(): Boolean = length() > 0
public fun CharSequence.isNotEmpty(): Boolean = length > 0
// implemented differently in JVM and JS
//public fun String.isBlank(): Boolean = length() == 0 || all { it.isWhitespace() }
@@ -240,7 +240,7 @@ public operator fun CharSequence.iterator(): CharIterator = object : CharIterato
public override fun nextChar(): Char = get(index++)
public override fun hasNext(): Boolean = index < length()
public override fun hasNext(): Boolean = index < length
}
/** Returns the string if it is not `null`, or the empty string otherwise. */
@@ -250,13 +250,13 @@ public fun String?.orEmpty(): String = this ?: ""
* Returns the range of valid character indices for this char sequence.
*/
public val CharSequence.indices: IntRange
get() = 0..length() - 1
get() = 0..length - 1
/**
* Returns the index of the last character in the char sequence or -1 if it is empty.
*/
public val CharSequence.lastIndex: Int
get() = this.length() - 1
get() = this.length - 1
/**
* Returns a character at the given index in a [CharSequence]. Allows to use the
@@ -271,7 +271,7 @@ public operator fun CharSequence.get(index: Int): Char = this.get(index)
* Returns `true` if this CharSequence has Unicode surrogate pair at the specified [index].
*/
public fun CharSequence.hasSurrogatePairAt(index: Int): Boolean {
return index in 0..length() - 2
return index in 0..length - 2
&& this[index].isHighSurrogate()
&& this[index + 1].isLowSurrogate()
}
@@ -320,7 +320,7 @@ public fun String.substringBefore(delimiter: String, missingDelimiterValue: Stri
*/
public fun String.substringAfter(delimiter: Char, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + 1, length())
return if (index == -1) missingDelimiterValue else substring(index + 1, length)
}
/**
@@ -329,7 +329,7 @@ public fun String.substringAfter(delimiter: Char, missingDelimiterValue: String
*/
public fun String.substringAfter(delimiter: String, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + delimiter.length(), length())
return if (index == -1) missingDelimiterValue else substring(index + delimiter.length, length)
}
/**
@@ -356,7 +356,7 @@ public fun String.substringBeforeLast(delimiter: String, missingDelimiterValue:
*/
public fun String.substringAfterLast(delimiter: Char, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + 1, length())
return if (index == -1) missingDelimiterValue else substring(index + 1, length)
}
/**
@@ -365,7 +365,7 @@ public fun String.substringAfterLast(delimiter: Char, missingDelimiterValue: Str
*/
public fun String.substringAfterLast(delimiter: String, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else substring(index + delimiter.length(), length())
return if (index == -1) missingDelimiterValue else substring(index + delimiter.length, length)
}
/**
@@ -380,7 +380,7 @@ public fun CharSequence.replaceRange(firstIndex: Int, lastIndex: Int, replacemen
val sb = StringBuilder()
sb.append(this, 0, firstIndex)
sb.append(replacement)
sb.append(this, lastIndex, length())
sb.append(this, lastIndex, length)
return sb
}
@@ -424,9 +424,9 @@ public fun CharSequence.removeRange(firstIndex: Int, lastIndex: Int): CharSequen
if (lastIndex == firstIndex)
return this.subSequence(0, length)
val sb = StringBuilder(length() - (lastIndex - firstIndex))
val sb = StringBuilder(length - (lastIndex - firstIndex))
sb.append(this, 0, firstIndex)
sb.append(this, lastIndex, length())
sb.append(this, lastIndex, length)
return sb
}
@@ -560,7 +560,7 @@ public fun String.replaceBefore(delimiter: String, replacement: String, missingD
*/
public fun String.replaceAfter(delimiter: Char, replacement: String, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length(), replacement)
return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length, replacement)
}
/**
@@ -569,7 +569,7 @@ public fun String.replaceAfter(delimiter: Char, replacement: String, missingDeli
*/
public fun String.replaceAfter(delimiter: String, replacement: String, missingDelimiterValue: String = this): String {
val index = indexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length(), length(), replacement)
return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length, length, replacement)
}
/**
@@ -578,7 +578,7 @@ public fun String.replaceAfter(delimiter: String, replacement: String, missingDe
*/
public fun String.replaceAfterLast(delimiter: String, replacement: String, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length(), length(), replacement)
return if (index == -1) missingDelimiterValue else replaceRange(index + delimiter.length, length, replacement)
}
/**
@@ -587,7 +587,7 @@ public fun String.replaceAfterLast(delimiter: String, replacement: String, missi
*/
public fun String.replaceAfterLast(delimiter: Char, replacement: String, missingDelimiterValue: String = this): String {
val index = lastIndexOf(delimiter)
return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length(), replacement)
return if (index == -1) missingDelimiterValue else replaceRange(index + 1, length, replacement)
}
/**
@@ -662,13 +662,13 @@ internal fun CharSequence.regionMatchesImpl(thisOffset: Int, other: CharSequence
* Returns `true` if this char sequence starts with the specified character.
*/
public fun CharSequence.startsWith(char: Char, ignoreCase: Boolean = false): Boolean =
this.length() > 0 && this[0].equals(char, ignoreCase)
this.length > 0 && this[0].equals(char, ignoreCase)
/**
* Returns `true` if this char sequence ends with the specified character.
*/
public fun CharSequence.endsWith(char: Char, ignoreCase: Boolean = false): Boolean =
this.length() > 0 && this[lastIndex].equals(char, ignoreCase)
this.length > 0 && this[lastIndex].equals(char, ignoreCase)
/**
* Returns `true` if this char sequence starts with the specified prefix.
@@ -677,7 +677,7 @@ public fun CharSequence.startsWith(prefix: CharSequence, ignoreCase: Boolean = f
if (!ignoreCase && this is String && prefix is String)
return this.startsWith(prefix)
else
return regionMatchesImpl(0, prefix, 0, prefix.length(), ignoreCase)
return regionMatchesImpl(0, prefix, 0, prefix.length, ignoreCase)
}
/**
@@ -687,7 +687,7 @@ public fun CharSequence.startsWith(prefix: CharSequence, thisOffset: Int, ignore
if (!ignoreCase && this is String && prefix is String)
return this.startsWith(prefix, thisOffset)
else
return regionMatchesImpl(thisOffset, prefix, 0, prefix.length(), ignoreCase)
return regionMatchesImpl(thisOffset, prefix, 0, prefix.length, ignoreCase)
}
/**
@@ -697,7 +697,7 @@ public fun CharSequence.endsWith(suffix: CharSequence, ignoreCase: Boolean = fal
if (!ignoreCase && this is String && suffix is String)
return this.endsWith(suffix)
else
return regionMatchesImpl(length() - suffix.length(), suffix, 0, suffix.length(), ignoreCase)
return regionMatchesImpl(length - suffix.length, suffix, 0, suffix.length, ignoreCase)
}
@@ -711,7 +711,7 @@ public fun CharSequence.endsWith(suffix: CharSequence, ignoreCase: Boolean = fal
* @param ignoreCase `true` to ignore character case when matching a character. By default `false`.
*/
public fun CharSequence.commonPrefixWith(other: CharSequence, ignoreCase: Boolean = false): String {
val shortestLength = Math.min(this.length(), other.length())
val shortestLength = Math.min(this.length, other.length)
var i = 0
while (i < shortestLength && this[i].equals(other[i], ignoreCase = ignoreCase)) {
@@ -731,8 +731,8 @@ public fun CharSequence.commonPrefixWith(other: CharSequence, ignoreCase: Boolea
* @param ignoreCase `true` to ignore character case when matching a character. By default `false`.
*/
public fun CharSequence.commonSuffixWith(other: CharSequence, ignoreCase: Boolean = false): String {
val thisLength = this.length()
val otherLength = other.length()
val thisLength = this.length
val otherLength = other.length
val shortestLength = Math.min(thisLength, otherLength)
var i = 0
@@ -749,7 +749,7 @@ public fun CharSequence.commonSuffixWith(other: CharSequence, ignoreCase: Boolea
// indexOfAny()
private fun CharSequence.findAnyOf(chars: CharArray, startIndex: Int, ignoreCase: Boolean, last: Boolean): Pair<Int, Char>? {
if (!ignoreCase && chars.size() == 1 && this is String) {
if (!ignoreCase && chars.size == 1 && this is String) {
val char = chars.single()
val index = if (!last) nativeIndexOf(char, startIndex) else nativeLastIndexOf(char, startIndex)
return if (index < 0) null else index to char
@@ -792,18 +792,18 @@ public fun CharSequence.lastIndexOfAny(chars: CharArray, startIndex: Int = lastI
private fun CharSequence.indexOf(other: CharSequence, startIndex: Int, endIndex: Int, ignoreCase: Boolean, last: Boolean = false): Int {
val indices = if (!last)
startIndex.coerceAtLeast(0)..endIndex.coerceAtMost(length())
startIndex.coerceAtLeast(0)..endIndex.coerceAtMost(length)
else
startIndex.coerceAtMost(lastIndex) downTo endIndex.coerceAtLeast(0)
if (this is String && other is String) { // smart cast
for (index in indices) {
if (other.regionMatches(0, this, index, other.length(), ignoreCase))
if (other.regionMatches(0, this, index, other.length, ignoreCase))
return index
}
} else {
for (index in indices) {
if (other.regionMatchesImpl(0, this, index, other.length(), ignoreCase))
if (other.regionMatchesImpl(0, this, index, other.length, ignoreCase))
return index
}
}
@@ -817,17 +817,17 @@ private fun CharSequence.findAnyOf(strings: Collection<String>, startIndex: Int,
return if (index < 0) null else index to string
}
val indices = if (!last) startIndex.coerceAtLeast(0)..length() else startIndex.coerceAtMost(lastIndex) downTo 0
val indices = if (!last) startIndex.coerceAtLeast(0)..length else startIndex.coerceAtMost(lastIndex) downTo 0
if (this is String) {
for (index in indices) {
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length(), ignoreCase) }
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length, ignoreCase) }
if (matchingString != null)
return index to matchingString
}
} else {
for (index in indices) {
val matchingString = strings.firstOrNull { it.regionMatchesImpl(0, this, index, it.length(), ignoreCase) }
val matchingString = strings.firstOrNull { it.regionMatchesImpl(0, this, index, it.length, ignoreCase) }
if (matchingString != null)
return index to matchingString
}
@@ -920,7 +920,7 @@ public fun CharSequence.indexOf(char: Char, startIndex: Int = 0, ignoreCase: Boo
*/
public fun CharSequence.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int {
return if (ignoreCase || this !is String)
indexOf(string, startIndex, length(), ignoreCase)
indexOf(string, startIndex, length, ignoreCase)
else
nativeIndexOf(string, startIndex)
}
@@ -964,7 +964,7 @@ public operator fun CharSequence.contains(other: CharSequence, ignoreCase: Boole
if (other is String)
indexOf(other, ignoreCase = ignoreCase) >= 0
else
indexOf(other, 0, length(), ignoreCase) >= 0
indexOf(other, 0, length, ignoreCase) >= 0
@@ -989,7 +989,7 @@ private class DelimitedRangesSequence(private val input: CharSequence, private v
override fun iterator(): Iterator<IntRange> = object : Iterator<IntRange> {
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
var currentStartIndex: Int = Math.min(Math.max(startIndex, 0), input.length())
var currentStartIndex: Int = Math.min(Math.max(startIndex, 0), input.length)
var nextSearchIndex: Int = currentStartIndex
var nextItem: IntRange? = null
var counter: Int = 0
@@ -1000,7 +1000,7 @@ private class DelimitedRangesSequence(private val input: CharSequence, private v
nextItem = null
}
else {
if (limit > 0 && ++counter >= limit || nextSearchIndex > input.length()) {
if (limit > 0 && ++counter >= limit || nextSearchIndex > input.length) {
nextItem = currentStartIndex..input.lastIndex
nextSearchIndex = -1
}
@@ -1076,7 +1076,7 @@ private fun CharSequence.rangesDelimitedBy(delimiters: Array<out String>, startI
require(limit >= 0, { "Limit must be non-negative, but was $limit" } )
val delimitersList = delimiters.asList()
return DelimitedRangesSequence(this, startIndex, limit, { startIndex -> findAnyOf(delimitersList, startIndex, ignoreCase = ignoreCase, last = false)?.let { it.first to it.second.length ()} })
return DelimitedRangesSequence(this, startIndex, limit, { startIndex -> findAnyOf(delimitersList, startIndex, ignoreCase = ignoreCase, last = false)?.let { it.first to it.second.length } })
}
@@ -75,7 +75,7 @@ public fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean
*/
public fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String {
val index = indexOf(oldValue, ignoreCase = ignoreCase)
return if (index < 0) this else this.replaceRange(index, index + oldValue.length(), newValue)
return if (index < 0) this else this.replaceRange(index, index + oldValue.length, newValue)
}
/**
@@ -134,7 +134,7 @@ public fun String.startsWith(prefix: String, ignoreCase: Boolean = false): Boole
if (!ignoreCase)
return (this as java.lang.String).startsWith(prefix)
else
return regionMatches(0, prefix, 0, prefix.length(), ignoreCase)
return regionMatches(0, prefix, 0, prefix.length, ignoreCase)
}
/**
@@ -144,7 +144,7 @@ public fun String.startsWith(prefix: String, thisOffset: Int, ignoreCase: Boolea
if (!ignoreCase)
return (this as java.lang.String).startsWith(prefix, thisOffset)
else
return regionMatches(thisOffset, prefix, 0, prefix.length(), ignoreCase)
return regionMatches(thisOffset, prefix, 0, prefix.length, ignoreCase)
}
/**
@@ -154,7 +154,7 @@ public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean
if (!ignoreCase)
return (this as java.lang.String).endsWith(suffix)
else
return regionMatches(length() - suffix.length(), suffix, 0, suffix.length(), ignoreCase = true)
return regionMatches(length - suffix.length, suffix, 0, suffix.length, ignoreCase = true)
}
// "constructors" for String
@@ -292,7 +292,7 @@ public fun String.intern(): String = (this as java.lang.String).intern()
/**
* Returns `true` if this string is empty or consists solely of whitespace characters.
*/
public fun CharSequence.isBlank(): Boolean = length() == 0 || indices.all { this[it].isWhitespace() }
public fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }
/**
* Returns the index within this string that is offset from the given [index] by [codePointOffset] code points.
@@ -440,7 +440,7 @@ public fun CharSequence.repeat(n: Int): String {
if (n < 0)
throw IllegalArgumentException("Value should be non-negative, but was $n")
val sb = StringBuilder(n * length())
val sb = StringBuilder(n * length)
for (i in 1..n) {
sb.append(this)
}
@@ -162,7 +162,7 @@ public class Regex internal constructor(private val nativePattern: Pattern) {
var match: MatchResult? = find(input) ?: return input.toString()
var lastStart = 0
val length = input.length()
val length = input.length
val sb = StringBuilder(length)
do {
val foundMatch = match!!
@@ -256,7 +256,7 @@ private class MatcherMatchResult(private val matcher: Matcher, private val input
override fun next(): MatchResult? {
val nextIndex = matchResult.end() + if (matchResult.end() == matchResult.start()) 1 else 0
return if (nextIndex <= input.length()) matcher.findNext(nextIndex, input) else null
return if (nextIndex <= input.length) matcher.findNext(nextIndex, input) else null
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ import java.util.Comparator
* compare as equal, the result of that comparison is returned.
*/
public fun <T> compareValuesBy(a: T, b: T, vararg selectors: (T) -> Comparable<*>?): Int {
require(selectors.size() > 0)
require(selectors.size > 0)
for (fn in selectors) {
val v1 = fn(a)
val v2 = fn(b)