Do not allow removeSurrounding remove overlapping prefix and suffix.
This commit is contained in:
@@ -534,7 +534,7 @@ public fun String.removeSuffix(suffix: CharSequence): String {
|
||||
* Otherwise returns a new char sequence with the same characters.
|
||||
*/
|
||||
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(0, length)
|
||||
@@ -546,7 +546,7 @@ public fun CharSequence.removeSurrounding(prefix: CharSequence, suffix: CharSequ
|
||||
* Otherwise returns this string unchanged.
|
||||
*/
|
||||
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 this
|
||||
|
||||
@@ -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))
|
||||
|
||||
assertEquals("<->", "<->".removeSurrounding(arg1("<-"), arg1("->")), "Does not remove overlapping prefix and suffix")
|
||||
}
|
||||
|
||||
@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("<", ">"))
|
||||
|
||||
assertContentEquals("<->", "<->".removeSurrounding("<-", "->"), "Does not remove overlapping prefix and suffix")
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user