add function fc_itoa

remotes/origin/fstore_storage_engine
YuQing 2022-09-28 22:13:57 +08:00
parent cf66174cf9
commit 6ea757f492
4 changed files with 131 additions and 1 deletions

View File

@ -1,6 +1,7 @@
Version 1.62 2022-09-27 Version 1.62 2022-09-28
* add function fc_sleep_us * add function fc_sleep_us
* add function fc_itoa
Version 1.61 2022-09-21 Version 1.61 2022-09-21
* get_base_path_from_conf_file_ex support parameter: noent_log_level * get_base_path_from_conf_file_ex support parameter: noent_log_level

View File

@ -159,6 +159,25 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
#define FC_TIME_UNIT_USECOND 'u' //microsecond #define FC_TIME_UNIT_USECOND 'u' //microsecond
#define FC_TIME_UNIT_NSECOND 'n' //nanosecond #define FC_TIME_UNIT_NSECOND 'n' //nanosecond
#define FC_1E01 10LL
#define FC_1E02 100LL
#define FC_1E03 1000LL
#define FC_1E04 10000LL
#define FC_1E05 100000LL
#define FC_1E06 1000000LL
#define FC_1E07 10000000LL
#define FC_1E08 100000000LL
#define FC_1E09 1000000000LL
#define FC_1E10 10000000000LL
#define FC_1E11 100000000000LL
#define FC_1E12 1000000000000LL
#define FC_1E13 10000000000000LL
#define FC_1E14 100000000000000LL
#define FC_1E15 1000000000000000LL
#define FC_1E16 10000000000000000LL
#define FC_1E17 100000000000000000LL
#define FC_1E18 1000000000000000000LL
#define STRERROR(no) (strerror(no) != NULL ? strerror(no) : "Unkown error") #define STRERROR(no) (strerror(no) != NULL ? strerror(no) : "Unkown error")
#if defined(OS_LINUX) #if defined(OS_LINUX)

View File

@ -4060,3 +4060,106 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
fi->fd = -1; fi->fd = -1;
return 0; return 0;
} }
int fc_itoa(int64_t n, char *buff)
{
char *p;
int start;
int len;
int i;
if (n < 0) {
n *= -1;
*buff = '-';
start = 1;
} else {
start = 0;
}
if (n < FC_1E16) {
if (n < FC_1E08) {
if (n < FC_1E04) {
p = buff + start;
if (n < FC_1E02) {
if (n < FC_1E01) {
*p++ = '0' + n;
} else {
*p++ = '0' + n / 10;
*p++ = '0' + n % 10;
}
} else {
if (n < FC_1E03) {
*p++ = '0' + n / 100;
} else {
*p++ = '0' + n / 1000;
*p++ = '0' + (n % 1000) / 100;
}
*p++ = '0' + (n % 100) / 10;
*p++ = '0' + n % 10;
}
return p - buff;
} else {
if (n < FC_1E06) {
if (n < FC_1E05) {
len = 5;
} else {
len = 6;
}
} else {
if (n < FC_1E07) {
len = 7;
} else {
len = 8;
}
}
}
} else {
if (n < FC_1E12) {
if (n < FC_1E10) {
if (n < FC_1E09) {
len = 9;
} else {
len = 10;
}
} else {
if (n < FC_1E11) {
len = 11;
} else {
len = 12;
}
}
} else {
if (n < FC_1E14) {
if (n < FC_1E13) {
len = 13;
} else {
len = 14;
}
} else {
if (n < FC_1E15) {
len = 15;
} else {
len = 16;
}
}
}
}
} else {
if (n < FC_1E18) {
if (n < FC_1E17) {
len = 17;
} else {
len = 18;
}
} else {
len = 19;
}
}
p = buff + start + len - 1;
for (i=0; i<len; i++) {
*p-- = '0' + n % 10;
n /= 10;
}
return start + len;
}

View File

@ -1175,6 +1175,13 @@ int fc_get_last_lines(const char *filename, char *buff,
bool fc_path_contains(const string_t *path, const string_t *needle, bool fc_path_contains(const string_t *path, const string_t *needle,
int *result); int *result);
/** itoa output as decimal number
* parameters:
* n: the number to convert
* buff: store the converted string
* return: string length
*/
int fc_itoa(int64_t n, char *buff);
/** sleep in microseconds /** sleep in microseconds
* parameters: * parameters: