Commit 452fab32 authored by Geoff Simmons's avatar Geoff Simmons

factor out some common code for sub(), suball() and extract() in the

C++ implementation
parent 9b897f6b
......@@ -182,6 +182,23 @@ vre2_get_group(vre2 *vre2, const char * const name, int * const refnum)
CATCHALL
}
static inline const char *
copy_on_match(const string& result, const int match, char * const dest,
const size_t bytes, size_t * const len)
{
try {
if (!match)
return NULL;
if (result.size() + 1 > bytes)
throw runtime_error("insufficient workspace");
*len = result.size() + 1;
result.copy(dest, *len);
dest[*len] = '\0';
return NULL;
}
CATCHALL
}
const char *
vre2_replace(vre2 *vre2, const int all, const char * const text,
const char * const rewrite, char * const dest, const size_t bytes,
......@@ -193,14 +210,7 @@ vre2_replace(vre2 *vre2, const int all, const char * const text,
*match = vre2->global_replace(&t, rewrite);
else
*match = vre2->replace(&t, rewrite);
if (!*match)
return NULL;
if (t.size() + 1 > bytes)
throw runtime_error("insufficient workspace");
*len = t.size() + 1;
t.copy(dest, *len);
dest[*len] = '\0';
return NULL;
return copy_on_match(t, *match, dest, bytes, len);
}
CATCHALL
}
......@@ -213,14 +223,7 @@ vre2_extract(vre2 *vre2, const char * const text, const char * const rewrite,
try {
string out;
*match = vre2->extract(&out, text, rewrite);
if (!*match)
return NULL;
if (out.size() + 1 > bytes)
throw runtime_error("insufficient workspace");
*len = out.size() + 1;
out.copy(dest, *len);
dest[*len] = '\0';
return NULL;
return copy_on_match(out, *match, dest, bytes, len);
}
CATCHALL
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment