Simplify the String.toRegex extension to a single function that uses a default arg.

Also moved it to the String.kt source file that that String extension functions can be found in a single place.
This commit is contained in:
Hiram Chirino
2012-03-09 11:57:05 -05:00
parent 0a70ed6aee
commit 3b85642b96
3 changed files with 22 additions and 12 deletions
-12
View File
@@ -104,15 +104,3 @@ inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
/** 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()
}
+9
View File
@@ -113,6 +113,15 @@ inline fun String.toLong() = java.lang.Long.parseLong(this).sure()
inline fun String.toFloat() = java.lang.Float.parseFloat(this).sure()
inline fun String.toDouble() = java.lang.Double.parseDouble(this).sure()
/**
* Converts the string into a regular expression [[Pattern]] optionally
* with the specified flags from [[Pattern]] or'd together
* so that strings can be split or matched on.
*/
inline fun String.toRegex(flags: Int=0): java.util.regex.Pattern {
return java.util.regex.Pattern.compile(this, flags).sure()
}
/**
Iterator for characters of given CharSequence
*/
+13
View File
@@ -33,4 +33,17 @@ class StringTest() : TestCase() {
assertEquals("hey", s.orEmpty())
assertEquals("", ns.orEmpty())
}
fun testToShort() {
assertEquals(77.toShort(), "77".toShort())
}
fun testToInt() {
assertEquals(77, "77".toInt())
}
fun testToLong() {
assertEquals(77.toLong(), "77".toLong())
}
}