Mercurial > cgi-bin > hgweb.cgi > JpegWasher
comparison src/name/blackcap/exifwasher/Whitelist.kt @ 2:efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 01 Apr 2020 14:23:54 -0700 |
parents | |
children | 19c381c536ec |
comparison
equal
deleted
inserted
replaced
1:42277ce58ace | 2:efd9fe2d70d7 |
---|---|
1 /* | |
2 * An exif key whitelist. Supports both prefixes and entire strings. | |
3 */ | |
4 package name.blackcap.exifwasher | |
5 | |
6 import java.util.regex.Pattern | |
7 import kotlin.collections.mutableSetOf | |
8 import kotlin.collections.mutableListOf | |
9 | |
10 class Whitelist { | |
11 private val entire = mutableSetOf<String>() | |
12 private val prefixes = mutableListOf<String>() | |
13 | |
14 fun addEntire(s: String) = entire.add(s) | |
15 | |
16 fun addPrefix(s: String) = prefixes.add(s) | |
17 | |
18 private fun autoOp(s: String, pfx: (String) -> Boolean, ent: (String) -> Boolean): Boolean { | |
19 return if (s.endsWith('*')) { pfx(s.dropLast(1)) } else { ent(s) } | |
20 } | |
21 | |
22 fun add(s: String) = autoOp(s, ::addPrefix, ::addEntire) | |
23 | |
24 fun removeEntire(s: String) = entire.remove(s) | |
25 | |
26 fun removePrefix(s: String) = prefixes.remove(s) | |
27 | |
28 fun remove(s: String) = autoOp(s, ::removePrefix, ::removeEntire) | |
29 | |
30 fun contains(s: String) = entire.contains(s) || prefixes.find { s.startsWith(it) } != null | |
31 | |
32 fun toList(): List<String> = mutableListOf<String>().also { | |
33 it.addAll(entire) | |
34 it.addAll(prefixes) | |
35 it.sort() | |
36 } | |
37 | |
38 override fun toString(): String = toList().joinToString(",") | |
39 | |
40 companion object { | |
41 private val SPLITTER = Pattern.compile(",\\s*") | |
42 fun parse(raw: String) = Whitelist().also { | |
43 for (s in raw.split(SPLITTER)) { | |
44 it.add(s) | |
45 } | |
46 } | |
47 } | |
48 } |