commonMain.xml.EscapedString.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kanvas-jvm Show documentation
Show all versions of kanvas-jvm Show documentation
A collection of drawing/charting utilities
The newest version!
package com.juul.krayon.kanvas.xml
internal class EscapedString private constructor(private val string: String) {
override fun toString(): String = string
override fun hashCode(): Int = string.hashCode()
override fun equals(other: Any?): Boolean = this === other || (other is EscapedString && string == other.string)
companion object {
fun encode(source: String): EscapedString = EscapedString(
// Assume no escaping for starting capacity.
buildString(capacity = source.length) {
for (value in source) {
when (value) {
'<' -> append("<")
'>' -> append(">")
'&' -> append("&")
'\'' -> append("'")
'"' -> append(""")
else -> append(value)
}
}
},
)
}
}
internal fun String.escape(): EscapedString = EscapedString.encode(this)