--- var.c 2002/03/06 10:33:15 1.82
+++ var.c 2002/03/06 11:45:03 1.83
@@ -755,7 +755,7 @@
tokenbuf_free(expanded);
return VAR_ERR_UNKNOWN_QUOTED_PAIR_IN_REPLACE;
}
- i = *p - '0';
+ i = (*p - '0');
p++;
if (pmatch[i].rm_so == -1) {
tokenbuf_free(expanded);
@@ -842,6 +842,7 @@
tokenbuf_t *replace,
tokenbuf_t *flags)
{
+ tokenbuf_t tmp;
const char *p;
int case_insensitive = 0;
int global = 0;
@@ -868,25 +869,26 @@
}
if (no_regex) {
- tokenbuf_t tmp;
+ /* plain text pattern based operation */
tokenbuf_init(&tmp);
-
for (p = data->begin; p != data->end;) {
if (case_insensitive)
rc = strncasecmp(p, search->begin, search->end - search->begin);
else
rc = strncmp(p, search->begin, search->end - search->begin);
if (rc != 0) {
- /* no match, copy character */
+ /* not matched, copy character */
if (!tokenbuf_append(&tmp, p, 1)) {
tokenbuf_free(&tmp);
return VAR_ERR_OUT_OF_MEMORY;
}
- ++p;
+ p++;
} else {
- tokenbuf_append(&tmp, replace->begin, replace->end - replace->begin);
- p += search->end - search->begin;
+ /* matched, copy replacement string */
+ tokenbuf_merge(&tmp, replace);
+ p += (search->end - search->begin);
if (!global) {
+ /* append remaining text */
if (!tokenbuf_append(&tmp, p, data->end - p)) {
tokenbuf_free(&tmp);
return VAR_ERR_OUT_OF_MEMORY;
@@ -895,19 +897,17 @@
}
}
}
-
tokenbuf_free(data);
tokenbuf_move(&tmp, data);
} else {
- tokenbuf_t tmp;
+ /* regular expression pattern based operation */
tokenbuf_t mydata;
tokenbuf_t myreplace;
regex_t preg;
regmatch_t pmatch[10];
int regexec_flag;
- /* Copy the pattern and the data to our own buffer to make
- sure they're terminated with a null byte. */
+ /* copy pattern and data to own buffer to make sure they are NUL-terminated */
if (!tokenbuf_assign(&tmp, search->begin, search->end - search->begin))
return VAR_ERR_OUT_OF_MEMORY;
if (!tokenbuf_assign(&mydata, data->begin, data->end - data->begin)) {
@@ -915,7 +915,7 @@
return VAR_ERR_OUT_OF_MEMORY;
}
- /* Compile the pattern. */
+ /* compile the pattern. */
rc = regcomp(&preg, tmp.begin, REG_NEWLINE|REG_EXTENDED|((case_insensitive)?REG_ICASE:0));
tokenbuf_free(&tmp);
if (rc != 0) {
@@ -923,7 +923,7 @@
return VAR_ERR_INVALID_REGEX_IN_REPLACE;
}
- /* Match the pattern and create the result string in the tmp buffer. */
+ /* match the pattern and create the result string in the tmp buffer */
tokenbuf_append(&tmp, "", 0);
for (p = mydata.begin; p != mydata.end; ) {
if (p == mydata.begin || p[-1] == '\n')
@@ -935,6 +935,7 @@
tokenbuf_append(&tmp, p, mydata.end - p);
break;
} else {
+ /* create replace string */
rc = parse_regex_replace(var, ctx, p, replace, pmatch, &myreplace);
if (rc != VAR_OK) {
regfree(&preg);
@@ -942,29 +943,35 @@
tokenbuf_free(&mydata);
return rc;
}
- if (!tokenbuf_append(&tmp, p, pmatch[0].rm_so) ||
- !tokenbuf_append(&tmp, myreplace.begin, myreplace.end - myreplace.begin)) {
+ /* append prolog string */
+ if (!tokenbuf_append(&tmp, p, pmatch[0].rm_so)) {
regfree(&preg);
tokenbuf_free(&tmp);
tokenbuf_free(&mydata);
tokenbuf_free(&myreplace);
return VAR_ERR_OUT_OF_MEMORY;
- } else {
- p += pmatch[0].rm_eo;
- if (pmatch[0].rm_eo - pmatch[0].rm_so == 0)
- {
- if (!tokenbuf_append(&tmp, p, 1))
- {
- regfree(&preg);
- tokenbuf_free(&tmp);
- tokenbuf_free(&mydata);
- tokenbuf_free(&myreplace);
- return VAR_ERR_OUT_OF_MEMORY;
- }
- ++p;
- }
+ }
+ /* append replace string */
+ if (!tokenbuf_append(&tmp, myreplace.begin, myreplace.end - myreplace.begin)) {
+ regfree(&preg);
+ tokenbuf_free(&tmp);
+ tokenbuf_free(&mydata);
tokenbuf_free(&myreplace);
+ return VAR_ERR_OUT_OF_MEMORY;
+ }
+ p += pmatch[0].rm_eo;
+ /* XXX??? */
+ if ((pmatch[0].rm_eo - pmatch[0].rm_so) == 0) {
+ if (!tokenbuf_append(&tmp, p, 1)) {
+ regfree(&preg);
+ tokenbuf_free(&tmp);
+ tokenbuf_free(&mydata);
+ tokenbuf_free(&myreplace);
+ return VAR_ERR_OUT_OF_MEMORY;
+ }
+ p++;
}
+ tokenbuf_free(&myreplace);
if (!global) {
if (!tokenbuf_append(&tmp, p, mydata.end - p)) {
regfree(&preg);
|