diff --git a/stdlib/ktSrc/Arrays.kt b/stdlib/ktSrc/Arrays.kt index 18931942e0f..be457620ccc 100644 --- a/stdlib/ktSrc/Arrays.kt +++ b/stdlib/ktSrc/Arrays.kt @@ -102,7 +102,11 @@ inline val ByteArray.inputStream : ByteArrayInputStream inline fun ByteArray.inputStream(offset: Int, length: Int) = ByteArrayInputStream(this, offset, length) /** Returns true if the array is not empty */ -inline fun Array.notEmpty() : Boolean = this.size > 0 +inline fun Array.notEmpty() : Boolean = !this.isEmpty() /** Returns true if the array is empty */ inline fun Array.isEmpty() : Boolean = this.size == 0 + +/** Returns the array if its not null or else returns an empty array */ +inline fun Array?.orEmpty() : Array = if (this != null) this else array() + diff --git a/stdlib/ktSrc/JavaIterables.kt b/stdlib/ktSrc/JavaIterables.kt index 529da9d4220..ddebe0115f0 100644 --- a/stdlib/ktSrc/JavaIterables.kt +++ b/stdlib/ktSrc/JavaIterables.kt @@ -53,6 +53,20 @@ inline fun > java.lang.Iterable.filterTo(result: C, pr return result } +/** Returns a List containing all the non null elements in this collection */ +inline fun java.lang.Iterable?.filterNulls() : Collection = filterNullsTo(java.util.ArrayList()) + +/** Filters all the null elements in this collection winto the given result collection */ +inline fun > java.lang.Iterable?.filterNullsTo(result: C) : C { + if (this != null) { + for (elem in this) { + if (elem != null) + result.add(elem) + } + } + return result +} + /** Returns a new collection containing all elements in this collection which do not match the given predicate */ inline fun java.lang.Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) diff --git a/stdlib/ktSrc/JavaUtil.kt b/stdlib/ktSrc/JavaUtil.kt index 9fb67df26f2..3ae59cbf6ad 100644 --- a/stdlib/ktSrc/JavaUtil.kt +++ b/stdlib/ktSrc/JavaUtil.kt @@ -1,6 +1,7 @@ package kotlin.util import java.util.* +import java.util.regex.Pattern /** Returns the size of the collection */ val Collection<*>.size : Int @@ -103,3 +104,15 @@ inline fun java.util.Collection.notEmpty() : Boolean = !this.isEmpty() inline fun java.util.Collection?.orEmpty() : Collection = if (this != null) this else Collections.EMPTY_LIST as Collection +/** Converts the string into a regular expression [[Pattern]] so that strings can be split or matched on */ +inline fun String.toRegex(): Pattern { + return Pattern.compile(this).sure() +} + +/** + * Converts the string into a regular expression [[Pattern]] with the given flags from [[Pattern]] or'd together + * so that strings can be split or matched on + */ +inline fun String.toRegex(flags: Int): Pattern { + return Pattern.compile(this, flags).sure() +} \ No newline at end of file diff --git a/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt b/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt index 6859637d6f4..ccbda01553b 100644 --- a/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt +++ b/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt @@ -56,6 +56,20 @@ inline fun > Array.filterTo(result: C, predicate: (T)- return result } +/** Returns a List containing all the non null elements in this collection */ +inline fun Array?.filterNulls() : Collection = filterNullsTo(java.util.ArrayList()) + +/** Filters all the null elements in this collection winto the given result collection */ +inline fun > Array?.filterNullsTo(result: C) : C { + if (this != null) { + for (elem in this) { + if (elem != null) + result.add(elem) + } + } + return result +} + /** Returns a new collection containing all elements in this collection which do not match the given predicate */ inline fun Array.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) @@ -160,7 +174,7 @@ inline fun > Array.to(result: C) : C { /** Converts the collection into a LinkedList */ inline fun Array.toLinkedList() : LinkedList = this.to(LinkedList()) -/** Converts the collection into a List */ +/** Converts the collection into a List */ inline fun Array.toList() : List = this.to(ArrayList()) /** Converts the collection into a Set */ diff --git a/stdlib/ktSrc/generated/StandardFromJavaIterables.kt b/stdlib/ktSrc/generated/StandardFromJavaIterables.kt index 478edeb0a75..ccd1d9dd4af 100644 --- a/stdlib/ktSrc/generated/StandardFromJavaIterables.kt +++ b/stdlib/ktSrc/generated/StandardFromJavaIterables.kt @@ -56,6 +56,8 @@ inline fun > Iterable.filterTo(result: C, predicate: ( return result } + + /** Returns a new collection containing all elements in this collection which do not match the given predicate */ inline fun Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) @@ -160,7 +162,7 @@ inline fun > Iterable.to(result: C) : C { /** Converts the collection into a LinkedList */ inline fun Iterable.toLinkedList() : LinkedList = this.to(LinkedList()) -/** Converts the collection into a List */ +/** Converts the collection into a List */ inline fun Iterable.toList() : List = this.to(ArrayList()) /** Converts the collection into a Set */ diff --git a/testlib/test/GenerateStandardLib.kt b/testlib/test/GenerateStandardLib.kt index 42fcbd0afb4..84e8fac4b79 100644 --- a/testlib/test/GenerateStandardLib.kt +++ b/testlib/test/GenerateStandardLib.kt @@ -59,25 +59,25 @@ fun main(args: Array) { // JavaIterables - Generic iterable stuff generateFile(File(outDir, "ArraysFromJavaIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JavaIterables.kt")) { - it.replaceAll("java.lang.Iterable", "Array") + it.replaceAll("java.lang.Iterable", "Iterable") + it.replaceAll("java.lang.Iterable", "Array") + it.replaceAll("java.util.Collection", "java.lang.Iterable").replaceAll("(this.size)", "") + it.replaceAll("java.util.Collection", "Iterable").replaceAll("(this.size)", "") + it.replaceAll("java.util.Collection