Add helper function CFCUtil_global_replace
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/de0cfa29 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/de0cfa29 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/de0cfa29 Branch: refs/heads/markdown_v2 Commit: de0cfa29a2a2e18d6fe40567f0ab140f85f2a302 Parents: 0a81160 Author: Nick Wellnhofer <[email protected]> Authored: Mon Dec 1 22:18:12 2014 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Tue Dec 2 18:31:15 2014 +0100 ---------------------------------------------------------------------- compiler/src/CFCPerlPod.c | 49 ++++-------------------------------------- compiler/src/CFCUtil.c | 41 +++++++++++++++++++++++++++++++++++ compiler/src/CFCUtil.h | 6 ++++++ 3 files changed, 51 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/de0cfa29/compiler/src/CFCPerlPod.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerlPod.c b/compiler/src/CFCPerlPod.c index 78c804f..329142b 100644 --- a/compiler/src/CFCPerlPod.c +++ b/compiler/src/CFCPerlPod.c @@ -222,47 +222,6 @@ CFCPerlPod_constructors_pod(CFCPerlPod *self, CFCClass *klass) { return pod; } -static char* -S_global_replace(const char *string, const char *match, - const char *replacement) { - char *found = (char*)string; - int string_len = (int)strlen(string); - int match_len = (int)strlen(match); - int replacement_len = (int)strlen(replacement); - int len_diff = replacement_len - match_len; - - // Allocate space. - unsigned count = 0; - while (NULL != (found = strstr(found, match))) { - count++; - found += match_len; - } - int size = string_len + count * len_diff + 1; - char *modified = (char*)MALLOCATE(size); - modified[size - 1] = 0; // NULL-terminate. - - // Iterate through all matches. - found = (char*)string; - char *target = modified; - size_t last_end = 0; - if (count) { - while (NULL != (found = strstr(found, match))) { - size_t pos = found - string; - size_t unchanged_len = pos - last_end; - found += match_len; - memcpy(target, string + last_end, unchanged_len); - target += unchanged_len; - last_end = pos + match_len; - memcpy(target, replacement, replacement_len); - target += replacement_len; - } - } - size_t remaining = string_len - last_end; - memcpy(target, string + string_len - remaining, remaining); - - return modified; -} - char* CFCPerlPod_gen_subroutine_pod(CFCPerlPod *self, CFCFunction *func, const char *alias, CFCClass *klass, @@ -351,10 +310,10 @@ CFCPerlPod_perlify_doc_text(CFCPerlPod *self, const char *source) { // Change <code>foo</code> to C<< foo >>. char *copy = CFCUtil_strdup(source); char *orig = copy; - copy = S_global_replace(orig, "<code>", "C<< "); + copy = CFCUtil_global_replace(orig, "<code>", "C<< "); FREEMEM(orig); orig = copy; - copy = S_global_replace(orig, "</code>", " >>"); + copy = CFCUtil_global_replace(orig, "</code>", " >>"); FREEMEM(orig); // Lowercase all method names: Open_In() => open_in() @@ -378,12 +337,12 @@ CFCPerlPod_perlify_doc_text(CFCPerlPod *self, const char *source) { // Change all instances of NULL to 'undef' orig = copy; - copy = S_global_replace(orig, "NULL", "undef"); + copy = CFCUtil_global_replace(orig, "NULL", "undef"); FREEMEM(orig); // Change "Err_error" to "Clownfish->error". orig = copy; - copy = S_global_replace(orig, "Err_error", "Clownfish->error"); + copy = CFCUtil_global_replace(orig, "Err_error", "Clownfish->error"); FREEMEM(orig); return copy; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/de0cfa29/compiler/src/CFCUtil.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.c b/compiler/src/CFCUtil.c index e3f05d3..6e99858 100644 --- a/compiler/src/CFCUtil.c +++ b/compiler/src/CFCUtil.c @@ -145,6 +145,47 @@ CFCUtil_trim_whitespace(char *text) { } char* +CFCUtil_global_replace(const char *string, const char *match, + const char *replacement) { + char *found = (char*)string; + int string_len = (int)strlen(string); + int match_len = (int)strlen(match); + int replacement_len = (int)strlen(replacement); + int len_diff = replacement_len - match_len; + + // Allocate space. + unsigned count = 0; + while (NULL != (found = strstr(found, match))) { + count++; + found += match_len; + } + int size = string_len + count * len_diff + 1; + char *modified = (char*)MALLOCATE(size); + modified[size - 1] = 0; // NULL-terminate. + + // Iterate through all matches. + found = (char*)string; + char *target = modified; + size_t last_end = 0; + if (count) { + while (NULL != (found = strstr(found, match))) { + size_t pos = found - string; + size_t unchanged_len = pos - last_end; + found += match_len; + memcpy(target, string + last_end, unchanged_len); + target += unchanged_len; + last_end = pos + match_len; + memcpy(target, replacement, replacement_len); + target += replacement_len; + } + } + size_t remaining = string_len - last_end; + memcpy(target, string + string_len - remaining, remaining); + + return modified; +} + +char* CFCUtil_enclose_lines(const char *text, const char *line_prefix, const char *line_postfix, const char *prefix, const char *postfix) { http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/de0cfa29/compiler/src/CFCUtil.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.h b/compiler/src/CFCUtil.h index 6540672..5c528d9 100644 --- a/compiler/src/CFCUtil.h +++ b/compiler/src/CFCUtil.h @@ -69,6 +69,12 @@ CFCUtil_cat(char *string, ...); void CFCUtil_trim_whitespace(char *text); +/** Replace all occurrences of `match` in `string` with `replacement`. + */ +char* +CFCUtil_global_replace(const char *string, const char *match, + const char *replacement); + /** Enclose every line in text with line_prefix and line_postfix and the * whole text with prefix and postfix. */
