#pragma once
#include <cstdio>
#include <string>
#include <cstring>
#include <regex>
#include <exception>
#include <pcre.h>
using namespace std;
class RegExp {
private:
pcre *re = NULL;
unsigned int max_matches = 0;
const char *pcre_error;
int pcre_error_offset;
int pcre_last_offset = 0;
int pcre_last_matches = 0;
const char *pcre_last_string = NULL;
unsigned int pcre_flags = 0;
unsigned int string_length;
int *result_vector = NULL;
RegExp *parseOptions(const char *options);
public:
typedef struct Match {
int start;
int end;
} _Match;
typedef std::function<string(RegExp *)> ReplaceCallback;
RegExp(const char *pattern, const char *options = NULL, unsigned int max_matches = 3);
RegExp *setPattern(const char *pattern, const char *options = NULL, unsigned int max_matches = 3);
int exec(const char *str, unsigned int len, int offset = -1);
int exec(string &str, int offset = -1);
int replace(string &str, string &out, const char *replace_str, unsigned int replace_length, ReplaceCallback callback, unsigned int limit = 0);
int replace(string &str, string &out, const char *replace_str, unsigned int replace_length, unsigned int limit = 0) {
return replace(str, out, replace_str, replace_length, NULL, limit);
}
inline int replace(string &str, string &out, string &replace_str, unsigned int limit = 0) {
return replace(str, out, replace_str.c_str(), replace_str.length(), limit);
}
inline int replace(string &str, string &out, const char *replace_str, unsigned int limit = 0) {
return replace(str, out, replace_str, strlen(replace_str), limit);
}
inline int replace(string &str, string &out, ReplaceCallback callback, unsigned int limit = 0) {
return replace(str, out, NULL, 0, callback, limit);
}
//
inline int replace(string &str, string &replace_str, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, replace_str, limit);
}
inline int replace(string &str, const char *replace_str, unsigned int replace_length, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, replace_str, replace_length, limit = 0);
}
inline int replace(string &str, const char *replace_str, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, replace_str, strlen(replace_str), limit);
}
inline int replace(string &str, ReplaceCallback callback, unsigned int limit = 0) {
string tmp = str;
return replace(tmp, str, NULL, 0, callback, limit);
}
//
inline string creplace(string &str, const char *replace_str, unsigned int replace_length, ReplaceCallback callback, unsigned int limit = 0, unsigned int *replaced = NULL) {
string tmp = str;
int c = replace(str, tmp, replace_str, replace_length, callback, limit);
if (replaced)
*replaced = c;
return tmp;
}
inline string creplace(string &str, const char *replace_str, unsigned int replace_length, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, replace_str, replace_length, NULL, limit, replaced);
}
inline string creplace(string &str, const char *replace_str, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, replace_str, strlen(replace_str), NULL, limit, replaced);
}
inline string creplace(string &str, string &replace_str, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, replace_str.c_str(), replace_str.length(), NULL, limit, replaced);
}
inline string creplace(string &str, ReplaceCallback callback, unsigned int limit = 0, unsigned int *replaced = NULL) {
return creplace(str, NULL, 0, callback, limit, replaced);
}
inline int matched() { return pcre_last_matches; }
const string operator[](int n);
const string getString(int n, const char *ptr = NULL);
const string getString(int n, string &str);
const Match getMatch(int n);
RegExp *freePattern();
~RegExp();
};