From 5e18c8eccf4ba05c9cce5672d8495c3904b66cd0 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Sat, 22 Feb 2020 21:03:52 +0300 Subject: [+] prefix function --- src/functions.hpp | 13 +++++++++++++ src/main.cpp | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/functions.hpp b/src/functions.hpp index 123f4ab..89e3b01 100644 --- a/src/functions.hpp +++ b/src/functions.hpp @@ -6,6 +6,7 @@ #define T_FUNCTIONS_HPP #include "sha256/picosha2.h" +#include std::string <rim(std::string &str, const std::string &chars = "\t\n\v\f\r ") { str.erase(0, str.find_first_not_of(chars)); @@ -28,4 +29,16 @@ std::string sha256_hash(std::string text) { std::string hex_str = picosha2::bytes_to_hex_string(hash.begin(), hash.end()); return hex_str; } + +std::string prefix(std::string hash, + std::unordered_map tasks) { + std::string prefix; + for (size_t i = 1; i <= hash.length(); i++) { + prefix = hash.substr(0, i); + if (tasks.find(prefix) == tasks.end()) + return prefix; + } + return hash; +} + #endif // T_FUNCTIONS_HPP diff --git a/src/main.cpp b/src/main.cpp index cc1144f..343b9d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,10 @@ #include +#include #include "functions.hpp" #include "opts/cxxopts.hpp" -using namespace std; +std::unordered_map tasks = {}; int main(int argc, char *argv[]) { cxxopts::Options options("MyProgram", "One line description of MyProgram"); @@ -37,7 +38,16 @@ int main(int argc, char *argv[]) { str += s + " "; } auto src_str = trim(str); - cout << "sha256('" << str << "'): " << sha256_hash(str) << endl; + // tasks[sha256_hash(str)] = str; + std::string hash = sha256_hash(str); + std::cout << "sha256('" << str << "'): " << hash << std::endl; + + // for (const auto &n : tasks) { + // std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; + // } + + std::string p = prefix(hash, tasks); + std::cout << "prefix: " << p << std::endl; } std::string taskdir; if (result.count("taskdir")) { -- cgit v1.2.3