Do not allow removeSurrounding remove overlapping prefix and suffix.

This commit is contained in:
Ilya Gorbunov
2015-10-29 20:43:32 +03:00
parent 05fd2b012a
commit 1400bdc09a
2 changed files with 6 additions and 2 deletions
+2 -2
View File
@@ -534,7 +534,7 @@ public fun String.removeSuffix(suffix: CharSequence): String {
* Otherwise returns a new char sequence with the same characters. * Otherwise returns a new char sequence with the same characters.
*/ */
public fun CharSequence.removeSurrounding(prefix: CharSequence, suffix: CharSequence): CharSequence { public fun CharSequence.removeSurrounding(prefix: CharSequence, suffix: CharSequence): CharSequence {
if (startsWith(prefix) && endsWith(suffix)) { if ((length >= prefix.length + suffix.length) && startsWith(prefix) && endsWith(suffix)) {
return subSequence(prefix.length, length - suffix.length) return subSequence(prefix.length, length - suffix.length)
} }
return subSequence(0, length) return subSequence(0, length)
@@ -546,7 +546,7 @@ public fun CharSequence.removeSurrounding(prefix: CharSequence, suffix: CharSequ
* Otherwise returns this string unchanged. * Otherwise returns this string unchanged.
*/ */
public fun String.removeSurrounding(prefix: CharSequence, suffix: CharSequence): String { public fun String.removeSurrounding(prefix: CharSequence, suffix: CharSequence): String {
if (startsWith(prefix) && endsWith(suffix)) { if ((length >= prefix.length + suffix.length) && startsWith(prefix) && endsWith(suffix)) {
return substring(prefix.length, length - suffix.length) return substring(prefix.length, length - suffix.length)
} }
return this return this
+4
View File
@@ -436,6 +436,8 @@ class StringTest {
assertEquals("<value", "<value".removeSurrounding(pre, post), "Only removes surrounding when both prefix and suffix present") assertEquals("<value", "<value".removeSurrounding(pre, post), "Only removes surrounding when both prefix and suffix present")
assertEquals("value>", "value>".removeSurrounding(pre, post), "Only removes surrounding when both prefix and suffix present") assertEquals("value>", "value>".removeSurrounding(pre, post), "Only removes surrounding when both prefix and suffix present")
assertEquals("value", "value".removeSurrounding(pre, post)) assertEquals("value", "value".removeSurrounding(pre, post))
assertEquals("<->", "<->".removeSurrounding(arg1("<-"), arg1("->")), "Does not remove overlapping prefix and suffix")
} }
@test fun removePrefixCharSequence() = withTwoCharSequenceArgs { arg1, arg2 -> @test fun removePrefixCharSequence() = withTwoCharSequenceArgs { arg1, arg2 ->
@@ -466,6 +468,8 @@ class StringTest {
assertContentEquals("<value", "<value".removeSurrounding("<", ">"), "Only removes surrounding when both prefix and suffix present") assertContentEquals("<value", "<value".removeSurrounding("<", ">"), "Only removes surrounding when both prefix and suffix present")
assertContentEquals("value>", "value>".removeSurrounding("<", ">"), "Only removes surrounding when both prefix and suffix present") assertContentEquals("value>", "value>".removeSurrounding("<", ">"), "Only removes surrounding when both prefix and suffix present")
assertContentEquals("value", "value".removeSurrounding("<", ">")) assertContentEquals("value", "value".removeSurrounding("<", ">"))
assertContentEquals("<->", "<->".removeSurrounding("<-", "->"), "Does not remove overlapping prefix and suffix")
} }
/* /*