| @file:kotlin.jvm.JvmMultifileClass |
| @file:kotlin.jvm.JvmName("StringsKt") |
| |
| package kotlin.text |
| |
| // |
| // 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 <C : Appendable> 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 <C : Appendable> 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 <C : Appendable> 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 <C : Appendable> 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 <C : Appendable> 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<Int>): 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<Int>): 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<Char> { |
| return toCollection(ArrayList<Char>(length)) |
| } |
| |
| /** |
| * Returns an [ArrayList] of all characters. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun String.toArrayList(): ArrayList<Char> { |
| return toCollection(ArrayList<Char>(length)) |
| } |
| |
| /** |
| * Appends all characters to the given [destination] collection. |
| */ |
| public fun <C : MutableCollection<in Char>> CharSequence.toCollection(destination: C): C { |
| for (item in this) { |
| destination.add(item) |
| } |
| return destination |
| } |
| |
| /** |
| * Appends all characters to the given [destination] collection. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun <C : MutableCollection<in Char>> String.toCollection(destination: C): C { |
| for (item in this) { |
| destination.add(item) |
| } |
| return destination |
| } |
| |
| /** |
| * Returns a [HashSet] of all characters. |
| */ |
| public fun CharSequence.toHashSet(): HashSet<Char> { |
| return toCollection(HashSet<Char>(mapCapacity(length))) |
| } |
| |
| /** |
| * Returns a [HashSet] of all characters. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun String.toHashSet(): HashSet<Char> { |
| return toCollection(HashSet<Char>(mapCapacity(length))) |
| } |
| |
| /** |
| * Returns a [LinkedList] containing all elements. |
| */ |
| @Deprecated("Use toCollection(LinkedList()) instead.", ReplaceWith("toCollection(LinkedList())")) |
| public fun String.toLinkedList(): LinkedList<Char> { |
| return toCollection(LinkedList()) |
| } |
| |
| /** |
| * Returns a [List] containing all characters. |
| */ |
| public fun CharSequence.toList(): List<Char> { |
| return this.toArrayList() |
| } |
| |
| /** |
| * Returns a [List] containing all characters. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun String.toList(): List<Char> { |
| return this.toArrayList() |
| } |
| |
| @Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"), level = DeprecationLevel.HIDDEN) |
| public inline fun <K> CharSequence.toMap(selector: (Char) -> K): Map<K, Char> { |
| return toMapBy(selector) |
| } |
| |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> { |
| return toMapBy(selector) |
| } |
| |
| /** |
| * Returns a [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. |
| */ |
| @Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector, transform)")) |
| public inline fun <K, V> CharSequence.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> { |
| return toMapBy(selector, transform) |
| } |
| |
| /** |
| * Returns a [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 <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> { |
| return toMapBy(selector, transform) |
| } |
| |
| /** |
| * Returns a [Map] containing key-value pairs provided by [transform] function applied to characters of the given char sequence. |
| * If any of two pairs would have the same key the last one gets added to the map. |
| */ |
| @kotlin.jvm.JvmName("toMapOfPairs") |
| public inline fun <K, V> CharSequence.toMap(transform: (Char) -> Pair<K, V>): Map<K, V> { |
| val capacity = (length/.75f) + 1 |
| val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16)) |
| for (element in this) { |
| result += transform(element) |
| } |
| return result |
| } |
| |
| /** |
| * Returns a [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 <K> CharSequence.toMapBy(selector: (Char) -> K): Map<K, Char> { |
| val capacity = (length/.75f) + 1 |
| val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16)) |
| for (element in this) { |
| result.put(selector(element), element) |
| } |
| return result |
| } |
| |
| /** |
| * Returns a [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 <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> { |
| val capacity = (length/.75f) + 1 |
| val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16)) |
| for (element in this) { |
| result.put(selector(element), element) |
| } |
| return result |
| } |
| |
| /** |
| * Returns a [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 <K, V> CharSequence.toMapBy(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> { |
| 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)) |
| } |
| return result |
| } |
| |
| /** |
| * Returns a [Set] of all characters. |
| */ |
| public fun CharSequence.toSet(): Set<Char> { |
| return toCollection(LinkedHashSet<Char>(mapCapacity(length))) |
| } |
| |
| /** |
| * Returns a [Set] of all characters. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun String.toSet(): Set<Char> { |
| return toCollection(LinkedHashSet<Char>(mapCapacity(length))) |
| } |
| |
| /** |
| * Returns a [SortedSet] of all characters. |
| */ |
| public fun CharSequence.toSortedSet(): SortedSet<Char> { |
| return toCollection(TreeSet<Char>()) |
| } |
| |
| /** |
| * Returns a [SortedSet] of all characters. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun String.toSortedSet(): SortedSet<Char> { |
| return toCollection(TreeSet<Char>()) |
| } |
| |
| /** |
| * 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 <R> CharSequence.flatMap(transform: (Char) -> Iterable<R>): List<R> { |
| return flatMapTo(ArrayList<R>(), 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 <R> String.flatMap(transform: (Char) -> Iterable<R>): List<R> { |
| return flatMapTo(ArrayList<R>(), 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 <R, C : MutableCollection<in R>> CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable<R>): 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 <R, C : MutableCollection<in R>> String.flatMapTo(destination: C, transform: (Char) -> Iterable<R>): 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 key returned by the given [selector] function. |
| */ |
| public inline fun <K> CharSequence.groupBy(selector: (Char) -> K): Map<K, List<Char>> { |
| return groupByTo(LinkedHashMap<K, MutableList<Char>>(), selector) |
| } |
| |
| /** |
| * Returns a map of the characters in original string grouped by the key returned by the given [selector] function. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun <K> String.groupBy(selector: (Char) -> K): Map<K, List<Char>> { |
| return groupByTo(LinkedHashMap<K, MutableList<Char>>(), selector) |
| } |
| |
| /** |
| * Appends characters from original char sequence grouped by the key returned by the given [selector] function to the given [map]. |
| */ |
| public inline fun <K> CharSequence.groupByTo(map: MutableMap<K, MutableList<Char>>, selector: (Char) -> K): Map<K, MutableList<Char>> { |
| for (element in this) { |
| val key = selector(element) |
| val list = map.getOrPut(key) { ArrayList<Char>() } |
| list.add(element) |
| } |
| return map |
| } |
| |
| /** |
| * Appends characters from original string grouped by the key returned by the given [selector] function to the given [map]. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun <K> String.groupByTo(map: MutableMap<K, MutableList<Char>>, selector: (Char) -> K): Map<K, MutableList<Char>> { |
| for (element in this) { |
| val key = selector(element) |
| val list = map.getOrPut(key) { ArrayList<Char>() } |
| 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 <R> CharSequence.map(transform: (Char) -> R): List<R> { |
| return mapTo(ArrayList<R>(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 <R> String.map(transform: (Char) -> R): List<R> { |
| return mapTo(ArrayList<R>(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 <R> CharSequence.mapIndexed(transform: (Int, Char) -> R): List<R> { |
| return mapIndexedTo(ArrayList<R>(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 <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> { |
| return mapIndexedTo(ArrayList<R>(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 <R : Any> CharSequence.mapIndexedNotNull(transform: (Int, Char) -> R?): List<R> { |
| return mapIndexedNotNullTo(ArrayList<R>(), 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 <R : Any, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R : Any> CharSequence.mapNotNull(transform: (Char) -> R?): List<R> { |
| return mapNotNullTo(ArrayList<R>(), 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 <R : Any, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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<IndexedValue<Char>> { |
| 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<IndexedValue<Char>> { |
| 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 <R> 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 <R> 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 <R> 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 <R> 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 [action] on each character. |
| */ |
| public inline fun CharSequence.forEach(action: (Char) -> Unit): Unit { |
| for (element in this) action(element) |
| } |
| |
| /** |
| * Performs the given [action] on each character. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun String.forEach(action: (Char) -> Unit): Unit { |
| for (element in this) action(element) |
| } |
| |
| /** |
| * Performs the given [action] on each character, providing sequential index with the character. |
| */ |
| public inline fun CharSequence.forEachIndexed(action: (Int, Char) -> Unit): Unit { |
| var index = 0 |
| for (item in this) action(index++, item) |
| } |
| |
| /** |
| * Performs the given [action] on each character, providing sequential index with the character. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun String.forEachIndexed(action: (Int, Char) -> Unit): Unit { |
| var index = 0 |
| for (item in this) action(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 <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R): Char? { |
| if (isEmpty()) return null |
| var maxElem = this[0] |
| var maxValue = selector(maxElem) |
| for (i in 1..lastIndex) { |
| val e = this[i] |
| val v = selector(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 <R : Comparable<R>> String.maxBy(selector: (Char) -> R): Char? { |
| if (isEmpty()) return null |
| var maxElem = this[0] |
| var maxValue = selector(maxElem) |
| for (i in 1..lastIndex) { |
| val e = this[i] |
| val v = selector(e) |
| if (maxValue < v) { |
| maxElem = e |
| maxValue = v |
| } |
| } |
| return maxElem |
| } |
| |
| /** |
| * Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters. |
| */ |
| public fun CharSequence.maxWith(comparator: Comparator<in Char>): Char? { |
| if (isEmpty()) return null |
| var max = this[0] |
| for (i in 1..lastIndex) { |
| val e = this[i] |
| if (comparator.compare(max, e) < 0) max = e |
| } |
| return max |
| } |
| |
| /** |
| * 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 <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R): Char? { |
| if (isEmpty()) return null |
| var minElem = this[0] |
| var minValue = selector(minElem) |
| for (i in 1..lastIndex) { |
| val e = this[i] |
| val v = selector(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 <R : Comparable<R>> String.minBy(selector: (Char) -> R): Char? { |
| if (isEmpty()) return null |
| var minElem = this[0] |
| var minValue = selector(minElem) |
| for (i in 1..lastIndex) { |
| val e = this[i] |
| val v = selector(e) |
| if (minValue > v) { |
| minElem = e |
| minValue = v |
| } |
| } |
| return minElem |
| } |
| |
| /** |
| * Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters. |
| */ |
| public fun CharSequence.minWith(comparator: Comparator<in Char>): Char? { |
| if (isEmpty()) return null |
| var min = this[0] |
| for (i in 1..lastIndex) { |
| val e = this[i] |
| if (comparator.compare(min, e) > 0) min = e |
| } |
| return min |
| } |
| |
| /** |
| * 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 [selector] function applied to each character in the char sequence. |
| */ |
| public inline fun CharSequence.sumBy(selector: (Char) -> Int): Int { |
| var sum: Int = 0 |
| for (element in this) { |
| sum += selector(element) |
| } |
| return sum |
| } |
| |
| /** |
| * Returns the sum of all values produced by [selector] function applied to each character in the string. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun String.sumBy(selector: (Char) -> Int): Int { |
| var sum: Int = 0 |
| for (element in this) { |
| sum += selector(element) |
| } |
| return sum |
| } |
| |
| /** |
| * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. |
| */ |
| public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double { |
| var sum: Double = 0.0 |
| for (element in this) { |
| sum += selector(element) |
| } |
| return sum |
| } |
| |
| /** |
| * Returns the sum of all values produced by [selector] function applied to each character in the string. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public inline fun String.sumByDouble(selector: (Char) -> Double): Double { |
| var sum: Double = 0.0 |
| for (element in this) { |
| sum += selector(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<CharSequence, CharSequence> { |
| 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<String, String> { |
| 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 infix fun CharSequence.zip(other: CharSequence): List<Pair<Char, Char>> { |
| 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 <V> CharSequence.zip(other: CharSequence, transform: (Char, Char) -> V): List<V> { |
| 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])) |
| } |
| 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 <V> String.zip(other: CharSequence, transform: (Char, Char) -> V): List<V> { |
| 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])) |
| } |
| return list |
| } |
| |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public infix fun CharSequence.zip(other: String): List<Pair<Char, Char>> { |
| return zip(other) { c1, c2 -> c1 to c2 } |
| } |
| |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public infix fun String.zip(other: String): List<Pair<Char, Char>> { |
| return zip(other) { c1, c2 -> c1 to c2 } |
| } |
| |
| /** |
| * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. |
| */ |
| public fun CharSequence.asIterable(): Iterable<Char> { |
| if (this is String && isEmpty()) return emptyList() |
| return object : Iterable<Char> { |
| override fun iterator(): Iterator<Char> = this@asIterable.iterator() |
| } |
| } |
| |
| /** |
| * Creates a [Sequence] instance that wraps the original char sequence returning its characters when being iterated. |
| */ |
| public fun CharSequence.asSequence(): Sequence<Char> { |
| if (this is String && isEmpty()) return emptySequence() |
| return object : Sequence<Char> { |
| override fun iterator(): Iterator<Char> { |
| return this@asSequence.iterator() |
| } |
| } |
| } |
| |
| /** |
| * Creates a [Sequence] instance that wraps the original string returning its characters when being iterated. |
| */ |
| @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) |
| public fun String.asSequence(): Sequence<Char> { |
| if (this is String && isEmpty()) return emptySequence() |
| return object : Sequence<Char> { |
| override fun iterator(): Iterator<Char> { |
| return this@asSequence.iterator() |
| } |
| } |
| } |
| |