Commonize and generalize JVM-only String.contentEquals #KT-42840

This commit is contained in:
Abduqodiri Qurbonzoda
2020-09-21 02:21:23 +03:00
parent 037505d9f2
commit ed57bcb3b1
9 changed files with 198 additions and 2 deletions
@@ -1378,3 +1378,56 @@ public fun CharSequence.lineSequence(): Sequence<String> = splitToSequence("\r\n
* The lines returned do not include terminating line separators.
*/
public fun CharSequence.lines(): List<String> = lineSequence().toList()
/**
* Returns `true` if the contents of this char sequence are equal to the contents of the specified [other],
* i.e. both char sequences contain the same number of the same characters in the same order.
*
* @sample samples.text.Strings.contentEquals
*/
@SinceKotlin("1.5")
public expect infix fun CharSequence?.contentEquals(other: CharSequence?): Boolean
/**
* Returns `true` if the contents of this char sequence are equal to the contents of the specified [other], optionally ignoring case difference.
*
* @param ignoreCase `true` to ignore character case when comparing contents.
*
* @sample samples.text.Strings.contentEquals
*/
@SinceKotlin("1.5")
public expect fun CharSequence?.contentEquals(other: CharSequence?, ignoreCase: Boolean): Boolean
internal fun CharSequence?.contentEqualsIgnoreCaseImpl(other: CharSequence?): Boolean {
if (this is String && other is String) {
return this.equals(other, ignoreCase = true)
}
if (this === other) return true
if (this == null || other == null || this.length != other.length) return false
for (i in 0 until length) {
if (!this[i].equals(other[i], ignoreCase = true)) {
return false
}
}
return true
}
internal fun CharSequence?.contentEqualsImpl(other: CharSequence?): Boolean {
if (this is String && other is String) {
return this == other
}
if (this === other) return true
if (this == null || other == null || this.length != other.length) return false
for (i in 0 until length) {
if (this[i] != other[i]) {
return false
}
}
return true
}