From 157c86b9c67d2c8b683854612e218bb7c32b61f7 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 14 Apr 2015 02:47:30 +0300 Subject: [PATCH] Empty string is supported as a delimiter in split and as an oldValue in replace. --- libraries/stdlib/src/kotlin/text/Strings.kt | 14 ++++++++------ libraries/stdlib/test/text/StringTest.kt | 6 ++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index 0f93aa39e50..faeafbb81cd 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -643,7 +643,7 @@ private fun String.findAnyOf(strings: Collection, 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 = object : Iterator { 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 diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 3bf0bda2e50..a0334e73eb8 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -361,6 +361,10 @@ class StringTest { assertEquals(listOf("abc", "def", "123", "456"), "abc
def
123
456".splitBy("
", 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("", "-")) } }