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 { public fun CharSequence.drop(n: Int): CharSequence {
return substring(Math.min(n, length())) 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 { 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. * 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." }) 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 inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence {
public fun String.dropLast(n: Int): String { for (index in this.indices.reversed())
require(n >= 0, { "Requested character count $n is less than zero." }) if (!predicate(this[index]))
return take((length() - n).coerceAtLeast(0)) return subSequence(0, index + 1)
return ""
} }
/** /**
* Returns a string containing all characters except last characters that satisfy the given [predicate]. * Returns a string containing all characters except last characters that satisfy the given [predicate].
*/ */
public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): String { public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
return trimEnd(predicate) 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 CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence {
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { for (index in this.indices)
return trimEnd(predicate) if (!predicate(this[index]))
return subSequence(index, length)
return ""
} }
/** /**
* Returns a string containing all characters except first characters that satisfy the given [predicate]. * 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 { 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." }) 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 { public fun String.take(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." }) 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. * 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." }) require(n >= 0, { "Requested character count $n is less than zero." })
val length = length() val length = length
return substring(length - Math.min(n, 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 inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence {
public fun String.takeLast(n: Int): String { for (index in lastIndex downTo 0) {
require(n >= 0, { "Requested character count $n is less than zero." }) if (!predicate(this[index])) {
val length = length() return subSequence(index + 1, length)
return substring(length - Math.min(n, length), length) }
}
return subSequence(0, length)
} }
/** /**
* Returns a string containing last characters that satisfy the given [predicate]. * 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 { public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0) { for (index in lastIndex downTo 0) {
if (!predicate(this[index])) { if (!predicate(this[index])) {
return substring(index + 1) 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]. * 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 { 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))) { if (!predicate(get(index))) {
return substring(0, index) return substring(0, index)
} }
return this.toString() return this
} }
/** /**
@@ -5,6 +5,18 @@ import templates.Family.*
fun filtering(): List<GenericFunction> { fun filtering(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>() val templates = arrayListOf<GenericFunction>()
fun subsequence(f: Family, start: String, end: String? = null): String {
return when (f) {
Strings -> "substring(${listOfNotNull(start, end).joinToString()})"
CharSequences -> "subSequence($start, ${end ?: "length"})"
else -> throw UnsupportedOperationException(f.toString())
}
}
fun toResult(f: Family): String = if (f == CharSequences) "" else ".toString()"
fun takeAll(f: Family): String = if (f == Strings) "this" else subsequence(f, "0")
templates add f("drop(n: Int)") { templates add f("drop(n: Int)") {
val n = "\$n" val n = "\$n"
doc { "Returns a list containing all elements except first [n] elements." } doc { "Returns a list containing all elements except first [n] elements." }
@@ -47,10 +59,15 @@ fun filtering(): List<GenericFunction> {
""" """
} }
deprecate(Strings) { forBinaryCompatibility } doc(Strings) { "Returns a string with the first [n] characters removed."}
doc(CharSequences) { "Returns a string with the first [n] characters removed."} doc(CharSequences) { "Returns a subsequence of this char sequence with the first [n] characters removed."}
body(CharSequences, Strings) { "return substring(Math.min(n, length()))" } returns(Strings, CharSequences) { "SELF" }
returns(CharSequences, Strings) { "String" } body(Strings, CharSequences) { f ->
"""
require(n >= 0, { "Requested character count $n is less than zero." })
return ${subsequence(f, "n.coerceAtMost(length)")}
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) { body(ArraysOfObjects, ArraysOfPrimitives) {
""" """
@@ -89,15 +106,15 @@ fun filtering(): List<GenericFunction> {
""" """
} }
deprecate(Strings) { forBinaryCompatibility } doc(Strings) { "Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter."}
doc(CharSequences) { "Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter."} doc(CharSequences) { "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."}
body(CharSequences, Strings) { returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) { f ->
""" """
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
return substring(0, Math.min(n, length())) return ${subsequence(f, "0", "n.coerceAtMost(length)")}
""" """
} }
returns(CharSequences, Strings) { "String" }
doc(Sequences) { "Returns a sequence containing first [n] elements." } doc(Sequences) { "Returns a sequence containing first [n] elements." }
returns(Sequences) { "Sequence<T>" } returns(Sequences) { "Sequence<T>" }
@@ -127,8 +144,7 @@ fun filtering(): List<GenericFunction> {
templates add f("dropLast(n: Int)") { templates add f("dropLast(n: Int)") {
val n = "\$n" val n = "\$n"
deprecate(Strings) { forBinaryCompatibility } only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings, CharSequences)
only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
doc { "Returns a list containing all elements except last [n] elements." } doc { "Returns a list containing all elements except last [n] elements." }
returns("List<T>") returns("List<T>")
@@ -139,32 +155,35 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(CharSequences) { "Returns a string with the last [n] characters removed." } doc(Strings) { "Returns a string with the last [n] characters removed." }
returns("String", CharSequences, Strings) doc(CharSequences) { "Returns a subsequence of this char sequence with the last [n] characters removed." }
body(CharSequences, Strings) { returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) {
""" """
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
return take((length() - n).coerceAtLeast(0)) return take((length - n).coerceAtLeast(0))
""" """
} }
} }
templates add f("takeLast(n: Int)") { templates add f("takeLast(n: Int)") {
val n = "\$n" val n = "\$n"
doc { "Returns a list containing last [n] elements." } doc { "Returns a list containing last [n] elements." }
deprecate(Strings) { forBinaryCompatibility } only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings, CharSequences)
only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
returns("List<T>") returns("List<T>")
doc(CharSequences) { "Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter."} doc(Strings) { "Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter."}
body(CharSequences, Strings) { doc(CharSequences) { "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."}
returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) { f ->
""" """
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
val length = length() val length = length
return substring(length - Math.min(n, length), length) return ${subsequence(f, "length - n.coerceAtMost(length)")}
""" """
} }
returns(CharSequences, Strings) { "String" }
body(Lists, ArraysOfObjects, ArraysOfPrimitives) { body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
@@ -200,12 +219,16 @@ fun filtering(): List<GenericFunction> {
""" """
} }
deprecate(Strings) { forBinaryCompatibility } doc(Strings) { "Returns a string containing all characters except first characters that satisfy the given [predicate]." }
doc(CharSequences) { "Returns a string containing all characters except first characters that satisfy the given [predicate]." } doc(CharSequences) { "Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate]." }
returns(CharSequences, Strings) { "String" } returns(Strings, CharSequences) { "SELF" }
body(CharSequences, Strings) { body(Strings, CharSequences) { f ->
""" """
return trimStart(predicate) for (index in this.indices)
if (!predicate(this[index]))
return ${subsequence(f, "index")}
return ""
""" """
} }
@@ -237,16 +260,16 @@ fun filtering(): List<GenericFunction> {
""" """
} }
deprecate(Strings) { forBinaryCompatibility } doc(Strings) { "Returns a string containing the first characters that satisfy the given [predicate]."}
doc(CharSequences) { "Returns a string containing the first characters that satisfy the given [predicate]."} doc(CharSequences) { "Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate]."}
returns(CharSequences, Strings) { "String" } returns(Strings, CharSequences) { "SELF" }
body(CharSequences, Strings) { body(Strings, CharSequences) { f ->
""" """
for (index in 0..length() - 1) for (index in 0..length - 1)
if (!predicate(get(index))) { if (!predicate(get(index))) {
return substring(0, index) return ${subsequence(f, "0", "index")}
} }
return this.toString() return ${takeAll(f)}
""" """
} }
@@ -262,7 +285,6 @@ fun filtering(): List<GenericFunction> {
templates add f("dropLastWhile(predicate: (T) -> Boolean)") { templates add f("dropLastWhile(predicate: (T) -> Boolean)") {
inline(true) inline(true)
deprecate(Strings) { forBinaryCompatibility }
only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings) only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
doc { "Returns a list containing all elements except last elements that satisfy the given [predicate]." } doc { "Returns a list containing all elements except last elements that satisfy the given [predicate]." }
returns("List<T>") returns("List<T>")
@@ -278,19 +300,23 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(CharSequences) { "Returns a string containing all characters except last characters that satisfy the given [predicate]." } doc(Strings) { "Returns a string containing all characters except last characters that satisfy the given [predicate]." }
returns("String", CharSequences, Strings) doc(CharSequences) { "Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate]." }
body(CharSequences, Strings) { returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f ->
""" """
return trimEnd(predicate) for (index in this.indices.reversed())
if (!predicate(this[index]))
return ${subsequence(f, "0", "index + 1")}
return ""
""" """
} }
} }
templates add f("takeLastWhile(predicate: (T) -> Boolean)") { templates add f("takeLastWhile(predicate: (T) -> Boolean)") {
inline(true) inline(true)
deprecate(Strings) { forBinaryCompatibility } only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings, CharSequences)
only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
doc { "Returns a list containing last elements satisfying the given [predicate]."} doc { "Returns a list containing last elements satisfying the given [predicate]."}
returns("List<T>") returns("List<T>")
@@ -305,16 +331,17 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(CharSequences) { "Returns a string containing last characters that satisfy the given [predicate]." } doc(Strings) { "Returns a string containing last characters that satisfy the given [predicate]." }
returns("String", CharSequences, Strings) doc(CharSequences) { "Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate]." }
body(CharSequences, Strings) { returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) { f ->
""" """
for (index in lastIndex downTo 0) { for (index in lastIndex downTo 0) {
if (!predicate(this[index])) { if (!predicate(this[index])) {
return substring(index + 1) return ${subsequence(f, "index + 1")}
} }
} }
return this.toString() return ${takeAll(f)}
""" """
} }
} }