From fe4f42f090c18fe46708bc07e2d219ba066caf31 Mon Sep 17 00:00:00 2001 From: yuqing Date: Fri, 25 Nov 2016 15:07:48 +0800 Subject: [PATCH] char_converter code refine --- src/char_converter.c | 76 ++++++++++++++++++++++++++++++++++++++++---- src/char_converter.h | 34 ++++++++++++++++---- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/src/char_converter.c b/src/char_converter.c index 41bab29..111acdc 100644 --- a/src/char_converter.c +++ b/src/char_converter.c @@ -33,7 +33,8 @@ int char_converter_init(FastCharConverter *pCharConverter, for (i=0; ichar_table[src] = charPairs[i].dest; + pCharConverter->char_table[src].op = FAST_CHAR_OP_NO_BACKSLASH; + pCharConverter->char_table[src].dest = charPairs[i].dest; } return 0; } @@ -63,15 +64,27 @@ int std_space_char_converter_init(FastCharConverter *pCharConverter, void char_converter_set_pair(FastCharConverter *pCharConverter, const unsigned char src, const unsigned char dest) { - pCharConverter->char_table[src] = dest; + char_converter_set_pair_ex(pCharConverter, src, FAST_CHAR_OP_NO_BACKSLASH, dest); +} + +void char_converter_set_pair_ex(FastCharConverter *pCharConverter, + const unsigned char src, const unsigned op, const unsigned char dest) +{ + pCharConverter->char_table[src].op = op; + pCharConverter->char_table[src].dest = dest; } int fast_char_convert(FastCharConverter *pCharConverter, - char *text, const int text_len) + char *text, int *text_len, const int max_size) { int count; unsigned char *p; + unsigned char *pi; unsigned char *end; + char fixed_buff[16 * 1024]; + char *buff; + int max_size_sub1; + int remain_len; if (pCharConverter->count <= 0) { @@ -79,16 +92,67 @@ int fast_char_convert(FastCharConverter *pCharConverter, } count = 0; - end = (unsigned char *)text + text_len; + end = (unsigned char *)text + *text_len; for (p=(unsigned char *)text; pchar_table[*p] != 0) + if (pCharConverter->char_table[*p].op != FAST_CHAR_OP_NONE) { - *p = pCharConverter->char_table[*p]; + if (pCharConverter->char_table[*p].op == FAST_CHAR_OP_ADD_BACKSLASH) { + break; + } + + *p = pCharConverter->char_table[*p].dest; ++count; } } + remain_len = end - p; + if (remain_len == 0) { + return count; + } + + if (remain_len < sizeof(fixed_buff)) { + buff = fixed_buff; + } else { + buff = (char *)malloc(remain_len); + if (buff == NULL) { + logError("file: "__FILE__", line: %d, " + "malloc %d bytes fail", __LINE__, remain_len); + return count; + } + } + memcpy(buff, p, remain_len); + + max_size_sub1 = max_size - 1; + end = (unsigned char *)buff + remain_len; + for (pi=(unsigned char *)buff; pi= max_size_sub1) + { + logWarning("file: "__FILE__", line: %d, " + "exceeds max size: %d", __LINE__, max_size); + break; + } + if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) + { + if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) + { + *p++ = '\\'; + } + + *p++ = pCharConverter->char_table[*pi].dest; + ++count; + } + else + { + *p++ = *pi; + } + } + + if (buff != fixed_buff) { + free(buff); + } + *text_len = p - (unsigned char *)text; return count; } diff --git a/src/char_converter.h b/src/char_converter.h index 206e82c..1cbfe04 100644 --- a/src/char_converter.h +++ b/src/char_converter.h @@ -20,12 +20,22 @@ extern "C" { #define FAST_MAX_CHAR_COUNT 256 +#define FAST_CHAR_OP_NONE 0 +#define FAST_CHAR_OP_ADD_BACKSLASH 1 +#define FAST_CHAR_OP_NO_BACKSLASH 2 + typedef struct fast_char_pair { unsigned char src; unsigned char dest; } FastCharPair; +typedef struct fast_char_target +{ + unsigned char op; + unsigned char dest; +} FastCharTarget; + typedef struct fast_char_converter { /* @@ -36,7 +46,7 @@ typedef struct fast_char_converter /* * char table to convert * */ - unsigned char char_table[FAST_MAX_CHAR_COUNT]; + FastCharTarget char_table[FAST_MAX_CHAR_COUNT]; } FastCharConverter; /** @@ -61,7 +71,7 @@ int std_space_char_converter_init(FastCharConverter *pCharConverter, const unsigned char dest_base); /** - * standard space chars to convert + * set char pair to converter * parameters: * pCharConverter: the char converter * src: the src char @@ -71,17 +81,29 @@ int std_space_char_converter_init(FastCharConverter *pCharConverter, void char_converter_set_pair(FastCharConverter *pCharConverter, const unsigned char src, const unsigned char dest); +/** + * set char pair to converter + * parameters: + * pCharConverter: the char converter + * src: the src char + * op: the operator type + * dest: the dest char + * return: none +*/ +void char_converter_set_pair_ex(FastCharConverter *pCharConverter, + const unsigned char src, const unsigned op, const unsigned char dest); + /** * char convert function * parameters: * pCharConverter: the char converter - * text: the text to convert - * text_len: the length of text + * text: the text to convert (input and output) + * text_len: the length of text (input and output) + * max_size: max buff size * return: converted char count */ int fast_char_convert(FastCharConverter *pCharConverter, - char *text, const int text_len); - + char *text, int *text_len, const int max_size); #ifdef __cplusplus }