Skip to content

Commit

Permalink
Fix split having extra separator at end
Browse files Browse the repository at this point in the history
Fix #210
Also use `select [_index]` when selecting entire right side of a string.
  • Loading branch information
PabstMirror committed Dec 3, 2015
1 parent fcc90c0 commit 614b3bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions addons/strings/fnc_split.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ if (_input == _separator) exitWith {["",""]};

_lastWasSeperator = true;
while {_index < _inputCount} do {
_find = (_input select [_index, (_inputCount - _index)]) find _separator;
_find = (_input select [_index]) find _separator;
if (_find == 0) then {
_index = _index + _separatorCount;
if (_lastWasSeperator) then {_split pushBack "";};
_lastWasSeperator = true;
} else {
_lastWasSeperator = false;
if (_find == -1) then {
_split pushBack (_input select [_index, (_inputCount - _index)]);
_split pushBack (_input select [_index]);
_index = _inputCount;
} else {
_split pushBack (_input select [_index, _find]);
Expand All @@ -62,7 +62,7 @@ while {_index < _inputCount} do {
};
};
//Handle split at end:
if ((_inputCount >= _separatorCount) && {(_input select [(_inputCount - _separatorCount), _separatorCount]) isEqualTo _separator}) then {
if ((_inputCount >= _separatorCount) && _lastWasSeperator && {(_input select [(_inputCount - _separatorCount)]) isEqualTo _separator}) then {
_split pushBack "";
};
_split
Expand Down
8 changes: 8 additions & 0 deletions addons/strings/test_strings.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ _array = ["this\is\a\path\to\fnc_test.sqf","\fnc_"] call CBA_fnc_split;
_expected = ["this\is\a\path\to", "test.sqf"];
TEST_OP(str _array, ==, str _expected, _fn);

_array = ["babab", "bab"] call CBA_fnc_split;
_expected = ["", "ab"];
TEST_OP(str _array, ==, str _expected, _fn);

_array = ["BbabTabAbabbabababab", "bab"] call CBA_fnc_split;
_expected = ["B","TabA","","a","ab"];
TEST_OP(str _array, ==, str _expected, _fn);

// ----------------------------------------------------------------------------
// UNIT TESTS (stringReplace)
_fn = "CBA_fnc_replace";
Expand Down

0 comments on commit 614b3bf

Please sign in to comment.