fast_char_convert function changed

pull/10/head
yuqing 2016-11-26 13:04:45 +08:00
parent 33085e9bf6
commit 562513b358
2 changed files with 36 additions and 54 deletions

View File

@ -85,83 +85,62 @@ void char_converter_set_pair_ex(FastCharConverter *pCharConverter,
} }
int fast_char_convert(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; int count;
unsigned char *p;
unsigned char *pi; unsigned char *pi;
unsigned char *po;
unsigned char *end; unsigned char *end;
char fixed_buff[16 * 1024]; int out_size_sub1;
char *buff;
int max_size_sub1;
int remain_len; int remain_len;
if (pCharConverter->count <= 0) {
return 0;
}
count = 0; count = 0;
end = (unsigned char *)text + *text_len; po = (unsigned char *)output;
for (p=(unsigned char *)text; p<end; p++) if (out_size >= input_len) {
{ end = (unsigned char *)input + input_len;
if (pCharConverter->char_table[*p].op != FAST_CHAR_OP_NONE) } else {
{ end = (unsigned char *)input + out_size;
if (pCharConverter->char_table[*p].op == FAST_CHAR_OP_ADD_BACKSLASH) { }
for (pi=(unsigned char *)input; pi<end; pi++) {
if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) {
if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) {
break; break;
} }
*p = pCharConverter->char_table[*p].dest; *po++ = pCharConverter->char_table[*pi].dest;
++count; ++count;
} else {
*po++ = *pi;
} }
} }
remain_len = end - p; remain_len = end - pi;
if (remain_len == 0) { if (remain_len == 0) {
*out_len = po - (unsigned char *)output;
return count; return count;
} }
if (remain_len < sizeof(fixed_buff)) { out_size_sub1 = out_size - 1;
buff = fixed_buff; for (; pi<end; pi++) {
} else { if (po - (unsigned char *)output >= out_size_sub1) {
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<end; pi++)
{
if (p - (unsigned char *)text >= max_size_sub1)
{
logWarning("file: "__FILE__", line: %d, " logWarning("file: "__FILE__", line: %d, "
"exceeds max size: %d", __LINE__, max_size); "exceeds max size: %d", __LINE__, out_size);
break; break;
} }
if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) {
{ if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) {
if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) *po++ = '\\';
{
*p++ = '\\';
} }
*p++ = pCharConverter->char_table[*pi].dest; *po++ = pCharConverter->char_table[*pi].dest;
++count; ++count;
} } else {
else *po++ = *pi;
{
*p++ = *pi;
} }
} }
if (buff != fixed_buff) { *out_len = po - (unsigned char *)output;
free(buff);
}
*text_len = p - (unsigned char *)text;
return count; return count;
} }

View File

@ -97,13 +97,16 @@ void char_converter_set_pair_ex(FastCharConverter *pCharConverter,
* char convert function * char convert function
* parameters: * parameters:
* pCharConverter: the char converter * pCharConverter: the char converter
* text: the text to convert (input and output) * input: the input to convert
* text_len: the length of text (input and output) * input_len: the length of input
* max_size: max buff size * output: the input to convert
* out_len: the length of output
* out_size: output buff size
* return: converted char count * return: converted char count
*/ */
int fast_char_convert(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);
#ifdef __cplusplus #ifdef __cplusplus
} }