Make func "kernaux_itoa" public

This commit is contained in:
Alex Kotov 2022-01-24 21:25:01 +05:00
parent 5c59abb6c5
commit 05719e8b40
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 168 additions and 3 deletions

View File

@ -1,3 +1,7 @@
2022-01-24 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/ntoa.h: Add func "kernaux_itoa"
2022-01-23 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/assert.h: Require semicolon after macros

View File

@ -10,6 +10,9 @@ extern "C" {
// TODO: define
#define KERNAUX_UTOA_BUFFER_SIZE (0/0)
// TODO: define
#define KERNAUX_ITOA_BUFFER_SIZE (0/0)
// "18446744073709551615"
#define KERNAUX_UTOA10_BUFFER_SIZE 21
@ -26,6 +29,7 @@ extern "C" {
// TODO: add tests for assertion
char *kernaux_utoa(uint64_t value, char *buffer, int base);
char *kernaux_itoa(int64_t value, char *buffer, int base);
void kernaux_utoa10(uint64_t value, char *buffer);
void kernaux_itoa10(int64_t value, char *buffer);

View File

@ -47,8 +47,7 @@ char *kernaux_utoa(uint64_t value, char *buffer, int base)
return result;
}
// TODO: example common func, maybe it may become public
static char *kernaux_itoa(int64_t value, char *buffer, int base)
char *kernaux_itoa(int64_t value, char *buffer, int base)
{
if (value >= 0) {
return kernaux_utoa(value, buffer, base);

View File

@ -126,6 +126,124 @@ static const struct {
{ "9ix", 36, 12345 }, { "9IX", -36, 12345 },
};
static const struct {
const char *result;
int base;
int64_t value;
} itoa_cases[] = {
{ "0", 2, 0 }, { "0", -2, 0 }, { "0", 'b', 0 }, { "0", 'B', 0 },
{ "0", 3, 0 }, { "0", -3, 0 },
{ "0", 4, 0 }, { "0", -4, 0 },
{ "0", 5, 0 }, { "0", -5, 0 },
{ "0", 6, 0 }, { "0", -6, 0 },
{ "0", 7, 0 }, { "0", -7, 0 },
{ "0", 8, 0 }, { "0", -8, 0 }, { "0", 'o', 0 }, { "0", 'O', 0 },
{ "0", 9, 0 }, { "0", -9, 0 },
{ "0", 10, 0 }, { "0", -10, 0 }, { "0", 'd', 0 }, { "0", 'D', 0 },
{ "0", 11, 0 }, { "0", -11, 0 },
{ "0", 12, 0 }, { "0", -12, 0 },
{ "0", 13, 0 }, { "0", -13, 0 },
{ "0", 14, 0 }, { "0", -14, 0 },
{ "0", 15, 0 }, { "0", -15, 0 },
{ "0", 16, 0 }, { "0", -16, 0 },
{ "0", 'h', 0 }, { "0", 'H', 0 }, { "0", 'x', 0 }, { "0", 'X', 0 },
{ "0", 17, 0 }, { "0", -17, 0 },
{ "0", 18, 0 }, { "0", -18, 0 },
{ "0", 19, 0 }, { "0", -19, 0 },
{ "0", 20, 0 }, { "0", -20, 0 },
{ "0", 21, 0 }, { "0", -21, 0 },
{ "0", 22, 0 }, { "0", -22, 0 },
{ "0", 23, 0 }, { "0", -23, 0 },
{ "0", 24, 0 }, { "0", -24, 0 },
{ "0", 25, 0 }, { "0", -25, 0 },
{ "0", 26, 0 }, { "0", -26, 0 },
{ "0", 27, 0 }, { "0", -27, 0 },
{ "0", 28, 0 }, { "0", -28, 0 },
{ "0", 29, 0 }, { "0", -29, 0 },
{ "0", 30, 0 }, { "0", -30, 0 },
{ "0", 31, 0 }, { "0", -31, 0 },
{ "0", 32, 0 }, { "0", -32, 0 },
{ "0", 33, 0 }, { "0", -33, 0 },
{ "0", 34, 0 }, { "0", -34, 0 },
{ "0", 35, 0 }, { "0", -35, 0 },
{ "0", 36, 0 }, { "0", -36, 0 },
{ "1111011", 2, 123 }, { "1111011", -2, 123 },
{ "1111011", 'b', 123 }, { "1111011", 'B', 123 },
{ "11120", 3, 123 }, { "11120", -3, 123 },
{ "1323", 4, 123 }, { "1323", -4, 123 },
{ "443", 5, 123 }, { "443", -5, 123 },
{ "323", 6, 123 }, { "323", -6, 123 },
{ "234", 7, 123 }, { "234", -7, 123 },
{ "173", 8, 123 }, { "173", -8, 123 },
{ "173", 'o', 123 }, { "173", 'O', 123 },
{ "146", 9, 123 }, { "146", -9, 123 },
{ "123", 10, 123 }, { "123", -10, 123 },
{ "123", 'd', 123 }, { "123", 'D', 123 },
{ "102", 11, 123 }, { "102", -11, 123 },
{ "a3", 12, 123 }, { "A3", -12, 123 },
{ "96", 13, 123 }, { "96", -13, 123 },
{ "8b", 14, 123 }, { "8B", -14, 123 },
{ "83", 15, 123 }, { "83", -15, 123 },
{ "7b", 16, 123 }, { "7B", -16, 123 },
{ "7b", 'h', 123 }, { "7B", 'H', 123 },
{ "7b", 'x', 123 }, { "7B", 'X', 123 },
{ "74", 17, 123 }, { "74", -17, 123 },
{ "6f", 18, 123 }, { "6F", -18, 123 },
{ "69", 19, 123 }, { "69", -19, 123 },
{ "63", 20, 123 }, { "63", -20, 123 },
{ "5i", 21, 123 }, { "5I", -21, 123 },
{ "5d", 22, 123 }, { "5D", -22, 123 },
{ "58", 23, 123 }, { "58", -23, 123 },
{ "53", 24, 123 }, { "53", -24, 123 },
{ "4n", 25, 123 }, { "4N", -25, 123 },
{ "4j", 26, 123 }, { "4J", -26, 123 },
{ "4f", 27, 123 }, { "4F", -27, 123 },
{ "4b", 28, 123 }, { "4B", -28, 123 },
{ "47", 29, 123 }, { "47", -29, 123 },
{ "43", 30, 123 }, { "43", -30, 123 },
{ "3u", 31, 123 }, { "3U", -31, 123 },
{ "3r", 32, 123 }, { "3R", -32, 123 },
{ "3o", 33, 123 }, { "3O", -33, 123 },
{ "3l", 34, 123 }, { "3L", -34, 123 },
{ "3i", 35, 123 }, { "3I", -35, 123 },
{ "3f", 36, 123 }, { "3F", -36, 123 },
{ "11000000111001", 2, 12345 }, { "11000000111001", -2, 12345 },
{ "121221020", 3, 12345 }, { "121221020", -3, 12345 },
{ "3000321", 4, 12345 }, { "3000321", -4, 12345 },
{ "343340", 5, 12345 }, { "343340", -5, 12345 },
{ "133053", 6, 12345 }, { "133053", -6, 12345 },
{ "50664", 7, 12345 }, { "50664", -7, 12345 },
{ "30071", 8, 12345 }, { "30071", -8, 12345 },
{ "17836", 9, 12345 }, { "17836", -9, 12345 },
{ "12345", 10, 12345 }, { "12345", -10, 12345 },
{ "9303", 11, 12345 }, { "9303", -11, 12345 },
{ "7189", 12, 12345 }, { "7189", -12, 12345 },
{ "5808", 13, 12345 }, { "5808", -13, 12345 },
{ "46db", 14, 12345 }, { "46DB", -14, 12345 },
{ "39d0", 15, 12345 }, { "39D0", -15, 12345 },
{ "3039", 16, 12345 }, { "3039", -16, 12345 },
{ "28c3", 17, 12345 }, { "28C3", -17, 12345 },
{ "221f", 18, 12345 }, { "221F", -18, 12345 },
{ "1f3e", 19, 12345 }, { "1F3E", -19, 12345 },
{ "1ah5", 20, 12345 }, { "1AH5", -20, 12345 },
{ "16ki", 21, 12345 }, { "16KI", -21, 12345 },
{ "13b3", 22, 12345 }, { "13B3", -22, 12345 },
{ "107h", 23, 12345 }, { "107H", -23, 12345 },
{ "la9", 24, 12345 }, { "LA9", -24, 12345 },
{ "jik", 25, 12345 }, { "JIK", -25, 12345 },
{ "i6l", 26, 12345 }, { "I6L", -26, 12345 },
{ "gp6", 27, 12345 }, { "GP6", -27, 12345 },
{ "fkp", 28, 12345 }, { "FKP", -28, 12345 },
{ "ejk", 29, 12345 }, { "EJK", -29, 12345 },
{ "dlf", 30, 12345 }, { "DLF", -30, 12345 },
{ "cq7", 31, 12345 }, { "CQ7", -31, 12345 },
{ "c1p", 32, 12345 }, { "C1P", -32, 12345 },
{ "bb3", 33, 12345 }, { "BB3", -33, 12345 },
{ "an3", 34, 12345 }, { "AN3", -34, 12345 },
{ "a2p", 35, 12345 }, { "A2P", -35, 12345 },
{ "9ix", 36, 12345 }, { "9IX", -36, 12345 },
};
static const struct {
uint64_t value;
const char *result;
@ -365,6 +483,7 @@ static const struct {
int main()
{
{
// TODO: replace 1000 with KERNAUX_UTOA_BUFFER_SIZE
char buffer[1000];
for (
@ -372,7 +491,11 @@ int main()
index < sizeof(utoa_cases) / sizeof(utoa_cases[0]);
++index
) {
const char *const end = kernaux_utoa(utoa_cases[index].value, buffer, utoa_cases[index].base);
const char *const end = kernaux_utoa(
utoa_cases[index].value,
buffer,
utoa_cases[index].base
);
assert(strcmp(buffer, utoa_cases[index].result) == 0);
for (const char *pos = buffer;; ++pos) {
if (*pos == '\0') {
@ -383,6 +506,41 @@ int main()
}
}
{
// TODO: replace 1000 with KERNAUX_ITOA_BUFFER_SIZE
char buffer[1000];
for (
size_t index = 0;
index < sizeof(itoa_cases) / sizeof(itoa_cases[0]);
++index
) {
const int64_t value = itoa_cases[index].value;
const int base = itoa_cases[index].base;
const char *const end1 = kernaux_itoa(value, buffer, base);
assert(strcmp(buffer, itoa_cases[index].result) == 0);
for (const char *pos = buffer;; ++pos) {
if (*pos == '\0') {
assert(end1 == pos);
break;
}
}
if (value <= 0 || base < 2 || base > 36) continue;
const char *const end2 = kernaux_itoa(-value, buffer, base);
assert(buffer[0] == '-');
assert(strcmp(&buffer[1], itoa_cases[index].result) == 0);
for (const char *pos = buffer;; ++pos) {
if (*pos == '\0') {
assert(end2 == pos);
break;
}
}
}
}
{
char buffer[KERNAUX_UTOA10_BUFFER_SIZE];