comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:a6cfdffcaa94
1 package name.blackcap.passman
2
3 import java.util.Formatter
4
5 private val UNPRINTABLE = setOf<Character.UnicodeBlock>(
6 Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS,
7 Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS_EXTENDED,
8 Character.UnicodeBlock.COMBINING_HALF_MARKS,
9 Character.UnicodeBlock.COMBINING_DIACRITICAL_MARKS_SUPPLEMENT,
10 Character.UnicodeBlock.COMBINING_MARKS_FOR_SYMBOLS,
11 Character.UnicodeBlock.HIGH_PRIVATE_USE_SURROGATES,
12 Character.UnicodeBlock.HIGH_SURROGATES,
13 Character.UnicodeBlock.LOW_SURROGATES,
14 Character.UnicodeBlock.PRIVATE_USE_AREA,
15 Character.UnicodeBlock.SPECIALS,
16
17 )
18
19 private val DELIM = '"'
20
21 private val EXEMPT = setOf<Char>(' ')
22 private val PREFIXED = setOf<Char>(DELIM, '\\')
23
24 fun see(input: String): String {
25 val accum = StringBuilder()
26 val formatter = Formatter(accum)
27 accum.append(DELIM)
28 for (ch in input) {
29 val block = Character.UnicodeBlock.of(ch)
30 if (ch in EXEMPT) {
31 accum.append(ch)
32 } else if (block == null || block in UNPRINTABLE || Character.isSpaceChar(ch) || Character.isWhitespace(ch)) {
33 formatter.format("\\u%04x", ch.code)
34 } else if (ch in PREFIXED) {
35 accum.append('\\')
36 accum.append(ch)
37 } else {
38 accum.append(ch)
39 }
40 }
41 accum.append(DELIM)
42 return accum.toString()
43 }
44
45 fun dump(input: String): String {
46 val accum = StringBuilder()
47 var needSpace = false
48 for (ch in input) {
49 if (needSpace) {
50 accum.append(' ')
51 }
52 accum.append(ch.code)
53 needSpace = true
54 }
55 return accum.toString()
56 }