char_converter code refine

pull/10/head
yuqing 2016-11-26 13:36:14 +08:00
parent 562513b358
commit 2e1d5eb8d2
4 changed files with 69 additions and 10 deletions

View File

@ -18,12 +18,18 @@
int char_convert_loader_init(FastCharConverter *pCharConverter, int char_convert_loader_init(FastCharConverter *pCharConverter,
const IniItem *items, const int count) const IniItem *items, const int count)
{
char_convert_loader_init(pCharConverter, NULL, 0);
return char_convert_loader_add(pCharConverter, items, count);
}
int char_convert_loader_add(FastCharConverter *pCharConverter,
const IniItem *items, const int count)
{ {
const IniItem *pItem; const IniItem *pItem;
const IniItem *pEnd; const IniItem *pEnd;
int result; int result;
char_convert_loader_init(pCharConverter, NULL, 0);
pEnd = items + count; pEnd = items + count;
for (pItem=items; pItem<pEnd; pItem++) { for (pItem=items; pItem<pEnd; pItem++) {
result = char_convert_loader_set_pair(pCharConverter, result = char_convert_loader_set_pair(pCharConverter,

View File

@ -31,6 +31,17 @@ extern "C" {
int char_convert_loader_init(FastCharConverter *pCharConverter, int char_convert_loader_init(FastCharConverter *pCharConverter,
const IniItem *items, const int count); const IniItem *items, const int count);
/**
* char converter init function
* parameters:
* pCharConverter: the char converter
* items: the char key value pairs
* count: the count of kv pairs
* return: 0 for success, != 0 fail
*/
int char_convert_loader_add(FastCharConverter *pCharConverter,
const IniItem *items, const int count);
/** /**
* set char src and dest pair * set char src and dest pair
* parameters: * parameters:

View File

@ -15,8 +15,9 @@
#include "shared_func.h" #include "shared_func.h"
#include "char_converter.h" #include "char_converter.h"
int char_converter_init(FastCharConverter *pCharConverter, int char_converter_init_ex(FastCharConverter *pCharConverter,
const FastCharPair *charPairs, const int count) const FastCharPair *charPairs, const int count,
const unsigned op)
{ {
int i; int i;
unsigned char src; unsigned char src;
@ -33,7 +34,7 @@ int char_converter_init(FastCharConverter *pCharConverter,
for (i=0; i<count; i++) for (i=0; i<count; i++)
{ {
src = charPairs[i].src; src = charPairs[i].src;
pCharConverter->char_table[src].op = FAST_CHAR_OP_NO_BACKSLASH; pCharConverter->char_table[src].op = op;
pCharConverter->char_table[src].dest = charPairs[i].dest; pCharConverter->char_table[src].dest = charPairs[i].dest;
} }
return 0; return 0;
@ -61,10 +62,28 @@ int std_space_char_converter_init(FastCharConverter *pCharConverter,
return char_converter_init(pCharConverter, pairs, SPACE_CHAR_PAIR_COUNT); return char_converter_init(pCharConverter, pairs, SPACE_CHAR_PAIR_COUNT);
} }
int std_spaces_add_backslash_converter_init(FastCharConverter *pCharConverter)
{
#define SPACE_CHAR_PAIR_COUNT 7
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT];
pairs[0].src = '\0'; pairs[0].dest = '0';
pairs[1].src = '\t'; pairs[1].dest = 't';
pairs[2].src = '\n'; pairs[2].dest = 'n';
pairs[3].src = '\v'; pairs[3].dest = 'v';
pairs[4].src = '\f'; pairs[4].dest = 'f';
pairs[5].src = '\r'; pairs[5].dest = 'r';
pairs[6].src = ' '; pairs[6].dest = '-';
return char_converter_init_ex(pCharConverter, pairs,
SPACE_CHAR_PAIR_COUNT, FAST_CHAR_OP_ADD_BACKSLASH);
}
void char_converter_set_pair(FastCharConverter *pCharConverter, void char_converter_set_pair(FastCharConverter *pCharConverter,
const unsigned char src, const unsigned char dest) const unsigned char src, const unsigned char dest)
{ {
char_converter_set_pair_ex(pCharConverter, src, FAST_CHAR_OP_NO_BACKSLASH, dest); char_converter_set_pair_ex(pCharConverter, src,
FAST_CHAR_OP_NO_BACKSLASH, dest);
} }
void char_converter_set_pair_ex(FastCharConverter *pCharConverter, void char_converter_set_pair_ex(FastCharConverter *pCharConverter,
@ -93,7 +112,6 @@ int fast_char_convert(FastCharConverter *pCharConverter,
unsigned char *po; unsigned char *po;
unsigned char *end; unsigned char *end;
int out_size_sub1; int out_size_sub1;
int remain_len;
count = 0; count = 0;
po = (unsigned char *)output; po = (unsigned char *)output;
@ -115,8 +133,7 @@ int fast_char_convert(FastCharConverter *pCharConverter,
} }
} }
remain_len = end - pi; if (pi == end) {
if (remain_len == 0) {
*out_len = po - (unsigned char *)output; *out_len = po - (unsigned char *)output;
return count; return count;
} }

View File

@ -55,10 +55,27 @@ typedef struct fast_char_converter
* pCharConverter: the char converter * pCharConverter: the char converter
* charPairs: the char pairs * charPairs: the char pairs
* count: the count of char pairs * count: the count of char pairs
* op: the operator type
* return: 0 for success, != 0 fail * return: 0 for success, != 0 fail
*/ */
int char_converter_init(FastCharConverter *pCharConverter, int char_converter_init_ex(FastCharConverter *pCharConverter,
const FastCharPair *charPairs, const int count); const FastCharPair *charPairs, const int count,
const unsigned op);
/**
* char converter init function
* parameters:
* pCharConverter: the char converter
* charPairs: the char pairs
* count: the count of char pairs
* return: 0 for success, != 0 fail
*/
static inline int char_converter_init(FastCharConverter *pCharConverter,
const FastCharPair *charPairs, const int count)
{
return char_converter_init_ex(pCharConverter, charPairs, count,
FAST_CHAR_OP_NO_BACKSLASH);
}
/** /**
* standard space chars to convert * standard space chars to convert
@ -70,6 +87,14 @@ int char_converter_init(FastCharConverter *pCharConverter,
int std_space_char_converter_init(FastCharConverter *pCharConverter, int std_space_char_converter_init(FastCharConverter *pCharConverter,
const unsigned char dest_base); const unsigned char dest_base);
/**
* standard space chars init to add backslash
* parameters:
* pCharConverter: the char converter
* return: 0 for success, != 0 fail
*/
int std_spaces_add_backslash_converter_init(FastCharConverter *pCharConverter);
/** /**
* set char pair to converter * set char pair to converter
* parameters: * parameters: