My Project
tr.h
1 #ifndef KTR_H_INCLUDED_
2 #define KTR_H_INCLUDED_
3 
4 #include "kblib/kblib.h"
5 #include "tinyutf8.h"
6 #include "utf8.h"
7 
8 namespace tr {
9 
10 std::u32string expand_set(std::u32string_view set);
11 
12 std::string filter(std::string whitelist, std::string_view input);
13 std::string translate(std::string_view set1, std::string set2,
14  std::string_view input);
15 
16 inline std::u32string widen(std::string_view str) {
17  std::u32string ret;
18  utf8::utf8to32(str.begin(), str.end(), std::back_inserter(ret));
19  return ret;
20 }
21 
22 inline std::string narrow(std::u32string_view str) {
23  std::string ret;
24  utf8::utf32to8(str.begin(), str.end(), std::back_inserter(ret));
25  return ret;
26 }
27 
28 inline bool match(std::string_view whitelist, std::string_view input) {
29  // if every character in the input is present in the whitelist
30  return widen(input).find_first_not_of(expand_set(widen(whitelist))) ==
31  std::u32string::npos;
32 }
33 
34 inline bool find(std::u32string_view set, char32_t c) {
35  auto p = std::find(set.begin(), set.end(), c);
36  return p != set.end();
37 }
38 
39 #if 0
40 //Currently, only filter. match, and translate are needed by the program.
41 
42 enum Mode : unsigned {
43  def = 0,
44 
45  complement = 1,
46  c = complement,
47  C = complement,
48 
49  del = 2,
50  d = del,
51 
52  squeeze = 4,
53  s = squeeze,
54 
55  truncate = 8,
56  t = truncate,
57 };
58 
59 template <typename Char>
60 std::basic_string<Char> tr(Mode mode, std::basic_string_view<Char> set1, std::basic_string_view<Char> set2, std::basic_string_view<Char> input)
61 {
62  if (mode == tr::squeeze) {
63 
64  } else if (mode & tr::complement) {
65 
66  } else if (mode & tr::del) {
67 
68  } else {
69 
70  }
71 }
72 
73 template <Mode mode, typename Char>
74 std::basic_string<Char> tr(std::basic_string_view<Char> set1, std::basic_string_view<Char> set2, std::basic_string_view<Char> input)
75 {
76  if constexpr (mode == tr::def) {
77  return translate(set1, set2, input);
78  } else if constexpr (mode == (tr::c | tr::d)) {
79  return filter(set1, input);
80  }
81 }
82 #endif
83 
84 } // namespace tr
85 
86 #endif // KTR_H_INCLUDED_
Definition: tr.cpp:5
Definition: fda.h:15