Skip to content

Commit bbc1438

Browse files
author
Julian R
committed
[stdlib] KT-80748 add ensurePrefix and ensureSuffix extension functions
1 parent 2ad0e16 commit bbc1438

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

libraries/stdlib/src/kotlin/text/Strings.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,28 @@ public fun String.removePrefix(prefix: CharSequence): String {
630630
return this
631631
}
632632

633+
/**
634+
* If this char sequence does not start with the given [prefix], returns a new char sequence
635+
* with the prefix added. Otherwise, returns a new char sequence with the same characters.
636+
*/
637+
public fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence {
638+
if (!this.startsWith(prefix)) {
639+
return prefix.toString() + this.toString()
640+
}
641+
return subSequence(0, length)
642+
}
643+
644+
/**
645+
* If this string does not start with the given [prefix], returns a copy of this string
646+
* with the prefix added. Otherwise, returns this string.
647+
*/
648+
public fun String.ensurePrefix(prefix: CharSequence): String {
649+
if (!this.startsWith(prefix)) {
650+
return prefix.toString() + this
651+
}
652+
return this
653+
}
654+
633655
/**
634656
* If this char sequence ends with the given [suffix], returns a new char sequence
635657
* with the suffix removed. Otherwise, returns a new char sequence with the same characters.
@@ -652,6 +674,28 @@ public fun String.removeSuffix(suffix: CharSequence): String {
652674
return this
653675
}
654676

677+
/**
678+
* If the char sequence not ends with the given [suffix], returns a copy of this string
679+
* with the suffix appended. Otherwise, returns a new char sequence with the same characters.
680+
*/
681+
public fun CharSequence.ensureSuffix(suffix: CharSequence): CharSequence {
682+
if (!endsWith(suffix)) {
683+
return this.toString() + suffix.toString()
684+
}
685+
return subSequence(0, length)
686+
}
687+
688+
/**
689+
* If the string not ends with the given [suffix], returns a copy of this string
690+
* with the suffix appended. Otherwise, returns this string.
691+
*/
692+
public fun String.ensureSuffix(suffix: CharSequence): String {
693+
if (!endsWith(suffix)) {
694+
return this + suffix.toString()
695+
}
696+
return this
697+
}
698+
655699
/**
656700
* When this char sequence starts with the given [prefix] and ends with the given [suffix],
657701
* returns a new char sequence having both the given [prefix] and [suffix] removed.

libraries/stdlib/test/text/StringTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,18 @@ class StringTest {
596596
assertEquals("sample", "sample".removeSuffix(""))
597597
}
598598

599+
@Test fun ensureSuffix() = withOneCharSequenceArg("fix") { suffix ->
600+
assertEquals("suffix", "suf".ensureSuffix(suffix), "Appends suffix")
601+
assertEquals("suffix", "suffix".ensureSuffix(suffix))
602+
assertEquals("sample", "sample".ensureSuffix(""))
603+
}
604+
605+
@Test fun ensurePrefix() = withOneCharSequenceArg("suf") { prefix ->
606+
assertEquals("suffix", "fix".ensurePrefix(prefix), "Appends prefix")
607+
assertEquals("suffix", "suffix".ensurePrefix(prefix))
608+
assertEquals("sample", "sample".ensurePrefix(""))
609+
}
610+
599611
@Test fun removeSurrounding() = withOneCharSequenceArg { arg1 ->
600612
val pre = arg1("<")
601613
val post = arg1(">")
@@ -628,6 +640,24 @@ class StringTest {
628640
assertContentEquals("sample", "sample".removeSuffix(""))
629641
}
630642

643+
@Test fun ensurePrefixCharSequence() = withTwoCharSequenceArgs { arg1, arg2 ->
644+
fun String.ensurePrefix(prefix: String) = arg1(this).ensurePrefix(arg2(prefix))
645+
val prefix = "suf"
646+
647+
assertContentEquals("suffix", "fix".ensurePrefix(prefix), "Appends prefix")
648+
assertContentEquals("suffix", "suffix".ensurePrefix(prefix))
649+
assertContentEquals("sample", "sample".ensurePrefix(""))
650+
}
651+
652+
@Test fun ensureSuffixCharSequence() = withTwoCharSequenceArgs { arg1, arg2 ->
653+
fun String.ensureSuffix(suffix: String) = arg1(this).ensureSuffix(arg2(suffix))
654+
val suffix = "fix"
655+
656+
assertContentEquals("suffix", "suf".ensureSuffix(suffix), "Appends suffix")
657+
assertContentEquals("suffix", "suffix".ensureSuffix(suffix))
658+
assertContentEquals("sample", "sample".ensureSuffix(""))
659+
}
660+
631661
@Test fun removeSurroundingCharSequence() = withTwoCharSequenceArgs { arg1, arg2 ->
632662
fun String.removeSurrounding(prefix: String, postfix: String) = arg1(this).removeSurrounding(arg2(prefix), arg2(postfix))
633663

0 commit comments

Comments
 (0)