mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-09 08:31:29 +00:00
17 lines
400 B
Kotlin
17 lines
400 B
Kotlin
fun escapeChar(c : Char) : String? = when (c) {
|
|
'\\' -> "\\\\"
|
|
'\n' -> "\\n"
|
|
'"' -> "\\\""
|
|
else -> "" + c
|
|
}
|
|
|
|
tailRecursive fun String.escape(i : Int = 0, result : StringBuilder = StringBuilder()) : String =
|
|
if (i == length) result.toString()
|
|
else escape(i + 1, result.append(escapeChar(get(i))))
|
|
|
|
fun box() : String {
|
|
"test me not \\".escape()
|
|
return "OK"
|
|
}
|
|
|