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:
@@ -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>
|
inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
|
||||||
= if (this != null) this else Collections.EMPTY_LIST as 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()
|
|
||||||
}
|
|
||||||
@@ -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.toFloat() = java.lang.Float.parseFloat(this).sure()
|
||||||
inline fun String.toDouble() = java.lang.Double.parseDouble(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
|
Iterator for characters of given CharSequence
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -33,4 +33,17 @@ class StringTest() : TestCase() {
|
|||||||
assertEquals("hey", s.orEmpty())
|
assertEquals("hey", s.orEmpty())
|
||||||
assertEquals("", ns.orEmpty())
|
assertEquals("", ns.orEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testToShort() {
|
||||||
|
assertEquals(77.toShort(), "77".toShort())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testToInt() {
|
||||||
|
assertEquals(77, "77".toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testToLong() {
|
||||||
|
assertEquals(77.toLong(), "77".toLong())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user