Mercurial > cgi-bin > hgweb.cgi > PassMan
view src/main/kotlin/name/blackcap/passman/See.kt @ 3:eafa3779aef8
More bug fixes, quote strings in diagnostics.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 11 Sep 2022 20:36:06 -0700 |
parents | a6cfdffcaa94 |
children | 02b101422726 |
line wrap: on
line source
package name.blackcap.passman /* Would be nice if there was a more programmatic and comprehensive way of doing this, but alas. Even the oft-recommended StringEscapeUtils does something cruder than the below. Le sigh. */ import java.util.Formatter private const val DELIM = '"' private val STD_ESC_MAP = mapOf<Char, Char>('\t' to 't', '\b' to 'b', '\n' to 'n', '\r' to 'r', '\u000c' to 'f', '\"' to '"', '\\' to '\\') private const val MIN_ASCII = ' ' private const val MAX_ASCII = '~' private const val MIN_8859 = '\u00a1' private const val MAX_8859 = '\u00ff' private const val SHY = '\u00ad' fun see(input: String, simple: Boolean = false): String = if (simple) seeSimple(input) else seeAggressive(input) private fun seeSimple(input: String): String = StringBuilder().run { append(DELIM) append(input) append(DELIM) toString() } private fun seeAggressive(input: String): String { val accum = Formatter() accum.format("%c", DELIM) for (ch in input) { if ((ch in MIN_ASCII..MAX_ASCII) || ((ch != SHY) && (ch in MIN_8859 .. MAX_8859))) { accum.format("%c", ch) continue } val mapped = STD_ESC_MAP[ch] if (mapped != null) { accum.format("\\%c", mapped) } else { accum.format("\\u%04x", ch.code) } } accum.format("%c", DELIM) return accum.toString() }