#ifndef KTR_H_INCLUDED_ #define KTR_H_INCLUDED_ #include #include #include #include "utf8.h" namespace tr { auto expand_set(std::u32string_view set) -> std::u32string; auto filter(std::string whitelist, std::string_view input) -> std::string; std::string translate(std::string_view set1, std::string set2, std::string_view input); inline auto widen(std::string_view str) -> std::u32string { std::u32string ret; utf8::utf8to32(str.begin(), str.end(), std::back_inserter(ret)); return ret; } inline auto narrow(std::u32string_view str) -> std::string { std::string ret; utf8::utf32to8(str.begin(), str.end(), std::back_inserter(ret)); return ret; } inline auto match(std::string_view whitelist, std::string_view input) -> bool { // if every character in the input is present in the whitelist return widen(input).find_first_not_of(expand_set(widen(whitelist))) == std::u32string::npos; } inline auto find(std::u32string_view set, char32_t c) -> bool { auto p = std::find(set.begin(), set.end(), c); return p != set.end(); } #if 0 //Currently, only filter. match, and translate are needed by the program. enum Mode : unsigned { def = 0, complement = 1, c = complement, C = complement, del = 2, d = del, squeeze = 4, s = squeeze, truncate = 8, t = truncate, }; template std::basic_string tr(Mode mode, std::basic_string_view set1, std::basic_string_view set2, std::basic_string_view input) { if (mode == tr::squeeze) { } else if (mode & tr::complement) { } else if (mode & tr::del) { } else { } } template std::basic_string tr(std::basic_string_view set1, std::basic_string_view set2, std::basic_string_view input) { if constexpr (mode == tr::def) { return translate(set1, set2, input); } else if constexpr (mode == (tr::c | tr::d)) { return filter(set1, input); } } #endif } // namespace tr #endif // KTR_H_INCLUDED_