All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.xml.EscapedString.kt Maven / Gradle / Ivy

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)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy