comparison src/main/kotlin/name/blackcap/passman/See.kt @ 4:02b101422726

See bugs.
author David Barts <n5jrn@me.com>
date Sun, 11 Sep 2022 20:44:59 -0700
parents eafa3779aef8
children ad997df1f560
comparison
equal deleted inserted replaced
3:eafa3779aef8 4:02b101422726
5 something cruder than the below. Le sigh. */ 5 something cruder than the below. Le sigh. */
6 6
7 import java.util.Formatter 7 import java.util.Formatter
8 8
9 private const val DELIM = '"' 9 private const val DELIM = '"'
10 private const val SHY = '\u00ad'
10 11
11 private val STD_ESC_MAP = mapOf<Char, Char>('\t' to 't', '\b' to 'b', '\n' to 'n', 12 private val STD_ESC_MAP = mapOf<Char, Char>('\t' to 't', '\b' to 'b', '\n' to 'n',
12 '\r' to 'r', '\u000c' to 'f', '\"' to '"', '\\' to '\\') 13 '\r' to 'r', '\u000c' to 'f', '"' to '"', '\\' to '\\')
14 private val BANNED = setOf<Char>(DELIM, SHY, '\\')
13 private const val MIN_ASCII = ' ' 15 private const val MIN_ASCII = ' '
14 private const val MAX_ASCII = '~' 16 private const val MAX_ASCII = '~'
15 private const val MIN_8859 = '\u00a1' 17 private const val MIN_8859 = '\u00a1'
16 private const val MAX_8859 = '\u00ff' 18 private const val MAX_8859 = '\u00ff'
17 private const val SHY = '\u00ad'
18 19
19 fun see(input: String, simple: Boolean = false): String = 20 fun see(input: String, simple: Boolean = false): String =
20 if (simple) seeSimple(input) else seeAggressive(input) 21 if (simple) seeSimple(input) else seeAggressive(input)
21 22
22 private fun seeSimple(input: String): String = StringBuilder().run { 23 private fun seeSimple(input: String): String = StringBuilder().run {
28 29
29 private fun seeAggressive(input: String): String { 30 private fun seeAggressive(input: String): String {
30 val accum = Formatter() 31 val accum = Formatter()
31 accum.format("%c", DELIM) 32 accum.format("%c", DELIM)
32 for (ch in input) { 33 for (ch in input) {
33 if ((ch in MIN_ASCII..MAX_ASCII) || ((ch != SHY) && (ch in MIN_8859 .. MAX_8859))) { 34 if ((ch !in BANNED) && ((ch in MIN_ASCII..MAX_ASCII) || (ch in MIN_8859 .. MAX_8859))) {
34 accum.format("%c", ch) 35 accum.format("%c", ch)
35 continue 36 continue
36 } 37 }
37 val mapped = STD_ESC_MAP[ch] 38 val mapped = STD_ESC_MAP[ch]
38 if (mapped != null) { 39 if (mapped != null) {