summaryrefslogtreecommitdiff
path: root/src/strops.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/strops.cpp')
-rw-r--r--src/strops.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/strops.cpp b/src/strops.cpp
index 116eec4..3389eb1 100644
--- a/src/strops.cpp
+++ b/src/strops.cpp
@@ -32,4 +32,29 @@ char* strops_stristr(char* haystack, char* needle)
}
} while (*haystack++);
return 0;
+}
+
+void strops_replace(char *buf, size_t buf_size, const char *search, const char *replace)
+{
+ size_t search_len = strlen(search);
+ size_t replace_len = strlen(replace);
+
+ char *r = buf; // read pointer
+ char *w = buf; // write pointer
+
+ while (*r && (w - buf) < (signed int)(buf_size - 1)) {
+ if (strncmp(r, search, search_len) == 0) {
+ // Ensure space
+ size_t remaining = buf_size - (w - buf) - 1;
+ size_t copy_len = (replace_len < remaining) ? replace_len : remaining;
+
+ memcpy(w, replace, copy_len);
+ w += copy_len;
+ r += search_len;
+ } else {
+ *w++ = *r++;
+ }
+ }
+
+ *w = '\0'; // terminate
} \ No newline at end of file