单元测试通过
This commit is contained in:
@@ -135,12 +135,8 @@ size_t select_replace_rule(enum replace_zone zone, const struct replace_rule * r
|
||||
return j;
|
||||
}
|
||||
|
||||
static struct evbuffer * replace_string(const char * in, size_t in_sz, const struct replace_rule * zone)
|
||||
size_t replace_string(const char * in, size_t in_sz, const struct replace_rule * zone, char** out)
|
||||
{
|
||||
|
||||
int status = 0, is_replaced = 0;
|
||||
struct evbuffer * out = NULL;
|
||||
|
||||
size_t replace_len = strlen(zone->replace_with);
|
||||
|
||||
assert(strlen(zone->find) != 0);
|
||||
@@ -154,59 +150,82 @@ static struct evbuffer * replace_string(const char * in, size_t in_sz, const str
|
||||
|
||||
pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
|
||||
if (re == 0)
|
||||
return NULL;
|
||||
return -1;
|
||||
|
||||
pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
|
||||
|
||||
PCRE2_SIZE outlen = in_sz*2;
|
||||
PCRE2_UCHAR* output = (PCRE2_UCHAR*)malloc(sizeof(PCRE2_UCHAR)*outlen);
|
||||
|
||||
int rc = pcre2_substitute(re, subject, in_sz, 0, PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement, PCRE2_ZERO_TERMINATED, output, &outlen);
|
||||
if (rc >= 0)
|
||||
printf("%s\n", output);
|
||||
|
||||
pcre2_code_free(re);
|
||||
free(output);
|
||||
return NULL;
|
||||
PCRE2_SIZE outbuff_size = in_sz+sizeof(replacement)*MAX_EDIT_MATCHES;
|
||||
PCRE2_SIZE outlen = 0;
|
||||
PCRE2_UCHAR* out_buffer = NULL;
|
||||
not_enough_mem_retry:
|
||||
out_buffer = (PCRE2_UCHAR*)malloc(sizeof(PCRE2_UCHAR)*outbuff_size);
|
||||
outlen = outbuff_size;
|
||||
int rc = pcre2_substitute(re, subject, in_sz, 0, PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH, 0, 0, replacement, PCRE2_ZERO_TERMINATED, out_buffer, &outlen);
|
||||
if(outlen>outbuff_size)
|
||||
{
|
||||
outbuff_size=outlen;
|
||||
free(out_buffer);
|
||||
out_buffer=NULL;
|
||||
goto not_enough_mem_retry;
|
||||
}
|
||||
if(rc<=0)
|
||||
{
|
||||
free(out_buffer);
|
||||
outlen=rc;
|
||||
}
|
||||
else
|
||||
{
|
||||
*out=(char*)out_buffer;
|
||||
}
|
||||
pcre2_code_free(re);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
struct evbuffer * execute_replace_rule(const char * in, size_t in_sz,
|
||||
enum replace_zone zone, const struct replace_rule * rules, size_t n_rule)
|
||||
size_t execute_replace_rule(const char * in, size_t in_sz,
|
||||
enum replace_zone zone, const struct replace_rule * rules, size_t n_rule, char** out)
|
||||
{
|
||||
const struct replace_rule * todo[n_rule];
|
||||
size_t n_todo = 0, i = 0, interator_sz=0;
|
||||
struct evbuffer * out = NULL;
|
||||
size_t n_todo = 0, i = 0, interator_sz=0, pre_out_sz=0;
|
||||
const char * interator = NULL;
|
||||
struct evbuffer * new_out = NULL, * pre_out = NULL;
|
||||
if (in == 0)
|
||||
char* new_out = NULL, * pre_out = NULL;
|
||||
size_t output_size=0;
|
||||
if (in_sz == 0 || in==NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
//Do not process buffer that contains '\0'.
|
||||
if (0 != memchr(in, '\0', in_sz))
|
||||
{
|
||||
return NULL;
|
||||
return 0;
|
||||
}
|
||||
n_todo = select_replace_rule(zone, rules, n_rule, todo, n_rule);
|
||||
interator = in;
|
||||
interator_sz = in_sz;
|
||||
for (i = 0; i < n_todo; i++)
|
||||
{
|
||||
new_out = replace_string(interator, interator_sz, todo[i]);
|
||||
if (new_out != NULL)
|
||||
output_size = replace_string(interator, interator_sz, todo[i], &new_out);
|
||||
if (output_size == 0)
|
||||
{
|
||||
pre_out = out;
|
||||
out = new_out;
|
||||
interator = (char *) evbuffer_pullup(out, -1);
|
||||
interator_sz = evbuffer_get_length(out);
|
||||
if (pre_out != NULL)
|
||||
{
|
||||
evbuffer_free(pre_out);
|
||||
pre_out = NULL;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (pre_out != NULL)
|
||||
{
|
||||
free(pre_out);
|
||||
pre_out = NULL;
|
||||
}
|
||||
pre_out = new_out;
|
||||
pre_out_sz = output_size;
|
||||
|
||||
interator = new_out;
|
||||
interator_sz = output_size;
|
||||
|
||||
new_out=NULL;
|
||||
output_size=0;
|
||||
}
|
||||
if(pre_out_sz>0)
|
||||
{
|
||||
*out=pre_out;
|
||||
return pre_out_sz;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user