Skip to content

Commit e00cc87

Browse files
committed
Merge pull request JetBrains#19 from chirino/master
Simplify the String.toRegex extension to a single function
2 parents 5bb7c21 + 3b85642 commit e00cc87

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

libraries/stdlib/src/JavaUtil.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,3 @@ inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
104104
inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
105105
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
106106

107-
/** Converts the string into a regular expression [[Pattern]] so that strings can be split or matched on */
108-
inline fun String.toRegex(): Pattern {
109-
return Pattern.compile(this).sure()
110-
}
111-
112-
/**
113-
* Converts the string into a regular expression [[Pattern]] with the given flags from [[Pattern]] or'd together
114-
* so that strings can be split or matched on
115-
*/
116-
inline fun String.toRegex(flags: Int): Pattern {
117-
return Pattern.compile(this, flags).sure()
118-
}

libraries/stdlib/src/String.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ inline fun String.toLong() = java.lang.Long.parseLong(this).sure()
113113
inline fun String.toFloat() = java.lang.Float.parseFloat(this).sure()
114114
inline fun String.toDouble() = java.lang.Double.parseDouble(this).sure()
115115

116+
/**
117+
* Converts the string into a regular expression [[Pattern]] optionally
118+
* with the specified flags from [[Pattern]] or'd together
119+
* so that strings can be split or matched on.
120+
*/
121+
inline fun String.toRegex(flags: Int=0): java.util.regex.Pattern {
122+
return java.util.regex.Pattern.compile(this, flags).sure()
123+
}
124+
116125
/**
117126
Iterator for characters of given CharSequence
118127
*/

libraries/testlib/test/StringTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,17 @@ class StringTest() : TestCase() {
3333
assertEquals("hey", s.orEmpty())
3434
assertEquals("", ns.orEmpty())
3535
}
36+
37+
fun testToShort() {
38+
assertEquals(77.toShort(), "77".toShort())
39+
}
40+
41+
fun testToInt() {
42+
assertEquals(77, "77".toInt())
43+
}
44+
45+
fun testToLong() {
46+
assertEquals(77.toLong(), "77".toLong())
47+
}
48+
3649
}

0 commit comments

Comments
 (0)