add functions: string trim and fast_mpool_strdup

pull/37/head
yuqing 2018-09-13 18:21:32 +08:00
parent ce98e53ae2
commit 8403f2246c
6 changed files with 122 additions and 2 deletions

View File

@ -226,6 +226,15 @@ typedef void* (*MallocFunc)(size_t size);
(dest).len = strlen(src); \ (dest).len = strlen(src); \
} while (0) } 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) #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) 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); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -165,6 +165,25 @@ void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size)
return NULL; 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) void fast_mpool_reset(struct fast_mpool_man *mpool)
{ {
struct fast_mpool_malloc *pMallocNode; struct fast_mpool_malloc *pMallocNode;

View File

@ -82,6 +82,18 @@ return the alloced ptr, return NULL if fail
*/ */
void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size); 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 get stats
parameters: parameters:

View File

@ -616,6 +616,52 @@ char *trim(char *pStr)
return pStr; return pStr;
} }
void string_ltrim(string_t *s)
{
char *p;
char *end;
end = s->str + s->len;
for (p=s->str; p<end; p++)
{
if (!(' ' == *p|| '\n' == *p || '\r' == *p || '\t' == *p))
{
break;
}
}
if (p != s->str)
{
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) char *formatDateYYYYMMDDHHMISS(const time_t t, char *szDateBuff, const int nSize)
{ {
time_t timer = t; time_t timer = t;

View File

@ -282,6 +282,31 @@ static inline char *fc_trim(char *pStr)
return 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 /** copy string to BufferInfo
* parameters: * parameters:
* pBuff: the dest buffer * pBuff: the dest buffer