Provide overloads both for Strings and CharSequences of filter, filterNot, reversed and partition

This commit is contained in:
Ilya Gorbunov
2015-11-04 06:02:54 +03:00
parent 62d6bcaa6d
commit 05fd2b012a
5 changed files with 106 additions and 75 deletions
@@ -1,6 +1,8 @@
package templates
import templates.Family.*
import templates.DocExtensions.collection
import templates.DocExtensions.element
fun filtering(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
@@ -357,14 +359,9 @@ fun filtering(): List<GenericFunction> {
"""
}
deprecate(Strings) { forBinaryCompatibility }
doc(CharSequences) { "Returns a string containing only those characters from the original string that match the given [predicate]." }
returns(CharSequences, Strings) { "String" }
body(CharSequences, Strings) {
"""
return filterTo(StringBuilder(), predicate).toString()
"""
}
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} containing only those characters from the original ${f.collection} that match the given [predicate]." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f -> """return filterTo(StringBuilder(), predicate)${toResult(f)}""" }
inline(false, Sequences)
doc(Sequences) { "Returns a sequence containing all elements matching the given [predicate]." }
@@ -414,14 +411,9 @@ fun filtering(): List<GenericFunction> {
"""
}
deprecate(Strings) { forBinaryCompatibility }
doc(CharSequences) { "Returns a string containing only those characters from the original string that do not match the given [predicate]." }
returns(CharSequences, Strings) { "String" }
body(CharSequences, Strings) {
"""
return filterNotTo(StringBuilder(), predicate).toString()
"""
}
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} containing only those characters from the original ${f.collection} that do not match the given [predicate]." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f -> """return filterNotTo(StringBuilder(), predicate)${toResult(f)}""" }
inline(false, Sequences)
doc(Sequences) { "Returns a sequence containing all elements not matching the given [predicate]." }
@@ -509,9 +501,9 @@ fun filtering(): List<GenericFunction> {
"""
}
doc(Strings) { "Returns a string containing characters at specified [indices]." }
returns(Strings) { "String" }
body(Strings) {
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} containing ${f.element}s of the original ${f.collection} at specified [indices]." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f ->
"""
val size = indices.collectionSizeOrDefault(10)
if (size == 0) return ""
@@ -519,7 +511,7 @@ fun filtering(): List<GenericFunction> {
for (i in indices) {
result.append(get(i))
}
return result.toString()
return result${toResult(f)}
"""
}
}
@@ -541,12 +533,12 @@ fun filtering(): List<GenericFunction> {
"""
}
doc(Strings) { "Returns a string containing characters at indices at the specified [indices]." }
returns(Strings) { "String" }
body(Strings) {
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} containing ${f.element}s of the original ${f.collection} at the specified range of [indices]." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f ->
"""
if (indices.isEmpty()) return ""
return substring(indices)
return ${ mapOf(Strings to "substring", CharSequences to "subSequence")[f]}(indices)
"""
}
}
@@ -420,16 +420,16 @@ fun generators(): List<GenericFunction> {
"""
}
deprecate(Strings) { forBinaryCompatibility }
doc(CharSequences) {
doc(CharSequences, Strings) { f ->
"""
Splits the original char sequence into pair of strings,
where *first* string contains characters for which [predicate] yielded `true`,
while *second* string contains characters for which [predicate] yielded `false`.
Splits the original ${f.collection} into pair of ${f.collection}s,
where *first* ${f.collection} contains characters for which [predicate] yielded `true`,
while *second* ${f.collection} contains characters for which [predicate] yielded `false`.
"""
}
returns(CharSequences, Strings) { "Pair<String, String>" }
body(CharSequences, Strings) {
returns(CharSequences, Strings) { "Pair<SELF, SELF>" }
body(CharSequences, Strings) { f ->
val toString = if (f == Strings) ".toString()" else ""
"""
val first = StringBuilder()
val second = StringBuilder()
@@ -440,7 +440,7 @@ fun generators(): List<GenericFunction> {
second.append(element)
}
}
return Pair(first.toString(), second.toString())
return Pair(first$toString, second$toString)
"""
}
}
@@ -1,6 +1,7 @@
package templates
import templates.Family.*
import templates.DocExtensions.collection
fun ordering(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
@@ -39,13 +40,11 @@ fun ordering(): List<GenericFunction> {
"""
}
deprecate(Strings) { forBinaryCompatibility }
doc(CharSequences) { "Returns a string with characters in reversed order." }
returns(CharSequences, Strings) { "String" }
body(CharSequences, Strings) {
// TODO: Replace with StringBuilder(this) when JS can handle it
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} with characters in reversed order." }
returns(CharSequences, Strings) { "SELF" }
body(CharSequences, Strings) { f ->
"""
return StringBuilder().append(this).reverse().toString()
return StringBuilder(this).reverse()${ if (f == Strings) ".toString()" else "" }
"""
}