2018-09-27 19:06:57 +08:00
|
|
|
|
#include "pattern_replace.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>//fstat
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2018-09-28 15:41:29 +08:00
|
|
|
|
|
2018-09-28 19:27:44 +08:00
|
|
|
|
|
|
|
|
|
|
TEST(PatternReplace, Grouping1)
|
2018-09-28 15:41:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
const char* find="(?<name1>John)|(?<name2>李梅梅)|(?<name3>Jake)";
|
|
|
|
|
|
const char* replacement="${name1:+Joseph:${name2:+王桃花:Keith}}";
|
|
|
|
|
|
const char* input="John loves 李梅梅, 李梅梅 loves Jake and Jake doesn't care about John and 李梅梅.";
|
2018-09-28 19:27:44 +08:00
|
|
|
|
const char* expect="Joseph loves 王桃花, 王桃花 loves Keith and Keith doesn't care about Joseph and 王桃花.";
|
|
|
|
|
|
char* output=NULL;
|
|
|
|
|
|
size_t output_sz=0;
|
|
|
|
|
|
|
|
|
|
|
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
|
|
|
|
|
EXPECT_TRUE(output_sz>0);
|
|
|
|
|
|
// EXPECT_STREQ(output, expect);
|
|
|
|
|
|
printf("%s\n", output);
|
|
|
|
|
|
free(output);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
TEST(PatternReplace, Grouping2)
|
|
|
|
|
|
{
|
|
|
|
|
|
const char* find="(?<=中央)(?<ac1>空调)|(?<=家用)(?<ac2>空调)";
|
|
|
|
|
|
const char* replacement="${ac1:+Air conditioner:${ac2:+立式空调}}";
|
|
|
|
|
|
const char* input="中央空调,家用空调,只有第一个空调会被替换。";
|
2018-09-28 15:41:29 +08:00
|
|
|
|
char* output=NULL;
|
|
|
|
|
|
size_t output_sz=0;
|
|
|
|
|
|
|
|
|
|
|
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
|
|
|
|
|
EXPECT_TRUE(output_sz>0);
|
|
|
|
|
|
// EXPECT_STREQ(output, expect);
|
|
|
|
|
|
printf("%s\n", output);
|
|
|
|
|
|
free(output);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-28 19:27:44 +08:00
|
|
|
|
|
2018-09-28 15:41:29 +08:00
|
|
|
|
TEST(PatternReplace, Non_Zero_Terminated)
|
|
|
|
|
|
{
|
|
|
|
|
|
const char* filename="./test_data/facebook_index.html";
|
|
|
|
|
|
char fn_replaced[256];
|
|
|
|
|
|
const char* find="添加手机号";
|
|
|
|
|
|
const char* replacement="Add a Mobile Phone Number";
|
|
|
|
|
|
|
2018-09-27 19:06:57 +08:00
|
|
|
|
FILE* fp=NULL;
|
|
|
|
|
|
struct stat file_info;
|
|
|
|
|
|
stat(filename, &file_info);
|
2018-09-28 15:41:29 +08:00
|
|
|
|
size_t input_sz=file_info.st_size;
|
|
|
|
|
|
|
2018-09-27 19:06:57 +08:00
|
|
|
|
fp=fopen(filename,"r");
|
|
|
|
|
|
ASSERT_FALSE(fp==NULL);
|
|
|
|
|
|
if(fp==NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-09-28 15:41:29 +08:00
|
|
|
|
char* input=(char*)malloc(input_sz);
|
|
|
|
|
|
fread(input,1,input_sz,fp);
|
2018-09-27 19:06:57 +08:00
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
2018-09-28 15:41:29 +08:00
|
|
|
|
char* output=NULL;
|
|
|
|
|
|
size_t output_sz=0;
|
|
|
|
|
|
simple_replace(find, replacement, input, input_sz, &output, &output_sz);
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(output_sz>0);
|
|
|
|
|
|
EXPECT_TRUE(NULL==memmem(output, output_sz, find, strlen(find)));
|
|
|
|
|
|
EXPECT_TRUE(NULL!=memmem(output, output_sz, replacement, strlen(replacement)));
|
|
|
|
|
|
snprintf(fn_replaced,sizeof(fn_replaced), "%s.replaced", filename);
|
|
|
|
|
|
fp=fopen(fn_replaced, "w");
|
|
|
|
|
|
fwrite(output, 1,output_sz, fp);
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
free(output);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PatternReplace, ASCII)
|
|
|
|
|
|
{
|
|
|
|
|
|
const char* find="James";
|
|
|
|
|
|
const char* replacement="John";
|
|
|
|
|
|
const char* input="James, where are you? Alice is calling you.";
|
|
|
|
|
|
char* output=NULL;
|
|
|
|
|
|
size_t output_sz=0;
|
|
|
|
|
|
|
|
|
|
|
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
|
|
|
|
|
EXPECT_TRUE(output_sz>0);
|
|
|
|
|
|
EXPECT_TRUE(NULL==strstr(output, find));
|
|
|
|
|
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
|
|
|
|
|
|
|
|
|
|
|
// printf("%s\n", output);
|
|
|
|
|
|
free(output);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PatternReplace, UTF8)
|
|
|
|
|
|
{
|
|
|
|
|
|
const char* find="视频";
|
|
|
|
|
|
const char* replacement="短片";
|
|
|
|
|
|
const char* input="欢迎来到 Facebook开始添加好友吧!他们的视频、照片和帖子都会显示在这里。";
|
|
|
|
|
|
char* output=NULL;
|
|
|
|
|
|
size_t output_sz=0;
|
|
|
|
|
|
|
|
|
|
|
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
|
|
|
|
|
EXPECT_TRUE(output_sz>0);
|
|
|
|
|
|
EXPECT_TRUE(NULL==strstr(output, find));
|
|
|
|
|
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
|
|
|
|
|
|
|
|
|
|
|
// printf("%s\n", output);
|
|
|
|
|
|
free(output);
|
2018-09-27 19:06:57 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
|
|
}
|
|
|
|
|
|
|