diff --git a/src/common_define.h b/src/common_define.h index 186c44f..927a731 100644 --- a/src/common_define.h +++ b/src/common_define.h @@ -226,6 +226,15 @@ typedef void* (*MallocFunc)(size_t size); (dest).len = strlen(src); \ } while (0) +#define FC_SET_STRING_NULL(dest) \ + do { \ + (dest).str = NULL; \ + (dest).len = 0; \ + } while (0) + +#define FC_IS_NULL_STRING(s) ((s)->str == NULL) +#define FC_IS_EMPTY_STRING(s) ((s)->len == 0) + #define fc_compare_string(s1, s2) fc_string_compare(s1, s2) static inline int fc_string_compare(const string_t *s1, const string_t *s2) @@ -247,6 +256,15 @@ static inline bool fc_string_equal(const string_t *s1, const string_t *s2) return (s1->len == s2->len) && (memcmp(s1->str, s2->str, s1->len) == 0); } +static inline bool fc_string_equal2(const string_t *s1, + const char *str2, const int len2) +{ + return (s1->len == len2) && (memcmp(s1->str, str2, s1->len) == 0); +} + +#define fc_string_equals(s1, s2) fc_string_equal(s1, s2) +#define fc_string_equals2(s1, str2, len2) fc_string_equal2(s1, str2, len2) + #ifdef __cplusplus } #endif diff --git a/src/fast_allocator.h b/src/fast_allocator.h index 9a23c2a..8f524ab 100644 --- a/src/fast_allocator.h +++ b/src/fast_allocator.h @@ -76,7 +76,7 @@ return error no, 0 for success, != 0 fail */ int fast_allocator_init(struct fast_allocator_context *acontext, const int64_t alloc_bytes_limit, const double expect_usage_ratio, - const int reclaim_interval, const bool need_lock); + const int reclaim_interval, const bool need_lock); /** allocator init @@ -93,7 +93,7 @@ return error no, 0 for success, != 0 fail int fast_allocator_init_ex(struct fast_allocator_context *acontext, struct fast_region_info *regions, const int region_count, const int64_t alloc_bytes_limit, const double expect_usage_ratio, - const int reclaim_interval, const bool need_lock); + const int reclaim_interval, const bool need_lock); /** allocator destroy diff --git a/src/fast_mpool.c b/src/fast_mpool.c index b101a0d..df78432 100644 --- a/src/fast_mpool.c +++ b/src/fast_mpool.c @@ -165,6 +165,25 @@ void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size) return NULL; } + +int fast_mpool_strdup(struct fast_mpool_man *mpool, string_t *dest, + const string_t *src) +{ + dest->str = (char *)fast_mpool_alloc(mpool, src->len); + if (dest->str == NULL) + { + logError("file: "__FILE__", line: %d, " + "alloc %d bytes from mpool fail", __LINE__, src->len); + return ENOMEM; + } + + if (src->len > 0) { + memcpy(dest->str, src->str, src->len); + } + dest->len = src->len; + return 0; +} + void fast_mpool_reset(struct fast_mpool_man *mpool) { struct fast_mpool_malloc *pMallocNode; diff --git a/src/fast_mpool.h b/src/fast_mpool.h index 5cbb1ca..b58b899 100644 --- a/src/fast_mpool.h +++ b/src/fast_mpool.h @@ -82,6 +82,18 @@ return the alloced ptr, return NULL if fail */ void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size); + +/** +alloc and copy string from the mpool +parameters: + mpool: the mpool pointer + dest: the dest string (return the alloced memory in dest->str) + src: the source string +return error no, 0 for success, != 0 fail +*/ +int fast_mpool_strdup(struct fast_mpool_man *mpool, string_t *dest, + const string_t *src); + /** get stats parameters: diff --git a/src/shared_func.c b/src/shared_func.c index 763415e..10e7eb5 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -616,6 +616,52 @@ char *trim(char *pStr) return pStr; } +void string_ltrim(string_t *s) +{ + char *p; + char *end; + + end = s->str + s->len; + for (p=s->str; pstr) + { + s->str = p; + s->len = end - p; + } +} + +void string_rtrim(string_t *s) +{ + char *p; + char *end; + + if (s->len == 0) + { + return; + } + + end = s->str + s->len - 1; + for (p = end; p >= s->str; p--) + { + if (!(' ' == *p || '\n' == *p || '\r' == *p || '\t' == *p)) + { + break; + } + } + + if (p != end) + { + s->len = (p + 1) - s->str; + } +} + char *formatDateYYYYMMDDHHMISS(const time_t t, char *szDateBuff, const int nSize) { time_t timer = t; diff --git a/src/shared_func.h b/src/shared_func.h index 220c8d1..f73e4a2 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -282,6 +282,31 @@ static inline char *fc_trim(char *pStr) return pStr; } +/** trim leading spaces ( \t\r\n) + * parameters: + * s: the string to trim + * return: none +*/ +void string_ltrim(string_t *s); + +/** trim tail spaces ( \t\r\n) + * parameters: + * s: the string to trim + * return: none +*/ +void string_rtrim(string_t *s); + +#define FC_STRING_TRIM(s) \ + do { \ + string_ltrim(s); \ + string_rtrim(s); \ + } while (0) + +static inline void string_trim(string_t *s) +{ + FC_STRING_TRIM(s); +} + /** copy string to BufferInfo * parameters: * pBuff: the dest buffer