From 05b5afcb9497d9d15f1dc46ef74703b1a5f3c944 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 20 Mar 2015 18:56:45 +0300 Subject: [PATCH] Add removePrefix, removeSuffix, removeEnclosing methods. Deprecate trimLeading/trimTrailing/trim(String) methods in favor of new ones. --- libraries/stdlib/src/kotlin/text/Strings.kt | 73 ++++++++++++++------- libraries/stdlib/test/text/StringTest.kt | 22 +++++++ 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index 967dfbf001d..329353ae609 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -1,10 +1,12 @@ package kotlin /** Returns the string with leading and trailing text matching the given string removed */ -public fun String.trim(text: String): String = trimLeading(text).trimTrailing(text) +deprecated("Use removeEnclosing(text, text) or removePrefix(text).removeSuffix(text)") +public fun String.trim(text: String): String = removePrefix(text).removeSuffix(text) /** Returns the string with the prefix and postfix text trimmed */ -public fun String.trim(prefix: String, postfix: String): String = trimLeading(prefix).trimTrailing(postfix) +deprecated("Use removeEnclosing(prefix, suffix) or removePrefix(prefix).removeSuffix(suffix)") +public fun String.trim(prefix: String, postfix: String): String = removePrefix(prefix).removeSuffix(postfix) /** * Returns the string with leading and trailing characters matching the [predicate] trimmed. @@ -72,29 +74,11 @@ public fun String.trimLeading(vararg chars: Char): String = trimLeading { it in */ public fun String.trimTrailing(vararg chars: Char): String = trimTrailing { it in chars } -/** - * If this string starts with the given [prefix], returns a copy of this string - * with the prefix removed. Otherwise, returns this string. - */ -public fun String.trimLeading(prefix: String): String { - var answer = this - if (answer.startsWith(prefix)) { - answer = answer.substring(prefix.length()) - } - return answer -} +deprecated("Use removePrefix() instead") +public fun String.trimLeading(prefix: String): String = removePrefix(prefix) -/** - * If this string ends with the given [postfix], returns a copy of this string - * with the postfix removed. Otherwise, returns this string. - */ -public fun String.trimTrailing(postfix: String): String { - var answer = this - if (answer.endsWith(postfix)) { - answer = answer.substring(0, length() - postfix.length()) - } - return answer -} +deprecated("Use removeSuffix() instead") +public fun String.trimTrailing(postfix: String): String = removeSuffix(postfix) /** * Returns a string with leading and trailing whitespace trimmed. @@ -356,6 +340,47 @@ public fun String.removeRange(firstIndex: Int, lastIndex: Int): String { */ public fun String.removeRange(range: IntRange): String = removeRange(range.start, range.end + 1) +/** + * If this string starts with the given [prefix], returns a copy of this string + * with the prefix removed. Otherwise, returns this string. + */ +public fun String.removePrefix(prefix: String): String { + if (startsWith(prefix)) { + return substring(prefix.length()) + } + return this +} + +/** + * If this string ends with the given [suffix], returns a copy of this string + * with the suffix removed. Otherwise, returns this string. + */ +public fun String.removeSuffix(suffix: String): String { + if (endsWith(suffix)) { + return substring(0, length() - suffix.length()) + } + return this +} + +/** + * Removes from a string both the given [prefix] and [suffix] if and only if + * it starts with the [prefix] and ends with the [suffix]. + * Otherwise returns this string unchanged. + */ +public fun String.removeEnclosing(prefix: String, suffix: String): String { + if (startsWith(prefix) && endsWith(suffix)) { + return substring(prefix.length(), length() - suffix.length()) + } + return this +} + +/** + * Removes the given [enclosing] string both from the start and the end of this string + * if and only if it starts with and ends with the [enclosing]. + * Otherwise returns this string unchanged. + */ +public fun String.removeEnclosing(enclosing: String): String = removeEnclosing(enclosing, enclosing) + /** * Replace part of string before the first occurrence of given delimiter with the [replacement] string. * If the string does not contain the delimiter, returns [missingDelimiterValue] which defaults to the original string. diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 387d6c29007..ac998ec101c 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -242,4 +242,26 @@ class StringTest { } } + test fun removePrefix() { + assertEquals("fix", "prefix".removePrefix("pre"), "Removes prefix") + assertEquals("prefix", "preprefix".removePrefix("pre"), "Removes prefix once") + assertEquals("sample", "sample".removePrefix("value")) + assertEquals("sample", "sample".removePrefix("")) + } + + test fun removeSuffix() { + assertEquals("suf", "suffix".removeSuffix("fix"), "Removes suffix") + assertEquals("suffix", "suffixfix".removeSuffix("fix"), "Removes suffix once") + assertEquals("sample", "sample".removeSuffix("value")) + assertEquals("sample", "sample".removeSuffix("")) + } + + test fun removeEnclosing() { + assertEquals("value", "".removeEnclosing("<", ">")) + assertEquals("", "<>".removeEnclosing("<", ">"), "Removes enclosing once") + assertEquals(""), "Only removes enclosing when both prefix and suffix present") + assertEquals("value>", "value>".removeEnclosing("<", ">"), "Only removes enclosing when both prefix and suffix present") + assertEquals("value", "value".removeEnclosing("<", ">")) + } + }