Add ReplaceString script function

qteditor
Sven Eberhardt 2016-07-13 02:12:31 -04:00
parent 279d89393c
commit d9f31b006b
2 changed files with 60 additions and 1 deletions

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>ReplaceString</title>
<category>Script</category>
<subcat>Strings</subcat>
<version>8.0 OC</version>
<syntax>
<rtype>string</rtype>
<params>
<param>
<type>string</type>
<name>text</name>
<desc>Source string in which the replacements occur.</desc>
</param>
<param>
<type>string</type>
<name>from</name>
<desc>Old substring to be replaced.</desc>
</param>
<param>
<type>string</type>
<name>to</name>
<desc>New substring by which the old substrings will be replaced.</desc>
</param>
</params>
</syntax>
<desc>
Returns a string in which all occurrences of a substring (from) got replaced by a different string (to).
</desc>
<examples>
<example>
<code><funclink>Log(</funclink>ReplaceString("An apple a day keeps the apple away.", "apple", "banana"));</code>
<text>Displays "An banana a day keeps the banana away."</text>
</example>
</examples>
</func>
<author>Sven2</author><date>2016-07</date>
</funcs>

View File

@ -834,6 +834,23 @@ static Nillable<C4String *> FnGetConstantNameByValue(C4PropList * _this, int val
return C4Void();
}
static Nillable<C4String *> FnReplaceString(C4PropList * _this, C4String *source, C4String *from, C4String *to)
{
if (!from) return source;
if (!source) return C4Void();
const char *szto = to ? to->GetCStr() : "";
const char *szfrom = from->GetCStr();
StdStrBuf s(source->GetData(), true);
if (s.Replace(szfrom, szto))
{
return ::Strings.RegString(s.getData());
}
else
{
return source;
}
}
static bool FnSortArray(C4PropList * _this, C4ValueArray *pArray, bool descending)
{
if (!pArray) throw C4AulExecError("SortArray: no array given");
@ -999,9 +1016,9 @@ void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
F(Trans_Rotate);
F(LocateFunc);
F(FileWrite);
F(eval);
F(GetConstantNameByValue);
F(ReplaceString);
::AddFunc(p, "Translate", C4AulExec::FnTranslate);
::AddFunc(p, "LogCallStack", C4AulExec::FnLogCallStack);