Empty string is supported as a delimiter in split and as an oldValue in replace.

This commit is contained in:
Ilya Gorbunov
2015-04-14 02:47:30 +03:00
parent 43dd23bd4b
commit 157c86b9c6
2 changed files with 14 additions and 6 deletions
+8 -6
View File
@@ -643,7 +643,7 @@ private fun String.findAnyOf(strings: Collection<String>, startIndex: Int, ignor
return if (index < 0) null else index to string
}
val indices = if (!last) Math.max(startIndex, 0)..lastIndex else Math.min(startIndex, lastIndex) downTo 0
val indices = if (!last) Math.max(startIndex, 0)..length() else Math.min(startIndex, lastIndex) downTo 0
for (index in indices) {
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length(), ignoreCase) }
if (matchingString != null)
@@ -794,29 +794,31 @@ private class DelimitedRangesSequence(private val string: String, private val st
override fun iterator(): Iterator<IntRange> = object : Iterator<IntRange> {
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
var currentStartIndex: Int = Math.min(Math.max(startIndex, 0), string.length())
var nextSearchIndex: Int = currentStartIndex
var nextItem: IntRange? = null
var counter: Int = 0
private fun calcNext() {
if (currentStartIndex < 0) {
if (nextSearchIndex < 0) {
nextState = 0
nextItem = null
}
else {
if (limit > 0 && ++counter >= limit) {
if (limit > 0 && ++counter >= limit || nextSearchIndex > string.length()) {
nextItem = currentStartIndex..string.lastIndex
currentStartIndex = -1
nextSearchIndex = -1
}
else {
val match = string.getNextMatch(currentStartIndex)
val match = string.getNextMatch(nextSearchIndex)
if (match == null) {
nextItem = currentStartIndex..string.lastIndex
currentStartIndex = -1
nextSearchIndex = -1
}
else {
val (index,length) = match
nextItem = currentStartIndex..index-1
currentStartIndex = index + length
nextSearchIndex = currentStartIndex + if (length == 0) 1 else 0
}
}
nextState = 1
+6
View File
@@ -361,6 +361,10 @@ class StringTest {
assertEquals(listOf("abc", "def", "123", "456"), "abc<BR>def<br>123<bR>456".splitBy("<BR>", ignoreCase = true))
assertEquals(listOf("abc", "def", "123", "456"), "abc=-def==123=456".splitBy("==", "=-", "="))
assertEquals(listOf("", "a", "b", "c", ""), "abc".splitBy(""))
assertEquals(listOf("", "a", "b", "b", "a", ""), "abba".splitBy("", "a"))
assertEquals(listOf("", "", "b", "b", "", ""), "abba".splitBy("a", ""))
}
test fun splitToLines() {
@@ -536,5 +540,7 @@ class StringTest {
assertEquals("${'$'}bAb", input.replace("ab", "$"))
assertEquals("/b/", input.replace("ab", "/", ignoreCase = true))
assertEquals("-a-b-b-A-b-", input.replace("", "-"))
}
}