Skip to content

Instantly share code, notes, and snippets.

@vls
Created February 21, 2012 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vls/1873417 to your computer and use it in GitHub Desktop.
Save vls/1873417 to your computer and use it in GitHub Desktop.
test program for re2
#include <re2/re2.h>
#include <iostream>
#include <string>
#include <cassert>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <time.h>
#include <unistd.h>
#define CLOCK CLOCK_MONOTONIC
using namespace std;
timespec diff(timespec& start, timespec& end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
int main() {
timespec start, end, temp;
long seconds, useconds;
/*
clock_gettime(CLOCK, &start);
sleep(3);
clock_gettime(CLOCK, &end);
temp = diff(start, end);
seconds = temp.tv_sec;
useconds = temp.tv_nsec;
printf("load time=%ld, %ld\n", seconds, useconds);
*/
int i;
const char* s_regex_file = "regex/clear_warning.txt";
const char* s_input_file = "test-utf-8.log";
char buf[4096];
FILE* fp = fopen(s_regex_file, "r");
if(NULL == fp) {
perror("no regex file");
exit(1);
}
int size = 0;
while(fgets(buf, 4096, fp) != NULL) {
size++;
}
printf("count re2 = %d\n", size);
fseek(fp, 0, SEEK_SET);
RE2** regex_arr = (RE2**) malloc(sizeof(RE2*) * size);
RE2* ptr_re2;
int count = 0;
clock_gettime(CLOCK, &start);
while(fscanf(fp, "%s", buf) != EOF) {
//printf("regex: %s\n", buf);
ptr_re2 = new RE2(buf);
assert(ptr_re2->ok());
regex_arr[count] = ptr_re2;
count += 1;
}
clock_gettime(CLOCK, &end);
temp = diff(start, end);
seconds = temp.tv_sec;
useconds = temp.tv_nsec;
printf("load time=%ld, %ld\n", seconds, useconds / 1000/ 1000);
printf("begin to read input\n");
clock_gettime(CLOCK, &start);
FILE* fp_input = fopen(s_input_file, "r");
if(NULL == fp_input) {
perror("no input.txt");
exit(1);
}
FILE* wf = fopen("output/c-plain-output.txt", "w");
bool match;
char* pos;
int j;
int match_count = 0;
while(fgets(buf, 4096, fp_input) != NULL) {
j = 8-1;
pos=strchr(buf, ' ');
while(--j && pos) {
pos = strchr(pos+1, ' ');
}
if(!pos) {
continue;
}
//printf("input:[%s](%d)\n", pos+1, strlen(pos+1));
for(i=0;i<size;i++) {
ptr_re2 = regex_arr[i];
match = RE2::PartialMatch(pos+1, *ptr_re2);
if(match) {
match_count += 1;
fwrite(buf, 1, strlen(buf) - 1, wf);
fwrite("\t", 1, 1, wf);
fwrite("警告", 1, 6, wf);
fwrite("\t", 1, 1, wf);
const char* cptr = ptr_re2->pattern().c_str();
fwrite(cptr, 1, strlen(cptr), wf);
fwrite("\n", 1,1, wf);
break;
}
}
}
clock_gettime(CLOCK, &end);
temp = diff(start, end);
seconds = temp.tv_sec;
useconds = temp.tv_nsec;
printf("process time=%ld, %ld\n", seconds, useconds / 1000 / 1000);
printf("match_count = %d\n", match_count);
fclose(wf);
fclose(fp_input);
fclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment