All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generated._Strings.kt Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("StringsKt")

package kotlin

//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//

import java.util.*

import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js

/**
 * Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence.
 */
public fun CharSequence.elementAt(index: Int): Char {
    return get(index)
}

/**
 * Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.elementAt(index: Int): Char {
    return get(index)
}

/**
 * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
 */
public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}

/**
 * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}

/**
 * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence.
 */
public fun CharSequence.elementAtOrNull(index: Int): Char? {
    return if (index >= 0 && index <= lastIndex) get(index) else null
}

/**
 * Returns a character at the given [index] or `null` if the [index] is out of bounds of this string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.elementAtOrNull(index: Int): Char? {
    return if (index >= 0 && index <= lastIndex) get(index) else null
}

/**
 * Returns the first character matching the given [predicate], or `null` if no such character was found.
 */
public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? {
    return firstOrNull(predicate)
}

/**
 * Returns the first character matching the given [predicate], or `null` if no such character was found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.find(predicate: (Char) -> Boolean): Char? {
    return firstOrNull(predicate)
}

/**
 * Returns the last character matching the given [predicate], or `null` if no such character was found.
 */
public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
    return lastOrNull(predicate)
}

/**
 * Returns the last character matching the given [predicate], or `null` if no such character was found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.findLast(predicate: (Char) -> Boolean): Char? {
    return lastOrNull(predicate)
}

/**
 * Returns first character.
 * @throws [NoSuchElementException] if the char sequence is empty.
 */
public fun CharSequence.first(): Char {
    if (isEmpty())
        throw NoSuchElementException("Collection is empty.")
    return this[0]
}

/**
 * Returns first character.
 * @throws [NoSuchElementException] if the string is empty.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.first(): Char {
    if (isEmpty())
        throw NoSuchElementException("Collection is empty.")
    return this[0]
}

/**
 * Returns the first character matching the given [predicate].
 * @throws [NoSuchElementException] if no such character is found.
 */
public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
    for (element in this) if (predicate(element)) return element
    throw NoSuchElementException("No element matching predicate was found.")
}

/**
 * Returns the first character matching the given [predicate].
 * @throws [NoSuchElementException] if no such character is found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.first(predicate: (Char) -> Boolean): Char {
    for (element in this) if (predicate(element)) return element
    throw NoSuchElementException("No element matching predicate was found.")
}

/**
 * Returns the first character, or `null` if the char sequence is empty.
 */
public fun CharSequence.firstOrNull(): Char? {
    return if (isEmpty()) null else this[0]
}

/**
 * Returns the first character, or `null` if the string is empty.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.firstOrNull(): Char? {
    return if (isEmpty()) null else this[0]
}

/**
 * Returns the first character matching the given [predicate], or `null` if character was not found.
 */
public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? {
    for (element in this) if (predicate(element)) return element
    return null
}

/**
 * Returns the first character matching the given [predicate], or `null` if character was not found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? {
    for (element in this) if (predicate(element)) return element
    return null
}

/**
 * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
 */
public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}

/**
 * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}

/**
 * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence.
 */
public fun CharSequence.getOrNull(index: Int): Char? {
    return if (index >= 0 && index <= lastIndex) get(index) else null
}

/**
 * Returns a character at the given [index] or `null` if the [index] is out of bounds of this string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.getOrNull(index: Int): Char? {
    return if (index >= 0 && index <= lastIndex) get(index) else null
}

/**
 * Returns index of the first character matching the given [predicate], or -1 if the char sequence does not contain such character.
 */
public inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int {
    for (index in indices) {
        if (predicate(this[index])) {
            return index
        }
    }
    return -1
}

/**
 * Returns index of the first character matching the given [predicate], or -1 if the string does not contain such character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int {
    for (index in indices) {
        if (predicate(this[index])) {
            return index
        }
    }
    return -1
}

/**
 * Returns index of the last character matching the given [predicate], or -1 if the char sequence does not contain such character.
 */
public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int {
    for (index in indices.reversed()) {
        if (predicate(this[index])) {
            return index
        }
    }
    return -1
}

/**
 * Returns index of the last character matching the given [predicate], or -1 if the string does not contain such character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int {
    for (index in indices.reversed()) {
        if (predicate(this[index])) {
            return index
        }
    }
    return -1
}

/**
 * Returns the last character.
 * @throws [NoSuchElementException] if the char sequence is empty.
 */
public fun CharSequence.last(): Char {
    if (isEmpty())
        throw NoSuchElementException("Collection is empty.")
    return this[lastIndex]
}

/**
 * Returns the last character.
 * @throws [NoSuchElementException] if the string is empty.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.last(): Char {
    if (isEmpty())
        throw NoSuchElementException("Collection is empty.")
    return this[lastIndex]
}

/**
 * Returns the last character matching the given [predicate].
 * @throws [NoSuchElementException] if no such character is found.
 */
public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
    for (index in this.indices.reversed()) {
        val element = this[index]
        if (predicate(element)) return element
    }
    throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
}

/**
 * Returns the last character matching the given [predicate].
 * @throws [NoSuchElementException] if no such character is found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.last(predicate: (Char) -> Boolean): Char {
    for (index in this.indices.reversed()) {
        val element = this[index]
        if (predicate(element)) return element
    }
    throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
}

/**
 * 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]
}

/**
 * Returns the last character, or `null` if the string is empty.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.lastOrNull(): Char? {
    return if (isEmpty()) null else this[length() - 1]
}

/**
 * Returns the last character matching the given [predicate], or `null` if no such character was found.
 */
public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? {
    for (index in this.indices.reversed()) {
        val element = this[index]
        if (predicate(element)) return element
    }
    return null
}

/**
 * Returns the last character matching the given [predicate], or `null` if no such character was found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? {
    for (index in this.indices.reversed()) {
        val element = this[index]
        if (predicate(element)) return element
    }
    return null
}

/**
 * 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()) {
        0 -> throw NoSuchElementException("Collection is empty.")
        1 -> this[0]
        else -> throw IllegalArgumentException("Collection has more than one element.")
    }
}

/**
 * Returns the single character, or throws an exception if the string is empty or has more than one character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.single(): Char {
    return when (length()) {
        0 -> throw NoSuchElementException("Collection is empty.")
        1 -> this[0]
        else -> throw IllegalArgumentException("Collection has more than one element.")
    }
}

/**
 * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character.
 */
public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char {
    var single: Char? = null
    var found = false
    for (element in this) {
        if (predicate(element)) {
            if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
            single = element
            found = true
        }
    }
    if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
    return single as Char
}

/**
 * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.single(predicate: (Char) -> Boolean): Char {
    var single: Char? = null
    var found = false
    for (element in this) {
        if (predicate(element)) {
            if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
            single = element
            found = true
        }
    }
    if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
    return single as 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
}

/**
 * Returns single character, or `null` if the string is empty or has more than one character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.singleOrNull(): Char? {
    return if (length() == 1) this[0] else null
}

/**
 * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found.
 */
public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? {
    var single: Char? = null
    var found = false
    for (element in this) {
        if (predicate(element)) {
            if (found) return null
            single = element
            found = true
        }
    }
    if (!found) return null
    return single
}

/**
 * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? {
    var single: Char? = null
    var found = false
    for (element in this) {
        if (predicate(element)) {
            if (found) return null
            single = element
            found = true
        }
    }
    if (!found) return null
    return single
}

/**
 * Returns a subsequence of this char sequence with the first [n] characters removed.
 */
public fun CharSequence.drop(n: Int): CharSequence {
    require(n >= 0, { "Requested character count $n is less than zero." })
    return subSequence(n.coerceAtMost(length), length)
}

/**
 * Returns a string with the first [n] characters removed.
 */
public fun String.drop(n: Int): String {
    require(n >= 0, { "Requested character count $n is less than zero." })
    return substring(n.coerceAtMost(length))
}

/**
 * Returns a subsequence of this char sequence with the last [n] characters removed.
 */
public fun CharSequence.dropLast(n: Int): CharSequence {
    require(n >= 0, { "Requested character count $n is less than zero." })
    return take((length - n).coerceAtLeast(0))
}

/**
 * Returns a string with the last [n] characters removed.
 */
public fun String.dropLast(n: Int): String {
    require(n >= 0, { "Requested character count $n is less than zero." })
    return take((length - n).coerceAtLeast(0))
}

/**
 * Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate].
 */
public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence {
    for (index in this.indices.reversed())
        if (!predicate(this[index]))
            return subSequence(0, index + 1)
    return ""
}

/**
 * Returns a string containing all characters except last characters that satisfy the given [predicate].
 */
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
    for (index in this.indices.reversed())
        if (!predicate(this[index]))
            return substring(0, index + 1)
    return ""
}

/**
 * Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate].
 */
public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence {
    for (index in this.indices)
        if (!predicate(this[index]))
            return subSequence(index, length)
    return ""
}

/**
 * Returns a string containing all characters except first characters that satisfy the given [predicate].
 */
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
    for (index in this.indices)
        if (!predicate(this[index]))
            return substring(index)
    return ""
}

/**
 * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate].
 */
public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence {
    return filterTo(StringBuilder(), predicate)
}

/**
 * Returns a string containing only those characters from the original string that match the given [predicate].
 */
public inline fun String.filter(predicate: (Char) -> Boolean): String {
    return filterTo(StringBuilder(), predicate).toString()
}

/**
 * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate].
 */
public inline fun CharSequence.filterIndexed(predicate: (Int, Char) -> Boolean): CharSequence {
    return filterIndexedTo(StringBuilder(), predicate)
}

/**
 * Returns a string containing only those characters from the original string that match the given [predicate].
 */
public inline fun String.filterIndexed(predicate: (Int, Char) -> Boolean): String {
    return filterIndexedTo(StringBuilder(), predicate).toString()
}

/**
 * Appends all characters matching the given [predicate] to the given [destination].
 */
public inline fun  CharSequence.filterIndexedTo(destination: C, predicate: (Int, Char) -> Boolean): C {
    forEachIndexed { index, element ->
        if (predicate(index, element)) destination.append(element)
    }
    return destination
}

/**
 * Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate].
 */
public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence {
    return filterNotTo(StringBuilder(), predicate)
}

/**
 * Returns a string containing only those characters from the original string that do not match the given [predicate].
 */
public inline fun String.filterNot(predicate: (Char) -> Boolean): String {
    return filterNotTo(StringBuilder(), predicate).toString()
}

/**
 * Appends all characters not matching the given [predicate] to the given [destination].
 */
public inline fun  CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C {
    for (element in this) if (!predicate(element)) destination.append(element)
    return destination
}

/**
 * Appends all elements not matching the given [predicate] to the given [destination].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.filterNotTo(destination: C, predicate: (Char) -> Boolean): C {
    for (element in this) if (!predicate(element)) destination.append(element)
    return destination
}

/**
 * Appends all characters matching the given [predicate] to the given [destination].
 */
public inline fun  CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C {
    for (index in 0..length() - 1) {
        val element = get(index)
        if (predicate(element)) destination.append(element)
    }
    return destination
}

/**
 * Appends all characters matching the given [predicate] to the given [destination].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.filterTo(destination: C, predicate: (Char) -> Boolean): C {
    for (index in 0..length() - 1) {
        val element = get(index)
        if (predicate(element)) destination.append(element)
    }
    return destination
}

/**
 * Returns a char sequence containing characters of the original char sequence at the specified range of [indices].
 */
public fun CharSequence.slice(indices: IntRange): CharSequence {
    if (indices.isEmpty()) return ""
    return subSequence(indices)
}

/**
 * Returns a string containing characters of the original string at the specified range of [indices].
 */
public fun String.slice(indices: IntRange): String {
    if (indices.isEmpty()) return ""
    return substring(indices)
}

/**
 * Returns a char sequence containing characters of the original char sequence at specified [indices].
 */
public fun CharSequence.slice(indices: Iterable): CharSequence {
    val size = indices.collectionSizeOrDefault(10)
    if (size == 0) return ""
    val result = StringBuilder(size)
    for (i in indices) {
        result.append(get(i))
    }
    return result
}

/**
 * Returns a string containing characters of the original string at specified [indices].
 */
public fun String.slice(indices: Iterable): String {
    val size = indices.collectionSizeOrDefault(10)
    if (size == 0) return ""
    val result = StringBuilder(size)
    for (i in indices) {
        result.append(get(i))
    }
    return result.toString()
}

/**
 * Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter.
 */
public fun CharSequence.take(n: Int): CharSequence {
    require(n >= 0, { "Requested character count $n is less than zero." })
    return subSequence(0, n.coerceAtMost(length))
}

/**
 * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter.
 */
public fun String.take(n: Int): String {
    require(n >= 0, { "Requested character count $n is less than zero." })
    return substring(0, n.coerceAtMost(length))
}

/**
 * Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter.
 */
public fun CharSequence.takeLast(n: Int): CharSequence {
    require(n >= 0, { "Requested character count $n is less than zero." })
    val length = length
    return subSequence(length - n.coerceAtMost(length), length)
}

/**
 * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter.
 */
public fun String.takeLast(n: Int): String {
    require(n >= 0, { "Requested character count $n is less than zero." })
    val length = length
    return substring(length - n.coerceAtMost(length))
}

/**
 * Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate].
 */
public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence {
    for (index in lastIndex downTo 0) {
        if (!predicate(this[index])) {
            return subSequence(index + 1, length)
        }
    }
    return subSequence(0, length)
}

/**
 * Returns a string containing last characters that satisfy the given [predicate].
 */
public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String {
    for (index in lastIndex downTo 0) {
        if (!predicate(this[index])) {
            return substring(index + 1)
        }
    }
    return this
}

/**
 * Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate].
 */
public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence {
    for (index in 0..length - 1)
        if (!predicate(get(index))) {
            return subSequence(0, index)
        }
    return subSequence(0, length)
}

/**
 * Returns a string containing the first characters that satisfy the given [predicate].
 */
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
    for (index in 0..length - 1)
        if (!predicate(get(index))) {
            return substring(0, index)
        }
    return this
}

/**
 * Returns a char sequence with characters in reversed order.
 */
public fun CharSequence.reversed(): CharSequence {
    return StringBuilder(this).reverse()
}

/**
 * Returns a string with characters in reversed order.
 */
public fun String.reversed(): String {
    return StringBuilder(this).reverse().toString()
}

/**
 * Returns an [ArrayList] of all characters.
 */
public fun CharSequence.toArrayList(): ArrayList {
    return toCollection(ArrayList(length()))
}

/**
 * Returns an [ArrayList] of all characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toArrayList(): ArrayList {
    return toCollection(ArrayList(length()))
}

/**
 * Appends all characters to the given [collection].
 */
public fun > CharSequence.toCollection(collection: C): C {
    for (item in this) {
        collection.add(item)
    }
    return collection
}

/**
 * Appends all characters to the given [collection].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun > String.toCollection(collection: C): C {
    for (item in this) {
        collection.add(item)
    }
    return collection
}

/**
 * Returns a [HashSet] of all characters.
 */
public fun CharSequence.toHashSet(): HashSet {
    return toCollection(HashSet(mapCapacity(length())))
}

/**
 * Returns a [HashSet] of all characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toHashSet(): HashSet {
    return toCollection(HashSet(mapCapacity(length())))
}

/**
 * Returns a [LinkedList] containing all elements.
 */
@Deprecated("Use toCollection(LinkedList()) instead.", ReplaceWith("toCollection(LinkedList())"))
public fun String.toLinkedList(): LinkedList {
    return toCollection(LinkedList())
}

/**
 * Returns a [List] containing all characters.
 */
public fun CharSequence.toList(): List {
    return this.toArrayList()
}

/**
 * Returns a [List] containing all characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toList(): List {
    return this.toArrayList()
}

@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun  CharSequence.toMap(selector: (Char) -> K): Map {
    return toMapBy(selector)
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.toMap(selector: (Char) -> K): Map {
    return toMapBy(selector)
}

/**
 * Returns Map containing the values provided by [transform] and indexed by [selector] functions applied to characters of the given char sequence.
 * If any two characters would have the same key returned by [selector] the last one gets added to the map.
 */
public inline fun  CharSequence.toMap(selector: (Char) -> K, transform: (Char) -> V): Map {
    val capacity = (length()/.75f) + 1
    val result = LinkedHashMap(Math.max(capacity.toInt(), 16))
    for (element in this) {
        result.put(selector(element), transform(element))
    }
    return result
}

/**
 * Returns Map containing the values provided by [transform] and indexed by [selector] functions applied to characters of the given string.
 * If any two characters would have the same key returned by [selector] the last one gets added to the map.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map {
    val capacity = (length()/.75f) + 1
    val result = LinkedHashMap(Math.max(capacity.toInt(), 16))
    for (element in this) {
        result.put(selector(element), transform(element))
    }
    return result
}

/**
 * Returns Map containing the characters from the given char sequence indexed by the key
 * returned from [selector] function applied to each character.
 * If any two characters would have the same key returned by [selector] the last one gets added to the map.
 */
public inline fun  CharSequence.toMapBy(selector: (Char) -> K): Map {
    val capacity = (length()/.75f) + 1
    val result = LinkedHashMap(Math.max(capacity.toInt(), 16))
    for (element in this) {
        result.put(selector(element), element)
    }
    return result
}

/**
 * Returns Map containing the characters from the given string indexed by the key
 * returned from [selector] function applied to each character.
 * If any two characters would have the same key returned by [selector] the last one gets added to the map.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.toMapBy(selector: (Char) -> K): Map {
    val capacity = (length()/.75f) + 1
    val result = LinkedHashMap(Math.max(capacity.toInt(), 16))
    for (element in this) {
        result.put(selector(element), element)
    }
    return result
}

/**
 * Returns a [Set] of all characters.
 */
public fun CharSequence.toSet(): Set {
    return toCollection(LinkedHashSet(mapCapacity(length())))
}

/**
 * Returns a [Set] of all characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toSet(): Set {
    return toCollection(LinkedHashSet(mapCapacity(length())))
}

/**
 * Returns a [SortedSet] of all characters.
 */
public fun CharSequence.toSortedSet(): SortedSet {
    return toCollection(TreeSet())
}

/**
 * Returns a [SortedSet] of all characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.toSortedSet(): SortedSet {
    return toCollection(TreeSet())
}

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each character of original char sequence.
 */
public inline fun  CharSequence.flatMap(transform: (Char) -> Iterable): List {
    return flatMapTo(ArrayList(), transform)
}

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each character of original string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.flatMap(transform: (Char) -> Iterable): List {
    return flatMapTo(ArrayList(), transform)
}

/**
 * Appends all elements yielded from results of [transform] function being invoked on each character of original char sequence, to the given [destination].
 */
public inline fun > CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable): C {
    for (element in this) {
        val list = transform(element)
        destination.addAll(list)
    }
    return destination
}

/**
 * Appends all elements yielded from results of [transform] function being invoked on each character of original string, to the given [destination].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun > String.flatMapTo(destination: C, transform: (Char) -> Iterable): C {
    for (element in this) {
        val list = transform(element)
        destination.addAll(list)
    }
    return destination
}

/**
 * Returns a map of the characters in original char sequence grouped by the result of given [toKey] function.
 */
public inline fun  CharSequence.groupBy(toKey: (Char) -> K): Map> {
    return groupByTo(LinkedHashMap>(), toKey)
}

/**
 * Returns a map of the characters in original string grouped by the result of given [toKey] function.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.groupBy(toKey: (Char) -> K): Map> {
    return groupByTo(LinkedHashMap>(), toKey)
}

/**
 * Appends characters from original char sequence grouped by the result of given [toKey] function to the given [map].
 */
public inline fun  CharSequence.groupByTo(map: MutableMap>, toKey: (Char) -> K): Map> {
    for (element in this) {
        val key = toKey(element)
        val list = map.getOrPut(key) { ArrayList() }
        list.add(element)
    }
    return map
}

/**
 * Appends characters from original string grouped by the result of given [toKey] function to the given [map].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.groupByTo(map: MutableMap>, toKey: (Char) -> K): Map> {
    for (element in this) {
        val key = toKey(element)
        val list = map.getOrPut(key) { ArrayList() }
        list.add(element)
    }
    return map
}

/**
 * Returns a list containing the results of applying the given [transform] function
 * to each character in the original char sequence.
 */
public inline fun  CharSequence.map(transform: (Char) -> R): List {
    return mapTo(ArrayList(length()), transform)
}

/**
 * Returns a list containing the results of applying the given [transform] function
 * to each character in the original string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.map(transform: (Char) -> R): List {
    return mapTo(ArrayList(length()), transform)
}

/**
 * Returns a list containing the results of applying the given [transform] function
 * to each character and its index in the original char sequence.
 */
public inline fun  CharSequence.mapIndexed(transform: (Int, Char) -> R): List {
    return mapIndexedTo(ArrayList(length()), transform)
}

/**
 * Returns a list containing the results of applying the given [transform] function
 * to each character and its index in the original string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.mapIndexed(transform: (Int, Char) -> R): List {
    return mapIndexedTo(ArrayList(length()), transform)
}

/**
 * Returns a list containing only the non-null results of applying the given [transform] function
 * to each character and its index in the original char sequence.
 */
public inline fun  CharSequence.mapIndexedNotNull(transform: (Int, Char) -> R?): List {
    return mapIndexedNotNullTo(ArrayList(), transform)
}

/**
 * Applies the given [transform] function to each character and its index in the original char sequence
 * and appends only the non-null results to the given [destination].
 */
public inline fun > CharSequence.mapIndexedNotNullTo(destination: C, transform: (Int, Char) -> R?): C {
    forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } }
    return destination
}

/**
 * Applies the given [transform] function to each character and its index in the original char sequence
 * and appends the results to the given [destination].
 */
public inline fun > CharSequence.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C {
    var index = 0
    for (item in this)
        destination.add(transform(index++, item))
    return destination
}

/**
 * Applies the given [transform] function to each character and its index in the original string
 * and appends the results to the given [destination].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun > String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C {
    var index = 0
    for (item in this)
        destination.add(transform(index++, item))
    return destination
}

/**
 * Returns a list containing only the non-null results of applying the given [transform] function
 * to each character in the original char sequence.
 */
public inline fun  CharSequence.mapNotNull(transform: (Char) -> R?): List {
    return mapNotNullTo(ArrayList(), transform)
}

/**
 * Applies the given [transform] function to each character in the original char sequence
 * and appends only the non-null results to the given [destination].
 */
public inline fun > CharSequence.mapNotNullTo(destination: C, transform: (Char) -> R?): C {
    forEach { element -> transform(element)?.let { destination.add(it) } }
    return destination
}

/**
 * Applies the given [transform] function to each character of the original char sequence
 * and appends the results to the given [destination].
 */
public inline fun > CharSequence.mapTo(destination: C, transform: (Char) -> R): C {
    for (item in this)
        destination.add(transform(item))
    return destination
}

/**
 * Applies the given [transform] function to each character of the original string
 * and appends the results to the given [destination].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun > String.mapTo(destination: C, transform: (Char) -> R): C {
    for (item in this)
        destination.add(transform(item))
    return destination
}

/**
 * Returns a lazy [Iterable] of [IndexedValue] for each character of the original char sequence.
 */
public fun CharSequence.withIndex(): Iterable> {
    return IndexingIterable { iterator() }
}

/**
 * Returns a lazy [Iterable] of [IndexedValue] for each character of the original string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.withIndex(): Iterable> {
    return IndexingIterable { iterator() }
}

/**
 * Returns `true` if all characters match the given [predicate].
 */
public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean {
    for (element in this) if (!predicate(element)) return false
    return true
}

/**
 * Returns `true` if all characters match the given [predicate].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.all(predicate: (Char) -> Boolean): Boolean {
    for (element in this) if (!predicate(element)) return false
    return true
}

/**
 * Returns `true` if char sequence has at least one character.
 */
public fun CharSequence.any(): Boolean {
    for (element in this) return true
    return false
}

/**
 * Returns `true` if string has at least one character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.any(): Boolean {
    for (element in this) return true
    return false
}

/**
 * Returns `true` if at least one character matches the given [predicate].
 */
public inline fun CharSequence.any(predicate: (Char) -> Boolean): Boolean {
    for (element in this) if (predicate(element)) return true
    return false
}

/**
 * Returns `true` if at least one character matches the given [predicate].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.any(predicate: (Char) -> Boolean): Boolean {
    for (element in this) if (predicate(element)) return true
    return false
}

/**
 * Returns the length of this char sequence.
 */
public fun CharSequence.count(): Int {
    return length()
}

/**
 * Returns the number of characters in this string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.count(): Int {
    return length()
}

/**
 * Returns the number of characters matching the given [predicate].
 */
public inline fun CharSequence.count(predicate: (Char) -> Boolean): Int {
    var count = 0
    for (element in this) if (predicate(element)) count++
    return count
}

/**
 * Returns the number of characters matching the given [predicate].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.count(predicate: (Char) -> Boolean): Int {
    var count = 0
    for (element in this) if (predicate(element)) count++
    return count
}

/**
 * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each character.
 */
public inline fun  CharSequence.fold(initial: R, operation: (R, Char) -> R): R {
    var accumulator = initial
    for (element in this) accumulator = operation(accumulator, element)
    return accumulator
}

/**
 * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.fold(initial: R, operation: (R, Char) -> R): R {
    var accumulator = initial
    for (element in this) accumulator = operation(accumulator, element)
    return accumulator
}

/**
 * Accumulates value starting with [initial] value and applying [operation] from right to left to each character and current accumulator value.
 */
public inline fun  CharSequence.foldRight(initial: R, operation: (Char, R) -> R): R {
    var index = lastIndex
    var accumulator = initial
    while (index >= 0) {
        accumulator = operation(get(index--), accumulator)
    }
    return accumulator
}

/**
 * Accumulates value starting with [initial] value and applying [operation] from right to left to each character and current accumulator value.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.foldRight(initial: R, operation: (Char, R) -> R): R {
    var index = lastIndex
    var accumulator = initial
    while (index >= 0) {
        accumulator = operation(get(index--), accumulator)
    }
    return accumulator
}

/**
 * Performs the given [operation] on each character.
 */
public inline fun CharSequence.forEach(operation: (Char) -> Unit): Unit {
    for (element in this) operation(element)
}

/**
 * Performs the given [operation] on each character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.forEach(operation: (Char) -> Unit): Unit {
    for (element in this) operation(element)
}

/**
 * Performs the given [operation] on each character, providing sequential index with the character.
 */
public inline fun CharSequence.forEachIndexed(operation: (Int, Char) -> Unit): Unit {
    var index = 0
    for (item in this) operation(index++, item)
}

/**
 * Performs the given [operation] on each character, providing sequential index with the character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.forEachIndexed(operation: (Int, Char) -> Unit): Unit {
    var index = 0
    for (item in this) operation(index++, item)
}

/**
 * Returns the largest character or `null` if there are no characters.
 */
public fun CharSequence.max(): Char? {
    if (isEmpty()) return null
    var max = this[0]
    for (i in 1..lastIndex) {
        val e = this[i]
        if (max < e) max = e
    }
    return max
}

/**
 * Returns the largest character or `null` if there are no characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.max(): Char? {
    if (isEmpty()) return null
    var max = this[0]
    for (i in 1..lastIndex) {
        val e = this[i]
        if (max < e) max = e
    }
    return max
}

/**
 * Returns the first character yielding the largest value of the given function or `null` if there are no characters.
 */
public inline fun > CharSequence.maxBy(f: (Char) -> R): Char? {
    if (isEmpty()) return null
    var maxElem = this[0]
    var maxValue = f(maxElem)
    for (i in 1..lastIndex) {
        val e = this[i]
        val v = f(e)
        if (maxValue < v) {
            maxElem = e
            maxValue = v
        }
    }
    return maxElem
}

/**
 * Returns the first character yielding the largest value of the given function or `null` if there are no characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun > String.maxBy(f: (Char) -> R): Char? {
    if (isEmpty()) return null
    var maxElem = this[0]
    var maxValue = f(maxElem)
    for (i in 1..lastIndex) {
        val e = this[i]
        val v = f(e)
        if (maxValue < v) {
            maxElem = e
            maxValue = v
        }
    }
    return maxElem
}

/**
 * Returns the smallest character or `null` if there are no characters.
 */
public fun CharSequence.min(): Char? {
    if (isEmpty()) return null
    var min = this[0]
    for (i in 1..lastIndex) {
        val e = this[i]
        if (min > e) min = e
    }
    return min
}

/**
 * Returns the smallest character or `null` if there are no characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.min(): Char? {
    if (isEmpty()) return null
    var min = this[0]
    for (i in 1..lastIndex) {
        val e = this[i]
        if (min > e) min = e
    }
    return min
}

/**
 * Returns the first character yielding the smallest value of the given function or `null` if there are no characters.
 */
public inline fun > CharSequence.minBy(f: (Char) -> R): Char? {
    if (isEmpty()) return null
    var minElem = this[0]
    var minValue = f(minElem)
    for (i in 1..lastIndex) {
        val e = this[i]
        val v = f(e)
        if (minValue > v) {
            minElem = e
            minValue = v
        }
    }
    return minElem
}

/**
 * Returns the first character yielding the smallest value of the given function or `null` if there are no characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun > String.minBy(f: (Char) -> R): Char? {
    if (isEmpty()) return null
    var minElem = this[0]
    var minValue = f(minElem)
    for (i in 1..lastIndex) {
        val e = this[i]
        val v = f(e)
        if (minValue > v) {
            minElem = e
            minValue = v
        }
    }
    return minElem
}

/**
 * Returns `true` if the char sequence has no characters.
 */
public fun CharSequence.none(): Boolean {
    for (element in this) return false
    return true
}

/**
 * Returns `true` if the string has no characters.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.none(): Boolean {
    for (element in this) return false
    return true
}

/**
 * Returns `true` if no characters match the given [predicate].
 */
public inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean {
    for (element in this) if (predicate(element)) return false
    return true
}

/**
 * Returns `true` if no characters match the given [predicate].
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.none(predicate: (Char) -> Boolean): Boolean {
    for (element in this) if (predicate(element)) return false
    return true
}

/**
 * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character.
 */
public inline fun CharSequence.reduce(operation: (Char, Char) -> Char): Char {
    val iterator = this.iterator()
    if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
    var accumulator = iterator.next()
    while (iterator.hasNext()) {
        accumulator = operation(accumulator, iterator.next())
    }
    return accumulator
}

/**
 * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.reduce(operation: (Char, Char) -> Char): Char {
    val iterator = this.iterator()
    if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
    var accumulator = iterator.next()
    while (iterator.hasNext()) {
        accumulator = operation(accumulator, iterator.next())
    }
    return accumulator
}

/**
 * Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value.
 */
public inline fun CharSequence.reduceRight(operation: (Char, Char) -> Char): Char {
    var index = lastIndex
    if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
    var accumulator = get(index--)
    while (index >= 0) {
        accumulator = operation(get(index--), accumulator)
    }
    return accumulator
}

/**
 * Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.reduceRight(operation: (Char, Char) -> Char): Char {
    var index = lastIndex
    if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
    var accumulator = get(index--)
    while (index >= 0) {
        accumulator = operation(get(index--), accumulator)
    }
    return accumulator
}

/**
 * Returns the sum of all values produced by [transform] function applied to each character in the char sequence.
 */
public inline fun CharSequence.sumBy(transform: (Char) -> Int): Int {
    var sum: Int = 0
    for (element in this) {
        sum += transform(element)
    }
    return sum
}

/**
 * Returns the sum of all values produced by [transform] function applied to each character in the string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.sumBy(transform: (Char) -> Int): Int {
    var sum: Int = 0
    for (element in this) {
        sum += transform(element)
    }
    return sum
}

/**
 * Returns the sum of all values produced by [transform] function applied to each character in the char sequence.
 */
public inline fun CharSequence.sumByDouble(transform: (Char) -> Double): Double {
    var sum: Double = 0.0
    for (element in this) {
        sum += transform(element)
    }
    return sum
}

/**
 * Returns the sum of all values produced by [transform] function applied to each character in the string.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.sumByDouble(transform: (Char) -> Double): Double {
    var sum: Double = 0.0
    for (element in this) {
        sum += transform(element)
    }
    return sum
}

/**
 * Splits the original char sequence into pair of char sequences,
 * where *first* char sequence contains characters for which [predicate] yielded `true`,
 * while *second* char sequence contains characters for which [predicate] yielded `false`.
 */
public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair {
    val first = StringBuilder()
    val second = StringBuilder()
    for (element in this) {
        if (predicate(element)) {
            first.append(element)
        } else {
            second.append(element)
        }
    }
    return Pair(first, second)
}

/**
 * Splits the original string into pair of strings,
 * where *first* string contains characters for which [predicate] yielded `true`,
 * while *second* string contains characters for which [predicate] yielded `false`.
 */
public inline fun String.partition(predicate: (Char) -> Boolean): Pair {
    val first = StringBuilder()
    val second = StringBuilder()
    for (element in this) {
        if (predicate(element)) {
            first.append(element)
        } else {
            second.append(element)
        }
    }
    return Pair(first.toString(), second.toString())
}

/**
 * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence.
 */
public fun CharSequence.zip(other: String): List> {
    return zip(other) { c1, c2 -> c1 to c2 }
}

/**
 * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.zip(other: String): List> {
    return zip(other) { c1, c2 -> c1 to c2 }
}

/**
 * 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  CharSequence.zip(other: String, transform: (Char, Char) -> V): List {
    val length = Math.min(this.length(), other.length())
    val list = ArrayList(length)
    for (i in 0..length-1) {
        list.add(transform(this[i], other[i]))
    }
    return list
}

/**
 * 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.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun  String.zip(other: String, transform: (Char, Char) -> V): List {
    val length = Math.min(this.length(), other.length())
    val list = ArrayList(length)
    for (i in 0..length-1) {
        list.add(transform(this[i], other[i]))
    }
    return list
}

/**
 * Returns a sequence from the given collection.
 */
public fun CharSequence.asSequence(): Sequence {
    if (this is String && isEmpty()) return emptySequence()
    return object : Sequence {
        override fun iterator(): Iterator {
            return [email protected]()
        }
    }
}

/**
 * Returns a sequence from the given collection.
 */
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.asSequence(): Sequence {
    if (this is String && isEmpty()) return emptySequence()
    return object : Sequence {
        override fun iterator(): Iterator {
            return [email protected]()
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy