feat(baseN): digitsMap

This commit is contained in:
2024-09-29 13:33:38 +03:00
parent 3c93056416
commit fb15184848
4 changed files with 28 additions and 0 deletions

View File

@ -83,6 +83,7 @@ docs:
cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}}
rm -f **/*.gcda
${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}}
mkdir cov
gcovr --html-nested cov/index.html --txt --exclude-throw-branches
${OBJDIR}/%${-g}-cov.o: ${SRCDIR}/%.cpp ${INCDIR}/${LIB}/%.hpp

View File

@ -7,6 +7,13 @@
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_size

View File

@ -9,6 +9,18 @@ static constexpr auto log256 = std::log(256);
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
{
return std::all_of(str, str + str_size, [map](char ch)

View File

@ -7,6 +7,14 @@
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)
{
std::vector<std::pair<bool, std::string>> tests = {