added count(predicate) helper function to String along with Char.is*() methods from java.lang.Character

This commit is contained in:
James Strachan
2012-04-17 10:12:51 +01:00
parent bd39691072
commit 3b95e7fa7f
3 changed files with 57 additions and 9 deletions
+27
View File
@@ -0,0 +1,27 @@
package kotlin
public inline fun Char.isDefined(): Boolean = Character.isDefined(this)
public inline fun Char.isDigit(): Boolean = Character.isDigit(this)
public inline fun Char.isHighSurrogate(): Boolean = Character.isHighSurrogate(this)
public inline fun Char.isIdentifierIgnorable(): Boolean = Character.isIdentifierIgnorable(this)
public inline fun Char.isISOControl(): Boolean = Character.isISOControl(this)
public inline fun Char.isJavaIdentifierPart(): Boolean = Character.isJavaIdentifierPart(this)
public inline fun Char.isJavaIdentifierStart(): Boolean = Character.isJavaIdentifierStart(this)
public inline fun Char.isJavaLetter(): Boolean = Character.isJavaLetter(this)
public inline fun Char.isJavaLetterOrDigit(): Boolean = Character.isJavaLetterOrDigit(this)
/**
* Returns true if the character is whitespace
*
* @includeFunctionBody ../../test/StringTest.kt count
*/
public inline fun Char.isWhitespace(): Boolean = Character.isWhitespace(this)
+16 -1
View File
@@ -211,4 +211,19 @@ public inline fun CharSequence.subSequence(start : Int, end : Int) : CharSequenc
public inline fun CharSequence.get(start : Int, end : Int) : CharSequence? = subSequence(start, end)
public inline fun CharSequence.toString() : String? = (this as java.lang.CharSequence).toString()
public inline fun CharSequence.toString() : String? = (this as java.lang.CharSequence).toString()
/**
* Counts the number of characters which match the given predicate
*
* @includeFunctionBody ../../test/StringTest.kt count
*/
public inline fun String.count(predicate: (Char) -> Boolean): Int {
var answer = 0
for (c in this) {
if (predicate(c)) {
answer++
}
}
return answer
}
+14 -8
View File
@@ -3,17 +3,17 @@ package test.string
import kotlin.io.*
import kotlin.test.*
import junit.framework.*
import org.junit.Test as test
class StringTest() : TestCase() {
fun testStringIterator() {
class StringTest {
test fun stringIterator() {
var sum = 0
for(c in "239")
sum += (c.toInt() - '0'.toInt())
assertTrue(sum == 14)
}
fun testStringBuilderIterator() {
test fun stringBuilderIterator() {
var sum = 0
val sb = StringBuilder()
for(c in "239")
@@ -26,7 +26,7 @@ class StringTest() : TestCase() {
assertTrue(sum == 14)
}
fun testOrEmpty() {
test fun orEmpty() {
val s: String? = "hey"
val ns: String? = null
@@ -34,16 +34,22 @@ class StringTest() : TestCase() {
assertEquals("", ns.orEmpty())
}
fun testToShort() {
test fun toShort() {
assertEquals(77.toShort(), "77".toShort())
}
fun testToInt() {
test fun toInt() {
assertEquals(77, "77".toInt())
}
fun testToLong() {
test fun toLong() {
assertEquals(77.toLong(), "77".toLong())
}
test fun count() {
val text = "hello there\tfoo\nbar"
val whitespaceCount = text.count { it.isWhitespace() }
assertEquals(3, whitespaceCount)
}
}