KT-29151 Provide specialized string samples for all drop/take functions

These include:
- take, takeLast, takeWhile, takeLastWhile,
- drop, dropLast, dropWhile, dropLastWhile
This commit is contained in:
Ilya Gorbunov
2019-03-06 05:59:28 +03:00
parent 991e739693
commit 42120b93b8
3 changed files with 41 additions and 28 deletions
@@ -253,7 +253,7 @@ public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char?
/**
* Returns a subsequence of this char sequence with the first [n] characters removed.
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public fun CharSequence.drop(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
@@ -263,7 +263,7 @@ public fun CharSequence.drop(n: Int): CharSequence {
/**
* Returns a string with the first [n] characters removed.
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public fun String.drop(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
@@ -273,7 +273,7 @@ public fun String.drop(n: Int): String {
/**
* Returns a subsequence of this char sequence with the last [n] characters removed.
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public fun CharSequence.dropLast(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
@@ -283,7 +283,7 @@ public fun CharSequence.dropLast(n: Int): CharSequence {
/**
* Returns a string with the last [n] characters removed.
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public fun String.dropLast(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
@@ -293,7 +293,7 @@ public fun String.dropLast(n: Int): String {
/**
* Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in lastIndex downTo 0)
@@ -305,7 +305,7 @@ public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): Char
/**
* Returns a string containing all characters except last characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0)
@@ -317,7 +317,7 @@ public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
/**
* Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in this.indices)
@@ -329,7 +329,7 @@ public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequ
/**
* Returns a string containing all characters except first characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
* @sample samples.text.Strings.drop
*/
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
for (index in this.indices)
@@ -475,7 +475,7 @@ public fun String.take(n: Int): String {
/**
* 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.
*
* @sample samples.collections.Collections.Transformations.take
* @sample samples.text.Strings.take
*/
public fun CharSequence.takeLast(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
@@ -486,7 +486,7 @@ public fun CharSequence.takeLast(n: Int): CharSequence {
/**
* Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter.
*
* @sample samples.collections.Collections.Transformations.take
* @sample samples.text.Strings.take
*/
public fun String.takeLast(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
@@ -497,7 +497,7 @@ public fun String.takeLast(n: Int): String {
/**
* Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
* @sample samples.text.Strings.take
*/
public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in lastIndex downTo 0) {
@@ -511,7 +511,7 @@ public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): Char
/**
* Returns a string containing last characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
* @sample samples.text.Strings.take
*/
public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0) {
@@ -525,7 +525,7 @@ public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String {
/**
* Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
* @sample samples.text.Strings.take
*/
public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in 0 until length)
@@ -538,7 +538,7 @@ public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequ
/**
* Returns a string containing the first characters that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
* @sample samples.text.Strings.take
*/
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0 until length)
@@ -193,8 +193,20 @@ class Strings {
@Sample
fun take() {
val string = "Lorem Ipsum"
assertPrints(string.take(5), "Lorem")
val string = "<<<First Grade>>>"
assertPrints(string.take(8), "<<<First")
assertPrints(string.takeLast(8), "Grade>>>")
assertPrints(string.takeWhile { !it.isLetter() }, "<<<")
assertPrints(string.takeLastWhile { !it.isLetter() }, ">>>")
}
@Sample
fun drop() {
val string = "<<<First Grade>>>"
assertPrints(string.drop(6), "st Grade>>>")
assertPrints(string.dropLast(6), "<<<First Gr")
assertPrints(string.dropWhile { !it.isLetter() }, "First Grade>>>")
assertPrints(string.dropLastWhile { !it.isLetter() }, "<<<First Grade")
}
}
@@ -33,6 +33,11 @@ object Filtering : TemplateGroupBase() {
}
}
private fun sampleClass(f: Family): String = when(f) {
Strings, CharSequences -> "samples.text.Strings"
else -> "samples.collections.Collections.Transformations"
}
private fun toResult(f: Family): String = if (f == CharSequences) "" else ".toString()"
private fun takeAll(f: Family): String = if (f == Strings) "this" else subsequence(f, "0")
@@ -47,7 +52,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing all elements except first [n] elements.
"""
}
sample("samples.collections.Collections.Transformations.drop")
sample("${sampleClass(f)}.drop")
returns("List<T>")
body {
"""
@@ -133,9 +138,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing first [n] elements.
"""
}
specialFor(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) {
sample("samples.collections.Collections.Transformations.take")
}
sample("${sampleClass(f)}.take")
returns("List<T>")
body {
"""
@@ -157,7 +160,6 @@ object Filtering : TemplateGroupBase() {
}
specialFor(Strings, CharSequences) {
sample("samples.text.Strings.take")
returns("SELF")
specialFor(Strings) {
doc { "Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter." }
@@ -216,7 +218,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing all elements except last [n] elements.
"""
}
sample("samples.collections.Collections.Transformations.drop")
sample("${sampleClass(f)}.drop")
returns("List<T>")
body {
"""
@@ -245,8 +247,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing last [n] elements.
"""
}
sample("samples.collections.Collections.Transformations.take")
sample("${sampleClass(f)}.take")
returns("List<T>")
specialFor(Strings, CharSequences) {
returns("SELF")
@@ -312,7 +313,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing all elements except first elements that satisfy the given [predicate].
"""
}
sample("samples.collections.Collections.Transformations.drop")
sample("${sampleClass(f)}.drop")
returns("List<T>")
body {
"""
@@ -369,7 +370,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing first elements satisfying the given [predicate].
"""
}
sample("samples.collections.Collections.Transformations.take")
sample("${sampleClass(f)}.take")
returns("List<T>")
body {
"""
@@ -421,7 +422,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing all elements except last elements that satisfy the given [predicate].
"""
}
sample("samples.collections.Collections.Transformations.drop")
sample("${sampleClass(f)}.drop")
returns("List<T>")
body {
@@ -479,7 +480,7 @@ object Filtering : TemplateGroupBase() {
Returns a list containing last elements satisfying the given [predicate].
"""
}
sample("samples.collections.Collections.Transformations.take")
sample("${sampleClass(f)}.take")
returns("List<T>")
body {