| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#ifndef LANGUAGE_BASE_H |
| 36 |
#define LANGUAGE_BASE_H |
| 37 |
|
| 38 |
#include <vector> |
| 39 |
#include <unordered_map> |
| 40 |
#include <string> |
| 41 |
#include <boost/algorithm/string.hpp> |
| 42 |
#include "misc_log_ex.h" |
| 43 |
#include "fnv1.h" |
| 44 |
#include "common/utf8.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
namespace Language |
| 51 |
{ |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
template<typename T> |
| 60 |
inline T utf8prefix(const T &s, size_t count) |
| 61 |
{ |
| 62 |
T prefix = ""; |
| 63 |
size_t avail = s.size(); |
| 64 |
const char *ptr = s.data(); |
| 65 |
while (count-- && avail--) |
| 66 |
{ |
| 67 |
prefix += *ptr++; |
| 68 |
while (avail && ((*ptr) & 0xc0) == 0x80) |
| 69 |
{ |
| 70 |
prefix += *ptr++; |
| 71 |
--avail; |
| 72 |
} |
| 73 |
} |
| 74 |
return prefix; |
| 75 |
} |
| 76 |
|
| 77 |
struct WordHash |
| 78 |
{ |
| 79 |
std::size_t operator()(const epee::wipeable_string &s) const |
| 80 |
{ |
| 81 |
const epee::wipeable_string sc = tools::utf8canonical(s, [](wint_t c) -> wint_t { return std::towlower(c); }); |
| 82 |
return epee::fnv::FNV1a(sc.data(), sc.size()); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
struct WordEqual |
| 87 |
{ |
| 88 |
bool operator()(const epee::wipeable_string &s0, const epee::wipeable_string &s1) const |
| 89 |
{ |
| 90 |
const epee::wipeable_string s0c = tools::utf8canonical(s0, [](wint_t c) -> wint_t { return std::towlower(c); }); |
| 91 |
const epee::wipeable_string s1c = tools::utf8canonical(s1, [](wint_t c) -> wint_t { return std::towlower(c); }); |
| 92 |
return s0c == s1c; |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
class Base |
| 102 |
{ |
| 103 |
protected: |
| 104 |
enum { |
| 105 |
ALLOW_SHORT_WORDS = 1<<0, |
| 106 |
ALLOW_DUPLICATE_PREFIXES = 1<<1, |
| 107 |
}; |
| 108 |
enum { |
| 109 |
NWORDS = 1626 |
| 110 |
}; |
| 111 |
std::vector<std::string> word_list; |
| 112 |
std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual> word_map; |
| 113 |
std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual> trimmed_word_map; |
| 114 |
std::string language_name; |
| 115 |
std::string english_language_name; |
| 116 |
uint32_t unique_prefix_length; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
void populate_maps(uint32_t flags = 0) |
| 121 |
{ |
| 122 |
int ii; |
| 123 |
std::vector<std::string>::const_iterator it; |
| 124 |
if (word_list.size () != NWORDS) |
| 125 |
throw std::runtime_error("Wrong word list length for " + language_name); |
| 126 |
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++) |
| 127 |
{ |
| 128 |
word_map[*it] = ii; |
| 129 |
if ((*it).size() < unique_prefix_length) |
| 130 |
{ |
| 131 |
if (flags & ALLOW_SHORT_WORDS) |
| 132 |
MINFO(language_name << " word '" << *it << "' is shorter than its prefix length, " << unique_prefix_length); |
| 133 |
else |
| 134 |
throw std::runtime_error("Too short word in " + language_name + " word list: " + *it); |
| 135 |
} |
| 136 |
epee::wipeable_string trimmed; |
| 137 |
if (it->length() > unique_prefix_length) |
| 138 |
{ |
| 139 |
trimmed = utf8prefix(*it, unique_prefix_length); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
trimmed = *it; |
| 144 |
} |
| 145 |
if (trimmed_word_map.find(trimmed) != trimmed_word_map.end()) |
| 146 |
{ |
| 147 |
if (flags & ALLOW_DUPLICATE_PREFIXES) |
| 148 |
MWARNING("Duplicate prefix in " << language_name << " word list: " << std::string(trimmed.data(), trimmed.size())); |
| 149 |
else |
| 150 |
throw std::runtime_error("Duplicate prefix in " + language_name + " word list: " + std::string(trimmed.data(), trimmed.size())); |
| 151 |
} |
| 152 |
trimmed_word_map[trimmed] = ii; |
| 153 |
} |
| 154 |
} |
| 155 |
public: |
| 156 |
Base(const char *language_name, const char *english_language_name, const std::vector<std::string> &words, uint32_t prefix_length): |
| 157 |
word_list(words), |
| 158 |
unique_prefix_length(prefix_length), |
| 159 |
language_name(language_name), |
| 160 |
english_language_name(english_language_name) |
| 161 |
{ |
| 162 |
} |
| 163 |
virtual ~Base() |
| 164 |
{ |
| 165 |
} |
| 166 |
void set_words(const char * const words[]) |
| 167 |
{ |
| 168 |
word_list.resize(NWORDS); |
| 169 |
for (size_t i = 0; i < NWORDS; ++i) |
| 170 |
word_list[i] = words[i]; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
const std::vector<std::string>& get_word_list() const |
| 177 |
{ |
| 178 |
return word_list; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
const std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual>& get_word_map() const |
| 185 |
{ |
| 186 |
return word_map; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
const std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual>& get_trimmed_word_map() const |
| 193 |
{ |
| 194 |
return trimmed_word_map; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
const std::string &get_language_name() const |
| 201 |
{ |
| 202 |
return language_name; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
const std::string &get_english_language_name() const |
| 209 |
{ |
| 210 |
return english_language_name; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
uint32_t get_unique_prefix_length() const |
| 217 |
{ |
| 218 |
return unique_prefix_length; |
| 219 |
} |
| 220 |
}; |
| 221 |
} |
| 222 |
|
| 223 |
#endif |