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
Changes from 1 commit
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
33 changes: 10 additions & 23 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", "", [""]]];
private ["_offset", "_index"];
if (_find == "" || {_replace find _find != -1}) exitWith {_string};
Copy link
Contributor

@commy2 commy2 May 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed. ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for infinite loops when you would do something like:

["Hello", "e", "ee"] call CBA_fnc_replace;


params ["_string","_pattern","_replacement"];
private["_i", "_cp", "_findIndex", "_stringArray", "_replaceArray", "_returnArray"];
_offset = count (_find splitString "");
Copy link
Contributor

@thojkooi thojkooi May 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this not be simply count _find?

Also, can switch this to private _offset = count _find;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count STRING works. Except if this is to circumvent those unsupported characters.

Copy link
Contributor

@commy2 commy2 May 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of _offset is, to find ASCII (?) characters that all string commands have problems with.

"o" splitString ""
-> ["o"]

"ö" splitString ""
-> ["�","�"]


_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 {
_index = _string find _find;
_string = (_string select [0, _index]) + _replace + (_string select [_index + _offset]);
};
_returnArray append _stringArray;
toString _returnArray

_string