diff --git a/HISTORY b/HISTORY index 53518a4..9922e3d 100644 --- a/HISTORY +++ b/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: diff --git a/libfastcommon.spec b/libfastcommon.spec index 3648b08..482e3fa 100644 --- a/libfastcommon.spec +++ b/libfastcommon.spec @@ -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} diff --git a/src/shared_func.c b/src/shared_func.c index dd7b302..893a870 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -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; +} diff --git a/src/shared_func.h b/src/shared_func.h index 1f8242e..282e96f 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -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 diff --git a/src/tests/Makefile b/src/tests/Makefile index fc71f50..1dde4cd 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -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: diff --git a/src/tests/test_thourands_seperator.c b/src/tests/test_thourands_seperator.c new file mode 100644 index 0000000..18322ef --- /dev/null +++ b/src/tests/test_thourands_seperator.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include "fastcommon/shared_func.h" + +int main(int argc, char *argv[]) +{ + int64_t n; + char buff[32]; + + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return EINVAL; + } + + n = strtol(argv[1], NULL, 10); + printf("%s\n", long_to_comma_str(n, buff)); + return 0; +}