view src/main/kotlin/name/blackcap/passman/See.kt @ 5:ad997df1f560

Fix see() to be about as good as sccc.
author David Barts <n5jrn@me.com>
date Sun, 11 Sep 2022 21:29:20 -0700
parents 02b101422726
children 72619175004e
line wrap: on
line source

package name.blackcap.passman

import java.util.Formatter

private const val DELIM = '"'
private val ALWAYS_ALLOW = setOf<Char>(' ')
private val ALWAYS_BAN = setOf<Char>(DELIM, '\\')
private val FORBIDDEN = setOf<Byte>(Character.CONTROL, Character.FORMAT,
    Character.SURROGATE, Character.PRIVATE_USE, Character.UNASSIGNED,
    Character.SPACE_SEPARATOR)
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 '\\')

fun see(input: String): String {
    val accum = Formatter()
    accum.format("%c", DELIM)
    for (ch in input) {
        if (ch in ALWAYS_ALLOW) {
            accum.format("%c", ch)
            continue
        }
        if (ch in ALWAYS_BAN || Character.getType(ch).toByte() in FORBIDDEN) {
            val mapped = STD_ESC_MAP[ch]
            if (mapped != null) {
                accum.format("\\%c", mapped)
            } else {
                accum.format("\\u%04x", ch.code)
            }
            continue
        }
        accum.format("%c", ch)
    }
    accum.format("%c", DELIM)
    return accum.toString()
}