mirror of
https://github.com/tailix/libkernaux.git
synced 2025-02-24 15:55:41 -05:00
Add func kernaux_utoa10
This commit is contained in:
parent
e8261034df
commit
22f1625002
3 changed files with 79 additions and 0 deletions
|
@ -5,8 +5,16 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// uint64_t: "18446744073709551615"
|
||||
#define KERNAUX_ITOA_BUFFER_SIZE 21
|
||||
|
||||
// TODO: remove this
|
||||
void kernaux_itoa(int d, char *buf, int base);
|
||||
|
||||
void kernaux_utoa10(uint64_t value, char *buffer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
21
src/itoa.c
21
src/itoa.c
|
@ -3,6 +3,7 @@
|
|||
#endif
|
||||
|
||||
#include <kernaux/itoa.h>
|
||||
#include <kernaux/libc.h>
|
||||
|
||||
void kernaux_itoa(const int d, char *buf, const int base)
|
||||
{
|
||||
|
@ -43,3 +44,23 @@ void kernaux_itoa(const int d, char *buf, const int base)
|
|||
p2--;
|
||||
}
|
||||
}
|
||||
|
||||
void kernaux_utoa10(uint64_t value, char *buffer)
|
||||
{
|
||||
char *pos = buffer;
|
||||
|
||||
if (value == 0) *(pos++) = '0';
|
||||
|
||||
while (value > 0) {
|
||||
*(pos++) = value % 10 + '0';
|
||||
value = value / 10;
|
||||
}
|
||||
|
||||
*(pos--) = '\0';
|
||||
|
||||
while (buffer < pos) {
|
||||
const char tmp = *buffer;
|
||||
*(buffer++) = *pos;
|
||||
*(pos--) = tmp;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,45 @@
|
|||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
static const struct {
|
||||
uint64_t value;
|
||||
const char *result;
|
||||
} utoa10_cases[] = {
|
||||
{ 0, "0" },
|
||||
{ 1, "1" },
|
||||
{ 2, "2" },
|
||||
{ 9, "9" },
|
||||
{ 10, "10" },
|
||||
{ 11, "11" },
|
||||
{ 12, "12" },
|
||||
{ 19, "19" },
|
||||
{ 20, "20" },
|
||||
{ 21, "21" },
|
||||
{ 99, "99" },
|
||||
{ 100, "100" },
|
||||
{ 101, "101" },
|
||||
{ 199, "199" },
|
||||
{ 200, "200" },
|
||||
{ 201, "201" },
|
||||
{ 999, "999" },
|
||||
{ 1000, "1000" },
|
||||
{ 1001, "1001" },
|
||||
{ 1999, "1999" },
|
||||
{ 2000, "2000" },
|
||||
{ 2001, "2001" },
|
||||
{ 9999, "9999" },
|
||||
{ 10000, "10000" },
|
||||
{ 10001, "10001" },
|
||||
{ UINT16_MAX, "65535" },
|
||||
{ UINT16_MAX + 1, "65536" },
|
||||
{ UINT32_MAX, "4294967295" },
|
||||
{ (uint64_t)UINT32_MAX + 1, "4294967296" },
|
||||
{ UINT64_MAX - 6, "18446744073709551609" },
|
||||
{ UINT64_MAX - 5, "18446744073709551610" },
|
||||
{ UINT64_MAX - 1, "18446744073709551614" },
|
||||
{ UINT64_MAX, "18446744073709551615" },
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
|
@ -33,5 +72,16 @@ int main()
|
|||
assert(buffer[5] == '\0');
|
||||
}
|
||||
|
||||
char buffer[KERNAUX_ITOA_BUFFER_SIZE];
|
||||
|
||||
for (
|
||||
size_t index = 0;
|
||||
index < sizeof(utoa10_cases) / sizeof(utoa10_cases[0]);
|
||||
++index
|
||||
) {
|
||||
kernaux_utoa10(utoa10_cases[index].value, buffer);
|
||||
assert(strcmp(buffer, utoa10_cases[index].result) == 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue