diff --git a/config/docs.cfg b/config/docs.cfg
index 32d8047b..2ed225dd 100644
--- a/config/docs.cfg
+++ b/config/docs.cfg
@@ -240,11 +240,11 @@ docident [testlist] [Tests a list argument for various things.];
docargument [L] [the list to test] [] [0];
docargument [N] [the type of test to run] [min 0/max 2/default 0] [0];
docremark [See /docs/reference.xml for more information about the usage of this command.];
-docident [replacestr] [Returns a string, with a portion of it replaced with a new sub-string.];
+docident [strreplace] [Returns a string, with a portion of it replaced with a new sub-string.];
docargument [S] [the original string to modify] [] [0];
docargument [T] [the target sub-string to replace] [] [0];
docargument [N] [the new sub-string to replace the target] [] [0];
-docexample [echo (replacestr "Hello cruel world" cruel "")] [Output: Hello world];
+docexample [echo (strreplace "Hello cruel world" cruel "")] [Output: Hello world];
docsection [General]
docident [addserver] [Adds a server to the list of server to query in the server list menu.];
docargument [S] [the address of the server (hostname or IP)] [] [0];
diff --git a/docs/reference.xml b/docs/reference.xml
index 10218124..b6dc772d 100644
--- a/docs/reference.xml
+++ b/docs/reference.xml
@@ -455,7 +455,7 @@
-
+
@@ -524,7 +524,7 @@
-
+
@@ -1044,11 +1044,11 @@
-
+
Returns a string, with a portion of it replaced with a new sub-string.
-
+
Output: Hello world
diff --git a/source/src/command.cpp b/source/src/command.cpp
index 7e8d45d8..4c8d8b86 100644
--- a/source/src/command.cpp
+++ b/source/src/command.cpp
@@ -1207,17 +1207,29 @@ void testlist(char *list, char *type = "0")
}
}
-void replacestr(char *arg1, char *arg2, char *arg3)
+void strreplace (const char *source, const char *search, const char *replace)
{
- string output;
- char *ptr = strstr(arg1, arg2);
+ vector buf;
- if(!ptr) { result(arg1); return; } // Return the string untouched if arg2 cannot be found in arg1
-
- strncpy(output, arg1, ptr - arg1);
- output[ptr - arg1] = 0;
- sprintf(output + (ptr - arg1), "%s%s", arg3, ptr + strlen(arg2));
- result(output);
+ int searchlen = strlen(search);
+ if(!searchlen) { result(source); return; }
+ for(;;)
+ {
+ const char *found = strstr(source, search);
+ if(found)
+ {
+ while(source < found) buf.add(*source++);
+ for(const char *n = replace; *n; n++) buf.add(*n);
+ source = found + searchlen;
+ }
+ else
+ {
+ while(*source) buf.add(*source++);
+ buf.add('\0');
+ result(newstring(buf.getbuf(), buf.length()));
+ return;
+ }
+ }
}
COMMANDN(c, colora, ARG_1STR);
@@ -1240,7 +1252,7 @@ COMMANDN(tolower, toLower, ARG_1STR);
COMMANDN(toupper, toUpper, ARG_1STR);
COMMAND(testchar, ARG_2STR);
COMMAND(testlist, ARG_2STR);
-COMMAND(replacestr, ARG_3STR);
+COMMAND(strreplace, ARG_3STR);
int add(int a, int b) { return a+b; } COMMANDN(+, add, ARG_2EXP);
int mul(int a, int b) { return a*b; } COMMANDN(*, mul, ARG_2EXP);