add function fast_char_unescape
parent
773f1bc28c
commit
d87d98dbe0
5
HISTORY
5
HISTORY
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
Version 1.44 2020-03-03
|
Version 1.44 2020-03-04
|
||||||
* add test file src/tests/test_pthread_lock.c
|
* add test file src/tests/test_pthread_lock.c
|
||||||
* add uniq_skiplist.[hc]
|
* add uniq_skiplist.[hc]
|
||||||
* add function split_string_ex
|
* add function split_string_ex
|
||||||
|
|
@ -13,7 +13,8 @@ Version 1.44 2020-03-03
|
||||||
* shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime
|
* shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime
|
||||||
* fast_mpool.[hc]: change function fast_mpool_strdup
|
* fast_mpool.[hc]: change function fast_mpool_strdup
|
||||||
* fast_allocator.[hc]: add function fast_allocator_strdup
|
* fast_allocator.[hc]: add function fast_allocator_strdup
|
||||||
* shared_func.[hc]: add functions getFileSize
|
* shared_func.[hc]: add function getFileSize
|
||||||
|
* char_converter.[hc]: add function fast_char_unescape
|
||||||
|
|
||||||
Version 1.43 2019-12-25
|
Version 1.43 2019-12-25
|
||||||
* replace function call system to getExecResult,
|
* replace function call system to getExecResult,
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ int char_converter_init_ex(FastCharConverter *pCharConverter,
|
||||||
const unsigned op)
|
const unsigned op)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
unsigned char src;
|
unsigned char from;
|
||||||
|
unsigned char to;
|
||||||
if (count > FAST_MAX_CHAR_COUNT)
|
if (count > FAST_MAX_CHAR_COUNT)
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
|
@ -33,10 +34,15 @@ int char_converter_init_ex(FastCharConverter *pCharConverter,
|
||||||
pCharConverter->count = count;
|
pCharConverter->count = count;
|
||||||
for (i=0; i<count; i++)
|
for (i=0; i<count; i++)
|
||||||
{
|
{
|
||||||
src = charPairs[i].src;
|
from = charPairs[i].src;
|
||||||
pCharConverter->char_table[src].op = op;
|
to = charPairs[i].dest;
|
||||||
pCharConverter->char_table[src].dest = charPairs[i].dest;
|
pCharConverter->char_table[from].op = op;
|
||||||
|
pCharConverter->char_table[from].dest = to;
|
||||||
|
|
||||||
|
pCharConverter->unescape_chars[to].op = op;
|
||||||
|
pCharConverter->unescape_chars[to].dest = from;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,14 +73,14 @@ int std_spaces_add_backslash_converter_init(FastCharConverter *pCharConverter)
|
||||||
#define SPACE_CHAR_PAIR_COUNT2 8
|
#define SPACE_CHAR_PAIR_COUNT2 8
|
||||||
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT2];
|
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT2];
|
||||||
|
|
||||||
pairs[0].src = '\0'; pairs[0].dest = '0';
|
FAST_CHAR_MAKE_PAIR(pairs[0], '\0', '0');
|
||||||
pairs[1].src = '\t'; pairs[1].dest = 't';
|
FAST_CHAR_MAKE_PAIR(pairs[1], '\t', 't');
|
||||||
pairs[2].src = '\n'; pairs[2].dest = 'n';
|
FAST_CHAR_MAKE_PAIR(pairs[2], '\n', 'n');
|
||||||
pairs[3].src = '\v'; pairs[3].dest = 'v';
|
FAST_CHAR_MAKE_PAIR(pairs[3], '\v', 'v');
|
||||||
pairs[4].src = '\f'; pairs[4].dest = 'f';
|
FAST_CHAR_MAKE_PAIR(pairs[4], '\f', 'f');
|
||||||
pairs[5].src = '\r'; pairs[5].dest = 'r';
|
FAST_CHAR_MAKE_PAIR(pairs[5], '\r', 'r');
|
||||||
pairs[6].src = ' '; pairs[6].dest = '-';
|
FAST_CHAR_MAKE_PAIR(pairs[6], ' ', 's');
|
||||||
pairs[7].src = '\\'; pairs[7].dest = '\\';
|
FAST_CHAR_MAKE_PAIR(pairs[7], '\\', '\\');
|
||||||
|
|
||||||
return char_converter_init_ex(pCharConverter, pairs,
|
return char_converter_init_ex(pCharConverter, pairs,
|
||||||
SPACE_CHAR_PAIR_COUNT2, FAST_CHAR_OP_ADD_BACKSLASH);
|
SPACE_CHAR_PAIR_COUNT2, FAST_CHAR_OP_ADD_BACKSLASH);
|
||||||
|
|
@ -142,7 +148,7 @@ int fast_char_convert(FastCharConverter *pCharConverter,
|
||||||
out_size_sub1 = out_size - 1;
|
out_size_sub1 = out_size - 1;
|
||||||
for (; pi<end; pi++) {
|
for (; pi<end; pi++) {
|
||||||
if (po - (unsigned char *)output >= out_size_sub1) {
|
if (po - (unsigned char *)output >= out_size_sub1) {
|
||||||
logDebug("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"exceeds max size: %d", __LINE__, out_size);
|
"exceeds max size: %d", __LINE__, out_size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -162,3 +168,47 @@ int fast_char_convert(FastCharConverter *pCharConverter,
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fast_char_unescape(FastCharConverter *pCharConverter, char *str, int *len)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
unsigned char *backslash;
|
||||||
|
unsigned char *p;
|
||||||
|
unsigned char *end;
|
||||||
|
unsigned char *dest;
|
||||||
|
|
||||||
|
backslash = (unsigned char *)memchr(str, '\\', *len);
|
||||||
|
if (backslash == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
end = (unsigned char *)str + *len;
|
||||||
|
p = dest = backslash;
|
||||||
|
while (p < end) {
|
||||||
|
if (*p == '\\') {
|
||||||
|
if (p + 1 < end) {
|
||||||
|
if (pCharConverter->unescape_chars[p[1]].op ==
|
||||||
|
FAST_CHAR_OP_ADD_BACKSLASH)
|
||||||
|
{
|
||||||
|
*dest++ = pCharConverter->unescape_chars[p[1]].dest;
|
||||||
|
p += 2;
|
||||||
|
++count;
|
||||||
|
} else {
|
||||||
|
*dest++ = *p++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*dest++ = *p++;
|
||||||
|
}
|
||||||
|
} else if (pCharConverter->unescape_chars[*p].op ==
|
||||||
|
FAST_CHAR_OP_NO_BACKSLASH)
|
||||||
|
{
|
||||||
|
*dest++ = pCharConverter->unescape_chars[*p++].dest;
|
||||||
|
++count;
|
||||||
|
} else {
|
||||||
|
*dest++ = *p++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*len = dest - (unsigned char *)str;
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ extern "C" {
|
||||||
#define FAST_CHAR_OP_ADD_BACKSLASH 1
|
#define FAST_CHAR_OP_ADD_BACKSLASH 1
|
||||||
#define FAST_CHAR_OP_NO_BACKSLASH 2
|
#define FAST_CHAR_OP_NO_BACKSLASH 2
|
||||||
|
|
||||||
|
#define FAST_CHAR_MAKE_PAIR(pair, from, to) \
|
||||||
|
pair.src = from; pair.dest = to
|
||||||
|
|
||||||
typedef struct fast_char_pair
|
typedef struct fast_char_pair
|
||||||
{
|
{
|
||||||
unsigned char src;
|
unsigned char src;
|
||||||
|
|
@ -47,6 +50,11 @@ typedef struct fast_char_converter
|
||||||
* char table to convert
|
* char table to convert
|
||||||
* */
|
* */
|
||||||
FastCharTarget char_table[FAST_MAX_CHAR_COUNT];
|
FastCharTarget char_table[FAST_MAX_CHAR_COUNT];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* char table to unescape
|
||||||
|
* */
|
||||||
|
FastCharTarget unescape_chars[FAST_MAX_CHAR_COUNT];
|
||||||
} FastCharConverter;
|
} FastCharConverter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -133,6 +141,21 @@ int fast_char_convert(FastCharConverter *pCharConverter,
|
||||||
const char *input, const int input_len,
|
const char *input, const int input_len,
|
||||||
char *output, int *out_len, const int out_size);
|
char *output, int *out_len, const int out_size);
|
||||||
|
|
||||||
|
#define fast_char_escape(pCharConverter, input, input_len, \
|
||||||
|
output, out_len, out_size) \
|
||||||
|
fast_char_convert(pCharConverter, input, input_len, \
|
||||||
|
output, out_len, out_size)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* char unescape function
|
||||||
|
* parameters:
|
||||||
|
* pCharConverter: the char converter
|
||||||
|
* str: the string to unescape
|
||||||
|
* len: the input string length and store the unscaped string length
|
||||||
|
* return: converted char count
|
||||||
|
*/
|
||||||
|
int fast_char_unescape(FastCharConverter *pCharConverter, char *str, int *len);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo
|
||||||
test_id_generator test_ini_parser test_char_convert test_char_convert_loader \
|
test_id_generator test_ini_parser test_char_convert test_char_convert_loader \
|
||||||
test_logger test_skiplist_set test_crc32 test_thourands_seperator test_sched_thread \
|
test_logger test_skiplist_set test_crc32 test_thourands_seperator test_sched_thread \
|
||||||
test_json_parser test_pthread_lock test_uniq_skiplist test_split_string \
|
test_json_parser test_pthread_lock test_uniq_skiplist test_split_string \
|
||||||
test_server_id_func
|
test_server_id_func test_pipe
|
||||||
|
|
||||||
all: $(ALL_PRGS)
|
all: $(ALL_PRGS)
|
||||||
.c:
|
.c:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include "fastcommon/logger.h"
|
||||||
|
#include "fastcommon/shared_func.h"
|
||||||
|
|
||||||
|
#define LOOP (200 * 1000)
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
int pipe_fds[2];
|
||||||
|
int i;
|
||||||
|
long n;
|
||||||
|
pid_t pid;
|
||||||
|
int64_t start_time;
|
||||||
|
|
||||||
|
log_init();
|
||||||
|
|
||||||
|
if (pipe(pipe_fds) != 0) {
|
||||||
|
result = errno != 0 ? errno : EPERM;
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"call pipe fail, "
|
||||||
|
"errno: %d, error info: %s",
|
||||||
|
__LINE__, result, strerror(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
result = errno != 0 ? errno : EPERM;
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"call fork fail, "
|
||||||
|
"errno: %d, error info: %s",
|
||||||
|
__LINE__, result, strerror(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (pid == 0) {
|
||||||
|
printf("i am the child proccess: %d\n", getpid());
|
||||||
|
start_time = get_current_time_ms();
|
||||||
|
for (i=0;i <LOOP; i++) {
|
||||||
|
if (read(pipe_fds[0], &n, sizeof(n)) != sizeof(n)) {
|
||||||
|
result = errno != 0 ? errno : EPERM;
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"call read fail, "
|
||||||
|
"errno: %d, error info: %s",
|
||||||
|
__LINE__, result, strerror(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("child done, time used: %"PRId64" ms\n", get_current_time_ms() - start_time);
|
||||||
|
} else {
|
||||||
|
printf("the child proccess: %d\n", pid);
|
||||||
|
start_time = get_current_time_ms();
|
||||||
|
for (i=0;i <LOOP; i++) {
|
||||||
|
n = i + 1;
|
||||||
|
if (write(pipe_fds[1], &n, sizeof(n)) != sizeof(n)) {
|
||||||
|
result = errno != 0 ? errno : EPERM;
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"call write fail, "
|
||||||
|
"errno: %d, error info: %s",
|
||||||
|
__LINE__, result, strerror(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("parent done, time used: %"PRId64" ms\n", get_current_time_ms() - start_time);
|
||||||
|
sleep(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue