shared_func.h: add functions int2str and long2str
parent
530c17aaa1
commit
571be969dd
3
HISTORY
3
HISTORY
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
Version 1.38 2018-06-07
|
||||
Version 1.38 2018-06-11
|
||||
* connection_pool.c: set err_no to 0 when success
|
||||
* shared_func.h: add functions float2buff / buff2float, double2buff / buff2double
|
||||
* logger.h: add function log_get_level_caption
|
||||
|
|
@ -8,6 +8,7 @@ Version 1.38 2018-06-07
|
|||
* ioevent.[hc]: remove care_events in FreeBSD or MacOS
|
||||
* add skiplist_set.[hc] and skiplist bug fixed
|
||||
* correct CRC32
|
||||
* shared_func.h: add functions int2str and long2str
|
||||
|
||||
Version 1.37 2018-02-24
|
||||
* ini_file_reader.c function annotations LOCAL_IP_GET support index, such as:
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ Summary: Development header file
|
|||
Requires: %{name}%{?_isa} = %{version}-%{release}
|
||||
|
||||
%description devel
|
||||
This pakcage provides the header files of libfastcommon
|
||||
This package provides the header files of libfastcommon
|
||||
commit version: %{CommitVersion}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2560,3 +2560,76 @@ key_t fc_ftok(const char *path, const int proj_id)
|
|||
hash_code = simple_hash(path, strlen(path));
|
||||
return (((proj_id & 0xFF) << 24) | (hash_code & 0xFFFFFF));
|
||||
}
|
||||
|
||||
static void add_thousands_separator(char *str, const int len)
|
||||
{
|
||||
int new_len;
|
||||
int addings;
|
||||
int sub;
|
||||
int chars;
|
||||
int add_count;
|
||||
char *src;
|
||||
char *dest;
|
||||
char *first;
|
||||
|
||||
if (len <= 3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (*str == '-')
|
||||
{
|
||||
first = str + 1;
|
||||
sub = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
first = str;
|
||||
sub = 1;
|
||||
}
|
||||
|
||||
addings = (len - sub) / 3;
|
||||
new_len = len + addings;
|
||||
|
||||
src = str + (len - 1);
|
||||
dest = str + new_len;
|
||||
*dest-- = '\0';
|
||||
chars = 0;
|
||||
add_count = 0;
|
||||
while (src >= first)
|
||||
{
|
||||
*dest-- = *src--;
|
||||
if (++chars % 3 == 0)
|
||||
{
|
||||
if (add_count == addings)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
*dest-- = ',';
|
||||
add_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
||||
{
|
||||
int len;
|
||||
len = sprintf(buff, "%d", n);
|
||||
if (thousands_separator)
|
||||
{
|
||||
add_thousands_separator(buff, len);
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
|
||||
{
|
||||
int len;
|
||||
len = sprintf(buff, "%"PRId64, n);
|
||||
if (thousands_separator)
|
||||
{
|
||||
add_thousands_separator(buff, len);
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -728,6 +728,34 @@ ssize_t fc_safe_read(int fd, char *buf, const size_t count);
|
|||
*/
|
||||
key_t fc_ftok(const char *path, const int proj_id);
|
||||
|
||||
/** convert int to string
|
||||
* parameters:
|
||||
* n: the 32 bits integer
|
||||
* buff: output buffer
|
||||
* thousands_separator: if add thousands separator
|
||||
* return: string buffer
|
||||
*/
|
||||
const char *int2str(const int n, char *buff, const bool thousands_separator);
|
||||
|
||||
static inline const char *int_to_comma_str(const int n, char *buff)
|
||||
{
|
||||
return int2str(n, buff, true);
|
||||
}
|
||||
|
||||
/** convert long to string
|
||||
* parameters:
|
||||
* n: the 64 bits integer
|
||||
* buff: output buffer
|
||||
* thousands_separator: if add thousands separator
|
||||
* return: string buffer
|
||||
*/
|
||||
const char *long2str(const int64_t n, char *buff, const bool thousands_separator);
|
||||
|
||||
static inline const char *long_to_comma_str(const int64_t n, char *buff)
|
||||
{
|
||||
return long2str(n, buff, true);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ LIB_PATH = -lfastcommon -lpthread
|
|||
|
||||
ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \
|
||||
test_id_generator test_ini_parser test_char_convert test_char_convert_loader \
|
||||
test_logger test_skiplist_set test_crc32
|
||||
test_logger test_skiplist_set test_crc32 test_thourands_seperator
|
||||
|
||||
all: $(ALL_PRGS)
|
||||
.c:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <inttypes.h>
|
||||
#include <sys/time.h>
|
||||
#include "fastcommon/shared_func.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int64_t n;
|
||||
char buff[32];
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: %s <integer>\n", argv[0]);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
n = strtol(argv[1], NULL, 10);
|
||||
printf("%s\n", long_to_comma_str(n, buff));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue