replacestr => strreplace (sauer)

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6489 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lucas GAUTHERON
2011-06-03 20:05:48 +00:00
parent eea4960b10
commit de3f70ac3b
3 changed files with 28 additions and 16 deletions
+2 -2
View File
@@ -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];
+4 -4
View File
@@ -455,7 +455,7 @@
<identifierReference identifier="concatword"/>
<identifierReference identifier="strlen"/>
<identifierReference identifier="substr"/>
<identifierReference identifier="replacestr"/>
<identifierReference identifier="strreplace"/>
</references>
</command>
@@ -524,7 +524,7 @@
<references>
<identifierReference identifier="strlen"/>
<identifierReference identifier="strpos"/>
<identifierReference identifier="replacestr"/>
<identifierReference identifier="strreplace"/>
</references>
</command>
@@ -1044,11 +1044,11 @@
</references>
</command>
<command name="replacestr">
<command name="strreplace">
<description>Returns a string, with a portion of it replaced with a new sub-string.</description>
<examples>
<example>
<code><![CDATA[echo (replacestr "Hello cruel world" cruel "")]]></code>
<code><![CDATA[echo (strreplace "Hello cruel world" cruel "")]]></code>
<explanation>Output: Hello world</explanation>
</example>
</examples>
+22 -10
View File
@@ -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<char> 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);