From 34c24eb71008b6379ef05f16093e89f0032d809d Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 17 Feb 2020 14:25:45 +0100 Subject: [PATCH] Adding core/dictionary --- CMakeLists.txt | 1 + src/core/dictionary.cpp | 105 ++++++++++++++++++++++++++++++++++++++++ src/core/dictionary.h | 72 +++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 src/core/dictionary.cpp create mode 100644 src/core/dictionary.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8854383..b38fe0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ include(GNUInstallDirs) set (PROGRAM_EXE ${CMAKE_PROJECT_NAME}) set (PROGRAM_SOURCE + src/core/dictionary.cpp src/string.cpp src/base58.cpp src/WIF.cpp diff --git a/src/core/dictionary.cpp b/src/core/dictionary.cpp new file mode 100644 index 0000000..eaa9c8b --- /dev/null +++ b/src/core/dictionary.cpp @@ -0,0 +1,105 @@ + +#include +#include +#include +#include "../string.h" +#include "dictionary.h" + +namespace eoskeygen { + +struct StringContains { + StringContains(const std::string& str, std::vector& pos) : m_str(str), m_pos(pos) {} + bool operator()(const std::string& w) { + for(size_t p = m_str.find(w); p != std::string::npos; p = m_str.find(w, p+1)) { + m_pos.push_back(p); + } + return !m_pos.empty(); + } + std::string m_str; + std::vector& m_pos; +}; + +bool Dictionary::loadFromFile(const std::string& filename) +{ + FILE *fd; + char buf[1024]; + + fd = fopen(filename.c_str(), "r"); + if (!fd) { + return false; + } + + // Clear before adding. + clear(); + + // Read each line and add to the dictionary. + while(fgets(buf, sizeof(buf), fd) != NULL) { + std::string word(buf); + add(trim(word)); + } + + fclose(fd); + return true; +} + +void Dictionary::add(const std::string& word) +{ + // Do not insert a empty string. + if (word.length()) { + m_words.insert(word); + } +} + +void Dictionary::add(const Dictionary& dictionary) +{ + std::set_union( + m_words.begin(), m_words.end(), + dictionary.m_words.begin(), dictionary.m_words.end(), + std::inserter(m_words, m_words.begin()) + ); +} + +void Dictionary::clear() +{ + m_words.clear(); +} + +bool Dictionary::contains(const std::string& word) const +{ + return m_words.find(word) != m_words.cend(); +} + +Dictionary::search_result_t Dictionary::search(const std::string& subject) const +{ + search_result_t res; + + std::vector pos; + StringContains pred(subject, pos); + + // Find all words. + for(auto it = std::find_if(m_words.begin(), m_words.end(), pred); + it != m_words.end(); + it = std::find_if(++it, m_words.end(), pred)) { + + // Go through all found positions. + for (auto it2 = pos.begin(); it2 != pos.end(); it2++) { + + // Insert + auto rit = res.find(*it2); + if (rit == res.end()) { + res.emplace(*it2, it->length()); + } + // Update length if it's longer then the previous we found. + else if (rit->second < it->length()) { + rit->second = it->length(); + } + } + + // Clear positions + pos.clear(); + } + + return res; +} + +} // namespace eoskeygen diff --git a/src/core/dictionary.h b/src/core/dictionary.h new file mode 100644 index 0000000..baa5f7a --- /dev/null +++ b/src/core/dictionary.h @@ -0,0 +1,72 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_DICTIONARY_H +#define EOSIOKEYGEN_DICTIONARY_H + +#include +#include +#include +#include + +namespace eoskeygen { + +class Dictionary +{ +public : + //typedef std::map< std::string, std::vector > search_result_t; + + // index = position in the search string. + // value = length of the word from this position. + typedef std::map< size_t, size_t > search_result_t; + +public : + + // Load words from file. + bool loadFromFile(const std::string& filename); + + // Add a word + void add(const std::string& word); + + // Add words from another dictionary. + void add(const Dictionary& dictionary); + + void clear(); + + // Returns true if word exists in the dictionary. + bool contains(const std::string& word) const; + + // Searches the subject for words defined in the dictionary. + // Returns a map with the word as key and a vector + // of each position the word was found. + search_result_t search(const std::string& subject) const; + +protected : + + // Words in the dictionary. + std::set m_words; +}; + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_DICTIONARY_H */