feat(baseN): digitsMap
This commit is contained in:
1
Makefile
1
Makefile
@ -83,6 +83,7 @@ docs:
|
|||||||
cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}}
|
cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}}
|
||||||
rm -f **/*.gcda
|
rm -f **/*.gcda
|
||||||
${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}}
|
${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}}
|
||||||
|
mkdir cov
|
||||||
gcovr --html-nested cov/index.html --txt --exclude-throw-branches
|
gcovr --html-nested cov/index.html --txt --exclude-throw-branches
|
||||||
|
|
||||||
${OBJDIR}/%${-g}-cov.o: ${SRCDIR}/%.cpp ${INCDIR}/${LIB}/%.hpp
|
${OBJDIR}/%${-g}-cov.o: ${SRCDIR}/%.cpp ${INCDIR}/${LIB}/%.hpp
|
||||||
|
|||||||
@ -7,6 +7,13 @@
|
|||||||
|
|
||||||
namespace baseN
|
namespace baseN
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param digits char[base] array of digits
|
||||||
|
* @param digits_size size of digits array. Equals to base
|
||||||
|
* @param map [out] int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol
|
||||||
|
* @throw std::logic_error if alphabet contain same chars
|
||||||
|
*/
|
||||||
|
void digitsMap(const char *digits, uint8_t digits_size, int8_t *map);
|
||||||
/**
|
/**
|
||||||
* @param str [in] pointer to string
|
* @param str [in] pointer to string
|
||||||
* @param str_size
|
* @param str_size
|
||||||
|
|||||||
@ -9,6 +9,18 @@ static constexpr auto log256 = std::log(256);
|
|||||||
|
|
||||||
namespace baseN
|
namespace baseN
|
||||||
{
|
{
|
||||||
|
void digitsMap(const char *digits, uint8_t digits_size, int8_t *map)
|
||||||
|
{
|
||||||
|
std::fill(map, map + 256, -1);
|
||||||
|
for (uint8_t i = 0; i < digits_size; i++)
|
||||||
|
{
|
||||||
|
if (map[(int8_t)digits[i]] != -1)
|
||||||
|
{
|
||||||
|
throw std::logic_error("baseN::digitsMap: alphabet contain same chars");
|
||||||
|
}
|
||||||
|
map[(int8_t)digits[i]] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept
|
bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept
|
||||||
{
|
{
|
||||||
return std::all_of(str, str + str_size, [map](char ch)
|
return std::all_of(str, str + str_size, [map](char ch)
|
||||||
|
|||||||
@ -7,6 +7,14 @@
|
|||||||
|
|
||||||
using namespace baseN;
|
using namespace baseN;
|
||||||
|
|
||||||
|
TEST(baseN, digitsMap)
|
||||||
|
{
|
||||||
|
int8_t map[256];
|
||||||
|
digitsMap(base58::digits, 58, map);
|
||||||
|
EXPECT_TRUE(std::equal(map, map + 256, base58::map));
|
||||||
|
|
||||||
|
EXPECT_THROW(digitsMap("11", 2, map), std::logic_error);
|
||||||
|
}
|
||||||
TEST(baseN, isValid)
|
TEST(baseN, isValid)
|
||||||
{
|
{
|
||||||
std::vector<std::pair<bool, std::string>> tests = {
|
std::vector<std::pair<bool, std::string>> tests = {
|
||||||
|
|||||||
Reference in New Issue
Block a user