add functions short2hex, int2hex, long2hex etc.

use_iouring
YuQing 2025-08-06 14:16:06 +08:00
parent 1d2f938a30
commit 63ef9aa8f4
5 changed files with 118 additions and 9 deletions

View File

@ -1,8 +1,9 @@
Version 1.78 2025-08-03 Version 1.78 2025-08-06
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6 * getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
* add files: spinlock.[hc] * add files: spinlock.[hc]
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline * shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
* shared_func.[hc]: add functions short2hex, int2hex, long2hex etc.
Version 1.77 2025-03-18 Version 1.77 2025-03-18
* impl. shorten_path for /./ and /../ * impl. shorten_path for /./ and /../

View File

@ -46,6 +46,7 @@
#endif #endif
bool g_set_cloexec = false; bool g_set_cloexec = false;
const char *g_lower_hex_chars = "0123456789abcdef";
char *formatDatetime(const time_t nTime, \ char *formatDatetime(const time_t nTime, \
const char *szDateFormat, \ const char *szDateFormat, \
@ -528,7 +529,6 @@ void daemon_init(bool bCloseFiles)
char *bin2hex(const char *s, const int len, char *szHexBuff) char *bin2hex(const char *s, const int len, char *szHexBuff)
{ {
const char *hex_chars = "0123456789abcdef";
unsigned char *p; unsigned char *p;
unsigned char *pEnd; unsigned char *pEnd;
char *dest; char *dest;
@ -537,8 +537,8 @@ char *bin2hex(const char *s, const int len, char *szHexBuff)
pEnd = (unsigned char *)s + len; pEnd = (unsigned char *)s + len;
for (p=(unsigned char *)s; p<pEnd; p++) for (p=(unsigned char *)s; p<pEnd; p++)
{ {
*dest++ = hex_chars[(*p >> 4) & 0x0F]; *dest++ = g_lower_hex_chars[*p >> 4];
*dest++ = hex_chars[*p & 0x0F]; *dest++ = g_lower_hex_chars[*p & 0x0F];
} }
*dest = '\0'; *dest = '\0';
@ -2568,7 +2568,7 @@ int get_time_item_from_str(const char *pValue, const char *item_name,
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len) char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
{ {
static unsigned char hex_chars[] = "0123456789ABCDEF"; const unsigned char upper_hex_chars[] = "0123456789ABCDEF";
const unsigned char *pSrc; const unsigned char *pSrc;
const unsigned char *pEnd; const unsigned char *pEnd;
char *pDest; char *pDest;
@ -2591,8 +2591,8 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
else else
{ {
*pDest++ = '%'; *pDest++ = '%';
*pDest++ = hex_chars[(*pSrc) >> 4]; *pDest++ = upper_hex_chars[(*pSrc) >> 4];
*pDest++ = hex_chars[(*pSrc) & 0x0F]; *pDest++ = upper_hex_chars[(*pSrc) & 0x0F];
} }
} }

View File

@ -46,6 +46,7 @@ extern "C" {
#endif #endif
extern bool g_set_cloexec; extern bool g_set_cloexec;
extern const char *g_lower_hex_chars;
static inline void fc_enable_fd_cloexec(const bool cloexec) static inline void fc_enable_fd_cloexec(const bool cloexec)
{ {
@ -339,6 +340,97 @@ static inline double buff2double(const char *buff)
return *p; return *p;
} }
static inline void padding_hex(char *buff, const int len, const int padding_len)
{
char *p;
char *end;
char *stop;
int new_len;
int fill_len;
if (padding_len == len) {
return;
} else if (padding_len > len) {
fill_len = padding_len - len;
memmove(buff + fill_len, buff, len + 1);
memset(buff, '0', fill_len);
return;
} else if (*buff != '0') {
return;
}
end = buff + len;
if (padding_len <= 0) {
stop = end - 1;
} else {
stop = end - padding_len;
}
p = buff + 1;
while (p < stop && *p == '0') {
++p;
}
new_len = end - p;
memmove(buff, p, new_len + 1);
}
static inline void short2hex(const short n, char *buff, const int padding_len)
{
unsigned char *p;
p = (unsigned char *)buff;
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
*p++ = g_lower_hex_chars[n & 0x0F];
*p = '\0';
padding_hex(buff, 4, padding_len);
}
static inline void int2hex(const int n, char *buff, const int padding_len)
{
unsigned char *p;
p = (unsigned char *)buff;
*p++ = g_lower_hex_chars[(n >> 28) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 24) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 20) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 16) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
*p++ = g_lower_hex_chars[n & 0x0F];
*p = '\0';
padding_hex(buff, 8, padding_len);
}
static inline void long2hex(const int64_t n,
char *buff, const int padding_len)
{
unsigned char *p;
p = (unsigned char *)buff;
*p++ = g_lower_hex_chars[(n >> 60) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 56) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 52) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 48) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 44) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 40) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 36) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 32) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 28) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 24) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 20) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 16) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
*p++ = g_lower_hex_chars[n & 0x0F];
*p = '\0';
padding_hex(buff, 16, padding_len);
}
/** trim leading spaces ( \t\r\n) /** trim leading spaces ( \t\r\n)
* parameters: * parameters:
* pStr: the string to trim * pStr: the string to trim
@ -1273,6 +1365,22 @@ bool fc_path_contains(const string_t *path, const string_t *needle,
*/ */
int fc_itoa(int64_t n, char *buff); int fc_itoa(int64_t n, char *buff);
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
{
int len;
len = strlen(src);
if (len < size) {
memcpy(dest, src, len + 1);
} else {
len = size - 1;
memcpy(dest, src, len);
*(dest + len) = '\0';
}
return len;
}
/** sleep in microseconds /** sleep in microseconds
* parameters: * parameters:
* microseconds: microseconds to sleep * microseconds: microseconds to sleep

View File

@ -11,7 +11,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo
test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \ test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \
test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \ test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \
test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \ test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \
test_thread_local test_memcpy mblock_benchmark cpool_benchmark test_thread_local test_memcpy test_fast_buffer mblock_benchmark cpool_benchmark
all: $(ALL_PRGS) all: $(ALL_PRGS)

View File

@ -112,7 +112,7 @@ int main(int argc, char *argv[])
setup_mblock_stat_task(); setup_mblock_stat_task();
if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) { if ((result=fast_buffer_init1(&buffer, 1024)) != 0) {
return result; return result;
} }
fc_server_to_config_string(&ctx, &buffer); fc_server_to_config_string(&ctx, &buffer);