36 #ifndef LANGUAGE_BASE_H 37 #define LANGUAGE_BASE_H 40 #include <unordered_map> 42 #include <boost/algorithm/string.hpp> 63 size_t avail = s.size();
64 const char *ptr = s.data();
65 while (
count-- && avail--)
68 while (avail && ((*ptr) & 0xc0) == 0x80)
81 size_t avail = s.size();
82 const char *ptr = s.data();
88 if ((*ptr & 0x80) == 0)
93 else if ((*ptr & 0xe0) == 0xc0)
96 throw std::runtime_error(
"Invalid UTF-8");
97 cp = (*ptr++ & 0x1f) << 6;
102 else if ((*ptr & 0xf0) == 0xe0)
105 throw std::runtime_error(
"Invalid UTF-8");
106 cp = (*ptr++ & 0xf) << 12;
107 cp |= (*ptr++ & 0x3f) << 6;
112 else if ((*ptr & 0xf8) == 0xf0)
115 throw std::runtime_error(
"Invalid UTF-8");
116 cp = (*ptr++ & 0x7) << 18;
117 cp |= (*ptr++ & 0x3f) << 12;
118 cp |= (*ptr++ & 0x3f) << 6;
124 throw std::runtime_error(
"Invalid UTF-8");
126 cp = std::towlower(cp);
130 case 1: *wptr++ = cp;
break;
131 case 2: *wptr++ = 0xc0 | (cp >> 6); *wptr++ = 0x80 | (cp & 0x3f);
break;
132 case 3: *wptr++ = 0xe0 | (cp >> 12); *wptr++ = 0x80 | ((cp >> 6) & 0x3f); *wptr++ = 0x80 | (cp & 0x3f);
break;
133 case 4: *wptr++ = 0xf0 | (cp >> 18); *wptr++ = 0x80 | ((cp >> 12) & 0x3f); *wptr++ = 0x80 | ((cp >> 6) & 0x3f); *wptr++ = 0x80 | (cp & 0x3f);
break;
134 default:
throw std::runtime_error(
"Invalid UTF-8");
137 sc +=
T(wbuf, bytes);
179 std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual>
word_map;
190 std::vector<std::string>::const_iterator it;
192 throw std::runtime_error(
"Wrong word list length for " +
language_name);
201 throw std::runtime_error(
"Too short word in " +
language_name +
" word list: " + *it);
236 for (
size_t i = 0; i <
NWORDS; ++i)
251 const std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual>&
get_word_map()
const const std::vector< std::string > & get_word_list() const
Returns a pointer to the word list.
const std::string & get_language_name() const
Returns the name of the language.
size_t size() const noexcept
T utf8canonical(const T &s)
Mnemonic language related namespace.
Base(const char *language_name, const char *english_language_name, const std::vector< std::string > &words, uint32_t prefix_length)
std::vector< std::string > word_list
A base language class which all languages have to inherit from for Polymorphism.
const std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > & get_trimmed_word_map() const
Returns a pointer to the trimmed word map.
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > word_map
bool operator()(const epee::wipeable_string &s0, const epee::wipeable_string &s1) const
std::string language_name
mdb_size_t count(MDB_cursor *cur)
const std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > & get_word_map() const
Returns a pointer to the word map.
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > trimmed_word_map
uint64_t FNV1a(const char *ptr, size_t sz)
std::size_t operator()(const epee::wipeable_string &s) const
std::string english_language_name
const std::string & get_english_language_name() const
Returns the name of the language in English.
const char * data() const noexcept
void populate_maps(uint32_t flags=0)
Populates the word maps after the list is ready.
uint32_t get_unique_prefix_length() const
Returns the number of unique starting characters to be used for matching.
uint32_t unique_prefix_length
T utf8prefix(const T &s, size_t count)
Returns a string made of (at most) the first count characters in s. Assumes well formedness. No check is made for this.
void set_words(const char *const words[])