Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace replace #331

Merged
merged 3 commits into from
May 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions addons/strings/fnc_replace.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,22 @@ Example:
(end)

Author:
jaynus
BaerMitUmlaut
--------------------------------------------------------------------------- */

#include "script_component.hpp"

SCRIPT(replace);

// ----------------------------------------------------------------------------
params [["_string", "", [""]], ["_find", "", [""]], ["_replace", "", [""]]];
if (_find == "") exitWith {_string}; // "1" find "" -> 0
private _result = "";
private _offset = count (_find splitString "");

params ["_string","_pattern","_replacement"];
private["_i", "_cp", "_findIndex", "_stringArray", "_replaceArray", "_returnArray"];

_returnArray = [];
_cp = count _pattern;
_stringArray = toArray _string;
_replaceArray = toArray _replacement;

_findIndex = _string find _pattern;
while { _findIndex != -1 } do {
_i = 0;
while { _i < _findIndex } do {
_returnArray pushBack (_stringArray select _i);
_i = _i + 1;
};
_returnArray append _replaceArray;
_stringArray deleteRange [0, _i + _cp];

_string = toString _stringArray;
_findIndex = _string find _pattern;
while {_string find _find != -1} do {
private _index = _string find _find;
_result = _result + (_string select [0, _index]) + _replace;
_string = _string select [_index + _offset];
};
_returnArray append _stringArray;
toString _returnArray

_result + _string
6 changes: 6 additions & 0 deletions addons/strings/test_strings.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ TEST_OP(_str,==,"frag",_fn);
_str = ["frodo", "o", "ai"] call CBA_fnc_replace;
TEST_OP(_str,==,"fraidai",_fn);

_str = ["Test", "e", "ea"] call CBA_fnc_replace;
TEST_OP(_str,==,"Teast",_fn);

_str = ["Mörser", "ö", "oe"] call CBA_fnc_replace;
TEST_OP(_str,==,"Moerser",_fn);

// ----------------------------------------------------------------------------
// UNIT TESTS (leftTrim)
_fn = "CBA_fnc_leftTrim";
Expand Down