From 562513b358d2c38405052a6e2d7c3e8fce3611fd Mon Sep 17 00:00:00 2001 From: yuqing Date: Sat, 26 Nov 2016 13:04:45 +0800 Subject: [PATCH] fast_char_convert function changed --- src/char_converter.c | 79 ++++++++++++++++---------------------------- src/char_converter.h | 11 +++--- 2 files changed, 36 insertions(+), 54 deletions(-) diff --git a/src/char_converter.c b/src/char_converter.c index b60116c..badbf47 100644 --- a/src/char_converter.c +++ b/src/char_converter.c @@ -85,83 +85,62 @@ void char_converter_set_pair_ex(FastCharConverter *pCharConverter, } int fast_char_convert(FastCharConverter *pCharConverter, - char *text, int *text_len, const int max_size) + const char *input, const int input_len, + char *output, int *out_len, const int out_size) { int count; - unsigned char *p; unsigned char *pi; + unsigned char *po; unsigned char *end; - char fixed_buff[16 * 1024]; - char *buff; - int max_size_sub1; + int out_size_sub1; int remain_len; - if (pCharConverter->count <= 0) { - return 0; - } - count = 0; - end = (unsigned char *)text + *text_len; - for (p=(unsigned char *)text; pchar_table[*p].op != FAST_CHAR_OP_NONE) - { - if (pCharConverter->char_table[*p].op == FAST_CHAR_OP_ADD_BACKSLASH) { + po = (unsigned char *)output; + if (out_size >= input_len) { + end = (unsigned char *)input + input_len; + } else { + end = (unsigned char *)input + out_size; + } + for (pi=(unsigned char *)input; pichar_table[*pi].op != FAST_CHAR_OP_NONE) { + if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) { break; } - *p = pCharConverter->char_table[*p].dest; + *po++ = pCharConverter->char_table[*pi].dest; ++count; + } else { + *po++ = *pi; } } - remain_len = end - p; + remain_len = end - pi; if (remain_len == 0) { + *out_len = po - (unsigned char *)output; 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) - { + out_size_sub1 = out_size - 1; + for (; pi= out_size_sub1) { logWarning("file: "__FILE__", line: %d, " - "exceeds max size: %d", __LINE__, max_size); + "exceeds max size: %d", __LINE__, out_size); break; } - if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) - { - if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) - { - *p++ = '\\'; + if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) { + if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) { + *po++ = '\\'; } - *p++ = pCharConverter->char_table[*pi].dest; + *po++ = pCharConverter->char_table[*pi].dest; ++count; - } - else - { - *p++ = *pi; + } else { + *po++ = *pi; } } - if (buff != fixed_buff) { - free(buff); - } - *text_len = p - (unsigned char *)text; + *out_len = po - (unsigned char *)output; return count; } diff --git a/src/char_converter.h b/src/char_converter.h index 1cbfe04..dd7742c 100644 --- a/src/char_converter.h +++ b/src/char_converter.h @@ -97,13 +97,16 @@ void char_converter_set_pair_ex(FastCharConverter *pCharConverter, * char convert function * parameters: * pCharConverter: the char converter - * text: the text to convert (input and output) - * text_len: the length of text (input and output) - * max_size: max buff size + * input: the input to convert + * input_len: the length of input + * output: the input to convert + * out_len: the length of output + * out_size: output buff size * return: converted char count */ int fast_char_convert(FastCharConverter *pCharConverter, - char *text, int *text_len, const int max_size); + const char *input, const int input_len, + char *output, int *out_len, const int out_size); #ifdef __cplusplus }