com.autonomousapps.internal.utils.strings.strings.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-analysis-gradle-plugin Show documentation
Show all versions of dependency-analysis-gradle-plugin Show documentation
Analyzes dependency usage in Android and JVM projects
package com.autonomousapps.internal.utils.strings
/**
* Replaces every instance of [oldValue] with [newValue], except the last one.
*
* Much of this implementation was borrowed from `StringsJVM`'s [String.replace].
*/
fun String.replaceExceptLast(oldValue: String, newValue: String, ignoreCase: Boolean = false): String {
val lastIndex = lastIndexOf(oldValue, ignoreCase = ignoreCase)
if (lastIndex == -1) return this
var occurrenceIndex: Int = indexOf(oldValue, 0, ignoreCase)
// FAST PATH: no match
if (occurrenceIndex < 0) return this
val oldValueLength = oldValue.length
val searchStep = oldValueLength.coerceAtLeast(1)
val newLengthHint = length - oldValueLength + newValue.length
if (newLengthHint < 0) throw OutOfMemoryError()
val stringBuilder = StringBuilder(newLengthHint)
var i = 0
do {
stringBuilder.append(this, i, occurrenceIndex).append(newValue)
i = occurrenceIndex + oldValueLength
if (occurrenceIndex >= length) break
occurrenceIndex = indexOf(oldValue, occurrenceIndex + searchStep, ignoreCase)
} while (occurrenceIndex > 0 && occurrenceIndex != lastIndex)
return stringBuilder.append(this, i, length).toString()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy