From 295ce5fdf8b0ad324744a4a650f533c7ad892972 Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Tue, 15 Jul 2014 21:29:51 +0400 Subject: [PATCH] Unify substringBefore/After and related functions, so that they always return receiver in case of missing delimiter by default. --- libraries/stdlib/src/kotlin/text/Strings.kt | 112 +++++++++++--------- libraries/stdlib/test/text/StringTest.kt | 10 +- 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index 4de2122fcd9..844947746cd 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -96,66 +96,74 @@ public fun Stream.join(separator: String = ", ", prefix: String = "", po return joinToString(separator, prefix, postfix, limit, truncated) } /** - * Returns a substring before first occurrence of delimiter. In case of no delimiter, returns the whole string. + * Returns a substring before first occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringBefore(delimiter: Char): String { +public fun String.substringBefore(delimiter: Char, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) this else substring(0, index) + return if (index == -1) missingSeparatorValue else substring(0, index) } /** - * Returns a substring before first occurrence of delimiter. In case of no delimiter, returns the whole string. + * Returns a substring before first occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringBefore(delimiter: String): String { +public fun String.substringBefore(delimiter: String, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) this else substring(0, index) + return if (index == -1) missingSeparatorValue else substring(0, index) } /** - * Returns a substring after first occurrence of delimiter. In case of no delimiter, returns an empty string. + * Returns a substring after first occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringAfter(delimiter: Char): String { +public fun String.substringAfter(delimiter: Char, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) "" else substring(index + 1, length) + return if (index == -1) missingSeparatorValue else substring(index + 1, length) } /** - * Returns a substring after first occurrence of delimiter. In case of no delimiter, returns an empty string. + * Returns a substring after first occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringAfter(delimiter: String): String { +public fun String.substringAfter(delimiter: String, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) "" else substring(index + delimiter.length, length) + return if (index == -1) missingSeparatorValue else substring(index + delimiter.length, length) } /** - * Returns a substring before last occurrence of delimiter. In case of no delimiter, returns the whole string. + * Returns a substring before last occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringBeforeLast(delimiter: Char): String { +public fun String.substringBeforeLast(delimiter: Char, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) this else substring(0, index) + return if (index == -1) missingSeparatorValue else substring(0, index) } /** - * Returns a substring before last occurrence of delimiter. In case of no delimiter, returns the whole string. + * Returns a substring before last occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringBeforeLast(delimiter: String): String { +public fun String.substringBeforeLast(delimiter: String, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) this else substring(0, index) + return if (index == -1) missingSeparatorValue else substring(0, index) } /** - * Returns a substring after last occurrence of delimiter. In case of no delimiter, returns an empty string. + * Returns a substring after last occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringAfterLast(delimiter: Char): String { +public fun String.substringAfterLast(delimiter: Char, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) "" else substring(index + 1, length) + return if (index == -1) missingSeparatorValue else substring(index + 1, length) } /** - * Returns a substring after last occurrence of delimiter. In case of no delimiter, returns an empty string. + * Returns a substring after last occurrence of delimiter. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.substringAfterLast(delimiter: String): String { +public fun String.substringAfterLast(delimiter: String, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) "" else substring(index + delimiter.length, length) + return if (index == -1) missingSeparatorValue else substring(index + delimiter.length, length) } /** @@ -185,65 +193,73 @@ public fun String.replaceRange(range: IntRange, replacement: String): String { } /** - * Replace part of string before first occurrence of given delimiter with replacement string + * Replace part of string before first occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceBefore(delimiter: Char, replacement: String): String { +public fun String.replaceBefore(delimiter: Char, replacement: String, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) replacement else replaceRange(0, index, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(0, index, replacement) } /** - * Replace part of string before first occurrence of given delimiter with replacement string + * Replace part of string before first occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceBefore(delimiter: String, replacement: String): String { +public fun String.replaceBefore(delimiter: String, replacement: String, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) replacement else replaceRange(0, index, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(0, index, replacement) } /** - * Replace part of string after first occurrence of given delimiter with replacement string + * Replace part of string after first occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceAfter(delimiter: Char, replacement: String): String { +public fun String.replaceAfter(delimiter: Char, replacement: String, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) this else replaceRange(index + 1, length, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(index + 1, length, replacement) } /** - * Replace part of string after first occurrence of given delimiter with replacement string + * Replace part of string after first occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceAfter(delimiter: String, replacement: String): String { +public fun String.replaceAfter(delimiter: String, replacement: String, missingSeparatorValue : String = this): String { val index = indexOf(delimiter) - return if (index == -1) this else replaceRange(index + delimiter.length, length, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(index + delimiter.length, length, replacement) } /** - * Replace part of string after last occurrence of given delimiter with replacement string + * Replace part of string after last occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceAfterLast(delimiter: String, replacement: String): String { +public fun String.replaceAfterLast(delimiter: String, replacement: String, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) this else replaceRange(index + delimiter.length, length, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(index + delimiter.length, length, replacement) } /** - * Replace part of string after last occurrence of given delimiter with replacement string + * Replace part of string after last occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceAfterLast(delimiter: Char, replacement: String): String { +public fun String.replaceAfterLast(delimiter: Char, replacement: String, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) this else replaceRange(index + 1, length, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(index + 1, length, replacement) } /** - * Replace part of string before last occurrence of given delimiter with replacement string + * Replace part of string before last occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceBeforeLast(delimiter: Char, replacement: String): String { +public fun String.replaceBeforeLast(delimiter: Char, replacement: String, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) replacement else replaceRange(0, index, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(0, index, replacement) } /** - * Replace part of string before last occurrence of given delimiter with replacement string + * Replace part of string before last occurrence of given delimiter with replacement string. + * In case of no delimiter, returns the value of missingSeparatorValue which defaults to original string. */ -public fun String.replaceBeforeLast(delimiter: String, replacement: String): String { +public fun String.replaceBeforeLast(delimiter: String, replacement: String, missingSeparatorValue : String = this): String { val index = lastIndexOf(delimiter) - return if (index == -1) replacement else replaceRange(0, index, replacement) + return if (index == -1) missingSeparatorValue else replaceRange(0, index, replacement) } diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 10c6b50c78f..8639cf8735d 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -99,7 +99,9 @@ class StringTest { assertEquals("", s.substringAfter("+")) assertEquals("", s.substringBefore("-")) assertEquals(s, s.substringBefore("=")) - assertEquals("", s.substringAfter("=")) + assertEquals(s, s.substringAfter("=")) + assertEquals("xxx", s.substringBefore("=", "xxx")) + assertEquals("xxx", s.substringAfter("=", "xxx")) } @@ -120,7 +122,9 @@ class StringTest { // non-existing delimiter assertEquals("/user/folder/file.extension", s.replaceAfter("=", "doc")) assertEquals("/user/folder/file.extension", s.replaceAfterLast("=", "another.doc")) - assertEquals("new name", s.replaceBefore("=", "new name")) - assertEquals("/new/path", s.replaceBeforeLast("=", "/new/path")) + assertEquals("/user/folder/file.extension", s.replaceBefore("=", "new name")) + assertEquals("/user/folder/file.extension", s.replaceBeforeLast("=", "/new/path")) + assertEquals("xxx", s.replaceBefore("=", "new name", "xxx")) + assertEquals("xxx", s.replaceBeforeLast("=", "/new/path", "xxx")) } }