Preserve type of subsequence in drop* and take* operations.

Validate argument of drop (required in JS, more meaningful exception in JVM)
This commit is contained in:
Ilya Gorbunov
2015-10-23 00:00:26 +03:00
parent 67229401c6
commit 5b055ac663
2 changed files with 150 additions and 117 deletions
+77 -71
View File
@@ -446,65 +446,75 @@ public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? {
}
/**
* Returns a string with the first [n] characters removed.
* Returns a subsequence of this char sequence with the first [n] characters removed.
*/
public fun CharSequence.drop(n: Int): String {
return substring(Math.min(n, length()))
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 list containing all elements except first [n] elements.
* Returns a string with the first [n] characters removed.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.drop(n: Int): String {
return substring(Math.min(n, length()))
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 CharSequence.dropLast(n: Int): String {
public fun String.dropLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
return take((length() - n).coerceAtLeast(0))
return take((length - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except last [n] elements.
* Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate].
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.dropLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
return take((length() - n).coerceAtLeast(0))
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 CharSequence.dropLastWhile(predicate: (Char) -> Boolean): String {
return trimEnd(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 list containing all elements except last elements that satisfy the given [predicate].
* Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate].
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
return trimEnd(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 CharSequence.dropWhile(predicate: (Char) -> Boolean): String {
return trimStart(predicate)
}
/**
* Returns a list containing all elements except first elements that satisfy the given [predicate].
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
return trimStart(predicate)
for (index in this.indices)
if (!predicate(this[index]))
return substring(index)
return ""
}
/**
@@ -599,87 +609,83 @@ public fun String.slice(indices: Iterable<Int>): String {
}
/**
* Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter.
* 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): String {
public fun CharSequence.take(n: Int): CharSequence {
require(n >= 0, { "Requested character count $n is less than zero." })
return substring(0, Math.min(n, length()))
return subSequence(0, n.coerceAtMost(length))
}
/**
* Returns a list containing first [n] elements.
* Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.take(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
return substring(0, Math.min(n, length()))
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 CharSequence.takeLast(n: Int): String {
public fun String.takeLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
val length = length()
return substring(length - Math.min(n, length), length)
val length = length
return substring(length - n.coerceAtMost(length))
}
/**
* Returns a list containing last [n] elements.
* Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate].
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun String.takeLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
val length = length()
return substring(length - Math.min(n, length), length)
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 CharSequence.takeLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return substring(index + 1)
}
}
return this.toString()
}
/**
* Returns a list containing last elements satisfying the given [predicate].
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
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.toString()
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 CharSequence.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length() - 1)
if (!predicate(get(index))) {
return substring(0, index)
}
return this.toString()
}
/**
* Returns a list containing first elements satisfying the given [predicate].
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length() - 1)
for (index in 0..length - 1)
if (!predicate(get(index))) {
return substring(0, index)
}
return this.toString()
return this
}
/**