mirror of
https://github.com/eosswedenorg/antelope-keygen
synced 2026-06-17 03:50:03 +02:00
Adding core/dictionary
This commit is contained in:
parent
fcbe784fc8
commit
34c24eb710
3 changed files with 178 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
105
src/core/dictionary.cpp
Normal file
105
src/core/dictionary.cpp
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#include "../string.h"
|
||||
#include "dictionary.h"
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
struct StringContains {
|
||||
StringContains(const std::string& str, std::vector<size_t>& 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<size_t>& 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<size_t> 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
|
||||
72
src/core/dictionary.h
Normal file
72
src/core/dictionary.h
Normal file
|
|
@ -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 <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace eoskeygen {
|
||||
|
||||
class Dictionary
|
||||
{
|
||||
public :
|
||||
//typedef std::map< std::string, std::vector<size_t> > 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<int>
|
||||
// of each position the word was found.
|
||||
search_result_t search(const std::string& subject) const;
|
||||
|
||||
protected :
|
||||
|
||||
// Words in the dictionary.
|
||||
std::set<std::string> m_words;
|
||||
};
|
||||
|
||||
} // namespace eoskeygen
|
||||
|
||||
#endif /* EOSIOKEYGEN_DICTIONARY_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue