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

commonMain.com.bkahlert.kommons.quoted.kt Maven / Gradle / Ivy

Go to download

Kommons Core is a Kotlin Multiplatform Library that offers shared features for all Kommons modules.

There is a newer version: 2.8.0
Show newest version
package com.bkahlert.kommons

/** This string escaped and wrapped with double quotes. */
public val Char.quoted: String get() = buildString { [email protected](this) }

/** This string escaped and wrapped with double quotes. */
public val CharSequence.quoted: String get() = buildString { [email protected](this) }

/**
 * The string returned by [Any.toString] escaped and wrapped with double quotes,
 * or the string "null" with no quotes if this object is `null`.
 */
public val Any?.quoted: String get() = this?.toString()?.quoted ?: "null"

/** Appends this character escaped and wrapped with double quotes to the specified [out]. */
private fun Char.quoteTo(out: StringBuilder) {
    out.append("\"")
    escapeTo(out)
    out.append("\"")
}

/** Appends this string escaped and wrapped with double quotes to the specified [out]. */
private fun CharSequence.quoteTo(out: StringBuilder) {
    out.append("\"")
    for (element in this) element.escapeTo(out)
    out.append("\"")
}

/** Appends this character escaped to the specified [out]. */
@Suppress("NOTHING_TO_INLINE")
private inline fun Char.escapeTo(out: StringBuilder) {
    when (this) {
        '\\' -> out.append("\\\\")
        '\n' -> out.append("\\n")
        '\r' -> out.append("\\r")
        '\t' -> out.append("\\t")
        '\"' -> out.append("\\\"")
        else -> out.append(this)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy