Mercurial > cgi-bin > hgweb.cgi > PassMan
diff src/main/kotlin/name/blackcap/passman/See.kt @ 0:a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 11 Sep 2022 16:11:37 -0700 |
parents | |
children | eafa3779aef8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/kotlin/name/blackcap/passman/See.kt Sun Sep 11 16:11:37 2022 -0700 @@ -0,0 +1,56 @@ +package name.blackcap.passman + +import java.util.Formatter + +private val UNPRINTABLE = setOf<Character.UnicodeBlock>( + Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS, + Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS_EXTENDED, + Character.UnicodeBlock.COMBINING_HALF_MARKS, + Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS_SUPPLEMENT, + Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS, + Character.UnicodeBlock.HIGH_PRIVATE_USE_SURROGATES, + Character.UnicodeBlock.HIGH_SURROGATES, + Character.UnicodeBlock.LOW_SURROGATES, + Character.UnicodeBlock.PRIVATE_USE_AREA, + Character.UnicodeBlock.SPECIALS, + +) + +private val DELIM = '"' + +private val EXEMPT = setOf<Char>(' ') +private val PREFIXED = setOf<Char>(DELIM, '\\') + +fun see(input: String): String { + val accum = StringBuilder() + val formatter = Formatter(accum) + accum.append(DELIM) + for (ch in input) { + val block = Character.UnicodeBlock.of(ch) + if (ch in EXEMPT) { + accum.append(ch) + } else if (block == null || block in UNPRINTABLE || Character.isSpaceChar(ch) || Character.isWhitespace(ch)) { + formatter.format("\\u%04x", ch.code) + } else if (ch in PREFIXED) { + accum.append('\\') + accum.append(ch) + } else { + accum.append(ch) + } + } + accum.append(DELIM) + return accum.toString() +} + +fun dump(input: String): String { + val accum = StringBuilder() + var needSpace = false + for (ch in input) { + if (needSpace) { + accum.append(' ') + } + accum.append(ch.code) + needSpace = true + } + return accum.toString() +}