From 3fb4df637ecfc76227cbdd62eb4d6e2763f9dd5e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 5 Apr 2017 20:04:21 +0100 Subject: [PATCH] A basic implementation of make_token(). Simply converts the given number into base 26. --- src/CMakeLists.txt | 1 + src/num_to_token.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/num_to_token.hpp | 9 +++++++++ 3 files changed, 57 insertions(+) create mode 100644 src/num_to_token.cpp create mode 100644 src/num_to_token.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5090117..b58b646 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${PROJECT_NAME} get_env.cpp envy.cpp cgi_env.cpp + num_to_token.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM diff --git a/src/num_to_token.cpp b/src/num_to_token.cpp new file mode 100644 index 0000000..0fe2dd5 --- /dev/null +++ b/src/num_to_token.cpp @@ -0,0 +1,47 @@ +#include "num_to_token.hpp" +#include + +namespace tawashi { + namespace { + //const int g_any_min = 0; + //const int g_any_max = g_any_min + 'z' - 'a' - 1; + //const int g_vowel_min = g_any_max + 1; + //const int g_vowel_max = g_vowel_min + 5 - 1; + //const int g_consonant_min = g_vowel_max + 1; + //const int g_consonant_max = g_consonant_min + ('z' - 'a') - (g_vowel_max - g_vowel_min) - 1; + + //char code_to_char (int parCode) { + // const char vowels[] = {'a', 'i', 'u', 'e', 'o'}; + // const char consonants[] = { + // 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', + // 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' + // }; + + // static_assert(sizeof(vowels) == g_vowel_max - g_vowel_min + 1, "Wrong vowels count"); + // static_assert(sizeof(consonants) == g_consonant_max - g_consonant_min + 1, "Wrong consonants count"); + + // if (parCode <= g_any_max) + // return static_cast('a' + parCode - g_any_min); + // else if (parCode <= g_vowel_max) + // return vowels[parCode - g_vowel_min]; + // else if (parCode <= g_consonant_max) + // return consonants[parCode - g_consonant_min]; + + // assert(false); + // return 'X'; + //} + } //unnamed namespace + + std::string make_token (uint64_t parNum) { + assert(0 != parNum); + std::string retval; + + do { + const auto remainder = parNum % ('z' - 'a' + 1); + retval.push_back(static_cast(remainder)); + parNum /= ('z' - 'a' + 1); + } while (parNum); + + return retval; + } +} //namespace tawashi diff --git a/src/num_to_token.hpp b/src/num_to_token.hpp new file mode 100644 index 0000000..aaa6d8f --- /dev/null +++ b/src/num_to_token.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "duckhandy/compatibility.h" +#include +#include + +namespace tawashi { + std::string make_token (uint64_t parNum) a_pure; +} //namespace tawashi