Empty string is supported as a delimiter in split and as an oldValue in replace.
This commit is contained in:
@@ -643,7 +643,7 @@ private fun String.findAnyOf(strings: Collection<String>, startIndex: Int, ignor
|
|||||||
return if (index < 0) null else index to string
|
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) {
|
for (index in indices) {
|
||||||
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length(), ignoreCase) }
|
val matchingString = strings.firstOrNull { it.regionMatches(0, this, index, it.length(), ignoreCase) }
|
||||||
if (matchingString != null)
|
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> {
|
override fun iterator(): Iterator<IntRange> = object : Iterator<IntRange> {
|
||||||
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
|
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 currentStartIndex: Int = Math.min(Math.max(startIndex, 0), string.length())
|
||||||
|
var nextSearchIndex: Int = currentStartIndex
|
||||||
var nextItem: IntRange? = null
|
var nextItem: IntRange? = null
|
||||||
var counter: Int = 0
|
var counter: Int = 0
|
||||||
|
|
||||||
private fun calcNext() {
|
private fun calcNext() {
|
||||||
if (currentStartIndex < 0) {
|
if (nextSearchIndex < 0) {
|
||||||
nextState = 0
|
nextState = 0
|
||||||
nextItem = null
|
nextItem = null
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (limit > 0 && ++counter >= limit) {
|
if (limit > 0 && ++counter >= limit || nextSearchIndex > string.length()) {
|
||||||
nextItem = currentStartIndex..string.lastIndex
|
nextItem = currentStartIndex..string.lastIndex
|
||||||
currentStartIndex = -1
|
nextSearchIndex = -1
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val match = string.getNextMatch(currentStartIndex)
|
val match = string.getNextMatch(nextSearchIndex)
|
||||||
if (match == null) {
|
if (match == null) {
|
||||||
nextItem = currentStartIndex..string.lastIndex
|
nextItem = currentStartIndex..string.lastIndex
|
||||||
currentStartIndex = -1
|
nextSearchIndex = -1
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val (index,length) = match
|
val (index,length) = match
|
||||||
nextItem = currentStartIndex..index-1
|
nextItem = currentStartIndex..index-1
|
||||||
currentStartIndex = index + length
|
currentStartIndex = index + length
|
||||||
|
nextSearchIndex = currentStartIndex + if (length == 0) 1 else 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nextState = 1
|
nextState = 1
|
||||||
|
|||||||
@@ -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<BR>def<br>123<bR>456".splitBy("<BR>", ignoreCase = true))
|
||||||
|
|
||||||
assertEquals(listOf("abc", "def", "123", "456"), "abc=-def==123=456".splitBy("==", "=-", "="))
|
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() {
|
test fun splitToLines() {
|
||||||
@@ -536,5 +540,7 @@ class StringTest {
|
|||||||
|
|
||||||
assertEquals("${'$'}bAb", input.replace("ab", "$"))
|
assertEquals("${'$'}bAb", input.replace("ab", "$"))
|
||||||
assertEquals("/b/", input.replace("ab", "/", ignoreCase = true))
|
assertEquals("/b/", input.replace("ab", "/", ignoreCase = true))
|
||||||
|
|
||||||
|
assertEquals("-a-b-b-A-b-", input.replace("", "-"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user