#KT-1675 Fixed, renaming join() -> makeString() and adding an appendString() which reduces the possible number of appendables being created
This commit is contained in:
@@ -61,19 +61,3 @@ public inline fun <T> Array<T>.take(n: Int): List<T> {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt join
|
||||
*/
|
||||
public inline fun <T> Array<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -61,19 +61,3 @@ public inline fun <T> Iterable<T>.take(n: Int): List<T> {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt join
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -142,20 +142,3 @@ private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val pred
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from the first *n (= limit)* elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt joinConcatenatesTheFirstNElementsAboveAThreshold
|
||||
*/
|
||||
public inline fun <T> java.util.Iterator<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int? = null) : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true; var count = 0
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
if (limit == null || ++count <= limit) buffer.append(element) else break
|
||||
}
|
||||
if (limit != null && count > limit) buffer.append("...")
|
||||
return buffer.append(postfix).toString().sure()
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,16 @@ package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
@@ -13,13 +23,22 @@ public inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boo
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
public inline fun <T> java.lang.Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append("...")
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,6 +148,17 @@ public inline fun <T, K> java.lang.Iterable<T>.groupBy(result: Map<K, List<T>> =
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
|
||||
@@ -58,19 +58,3 @@ public inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt join
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ get() {
|
||||
return answer
|
||||
}
|
||||
set(value) {
|
||||
this.classes = value.join(" ")
|
||||
this.classes = value.makeString(" ")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit {
|
||||
/** Converts the collection of nodes to an XML String */
|
||||
public fun nodesToXmlString(nodes: Iterable<Node>, xmlDeclaration: Boolean = false): String {
|
||||
// TODO this should work...
|
||||
// return this.map<Node,String>{it.toXmlString()}.join("")
|
||||
// return this.map<Node,String>{it.toXmlString()}.makeString("")
|
||||
val builder = StringBuilder()
|
||||
for (n in nodes) {
|
||||
builder.append(n.toXmlString(xmlDeclaration))
|
||||
|
||||
@@ -122,7 +122,7 @@ public inline fun <T, K> T?.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>
|
||||
|
||||
|
||||
/** Creates a String from the nullable or item with the given prefix and postfix if supplied */
|
||||
public inline fun <T> T?.join(separator: String, prefix: String = "", postfix: String = ""): String {
|
||||
public inline fun <T> T?.makeString(separator: String = ", ", prefix: String = "", postfix: String = ""): String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
if (this != null) {
|
||||
|
||||
Reference in New Issue
Block a user