47 lines
1.0 KiB
C++
47 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <utility>
|
|
#include <memory>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
#include "lib/utils/bts_assert.h"
|
|
|
|
namespace bts {
|
|
namespace utils {
|
|
|
|
static char base_62_alphabet [] =
|
|
{
|
|
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
|
|
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
|
|
'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
|
|
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
|
};
|
|
|
|
inline std::string to_base_62 (uint32_t src)
|
|
{
|
|
std::string out (6, ' ');
|
|
for (int i=5; i >= 0; i--)
|
|
{
|
|
int r = src % 62;
|
|
out[i] = base_62_alphabet[r];
|
|
src = src / 62;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
inline uint32_t from_base_62 (const std::string& src)
|
|
{
|
|
bts_assert_msg(src.size() == 6, "bad base62 hash");
|
|
uint32_t out = 0;
|
|
for (int i=5; i >= 0; i--)
|
|
{
|
|
uint32_t idx = std::distance(&base_62_alphabet[0], std::find(&base_62_alphabet[0], &base_62_alphabet[61], src[i]));
|
|
out += idx * pow(62, 5-i);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
|
|
}}
|