29 static char *buf =
NULp;
30 static size_t bufsize = 0;
35 freeset(buf, ARB_alloc<char>(bufsize));
41 GBS_regex *comreg = ARB_alloc<GBS_regex>(1);
42 int cflags = REG_EXTENDED|(case_flag ==
GB_IGNORE_CASE ? REG_ICASE : 0)|REG_NEWLINE;
43 int errcode = regcomp(&comreg->
compiled, regexpr, cflags);
46 size_t size = regerror(errcode, &comreg->
compiled,
NULp, 0);
49 regerror(errcode, &comreg->
compiled, buf, size);
82 const char *end = strchr(regexpr_in_slashes, 0);
84 if (end >= (regexpr_in_slashes+3)) {
90 if (regexpr_in_slashes[0] ==
'/' && end[-1] ==
'/') {
93 static char *result_buffer =
NULp;
94 static size_t max_len = 0;
96 size_t len = end-regexpr_in_slashes-2;
101 freeset(result_buffer, ARB_alloc<char>(max_len+1));
104 memcpy(result_buffer, regexpr_in_slashes+1, len);
105 result_buffer[len] = 0;
107 result = result_buffer;
112 *error =
GBS_global_string(
"Regular expression format is '/expr/' or '/expr/i', not '%s'",
127 int res = regexec(&comreg->
compiled, str, 1, &match, 0);
128 const char *matchpos =
NULp;
131 matchpos = str+match.rm_so;
132 if (matchlen) *matchlen = match.rm_eo-match.rm_so;
157 const char *firstMatch =
NULp;
161 if (unwrapped_expr) {
203 if (unwrapped_expr) {
204 const char *sep = unwrapped_expr;
206 sep = strchr(sep,
'/');
208 if (sep>unwrapped_expr && sep[-1] !=
'\\')
break;
214 *error =
"Missing '/' between search and replace string";
221 const bool exprMatchesBorderOfLine = regexpr[0] ==
'^' || sep[-1] ==
'$';
228 regmatch_t match[10];
229 int res = regexec(&comreg->
compiled, str, 10, match, eflags);
231 if (res == REG_NOMATCH) {
235 else if (match[0].rm_so == match[0].rm_eo && !exprMatchesBorderOfLine) {
236 *error =
GBS_global_string(
"The regular expression '%s' matched an empty string (not allowed for replace)", regexpr);
243 out.
ncat(str, match[0].rm_so);
245 for (p = 0; (c = replexpr[p]); ++p) {
249 if (c >=
'0' && c <=
'9') {
250 regoff_t
start = match[c-
'0'].rm_so;
251 out.
ncat(str+start, match[c-
'0'].rm_eo-start);
255 case 'n': c =
'\n';
break;
256 case 't': c =
'\t';
break;
267 str = str+match[0].rm_eo;
268 eflags = REG_NOTBOL|REG_NOTEOL;
#define implicated(hypothesis, conclusion)
GBS_regex * GBS_compile_regexpr(const char *regexpr, GB_CASE case_flag, GB_ERROR *error)
const char * GBS_unwrap_regexpr(const char *regexpr_in_slashes, GB_CASE *case_flag, GB_ERROR *error)
const char * GBS_global_string(const char *templat,...)
char * GBS_regreplace(const char *str, const char *regReplExpr, GB_ERROR *error)
void cat(const char *from)
char * ARB_strpartdup(const char *start, const char *end)
const char * GBS_regmatch(const char *str, const char *regExpr, size_t *matchlen, GB_ERROR *error)
static HelixNrInfo * start
static void error(const char *msg)
const char * GBS_regmatch_compiled(const char *str, GBS_regex *comreg, size_t *matchlen)
void ncat(const char *from, size_t count)
char * give_buffer(size_t size)
void GBS_free_regexpr(GBS_regex *toFree)