forked from CBATeam/CBA_A3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfnc_split.sqf
73 lines (57 loc) · 2.16 KB
/
fnc_split.sqf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ----------------------------------------------------------------------------
Function: CBA_fnc_split
Description:
Splits a string into substrings using a separator. Inverse of <CBA_fnc_join>.
Parameters:
_string - String to split up [String]
_separator - String to split around. If an empty string, "", then split
every character into a separate string [String, defaults to ""]
Returns:
The split string [Array of Strings]
Examples:
(begin example)
_result = ["FISH\Cheese\frog.sqf", "\"] call CBA_fnc_split;
_result is ["Fish", "Cheese", "frog.sqf"]
_result = ["Peas", ""] call CBA_fnc_split;
_result is ["P", "e", "a", "s"]
(end)
Author:
PabstMirror
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(split);
// ----------------------------------------------------------------------------
private ["_split", "_index", "_inputCount", "_separatorCount", "_find", "_lastWasSeperator"];
params [["_input", ""], ["_separator", ""]];
_split = [];
_index = 0;
_inputCount = count _input;
_separatorCount = count _separator;
// Corner cases
if (_separatorCount == 0 && _inputCount == 0) exitWith {[]};
if (_separatorCount == 0) exitWith {_input splitString ""};
if (_inputCount > 0 && _separatorCount > _inputCount) exitWith {[_input]};
if (_input == _separator) exitWith {["",""]};
_lastWasSeperator = true;
while {_index < _inputCount} do {
_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]);
_index = _inputCount;
} else {
_split pushBack (_input select [_index, _find]);
_index = _index + _find;
};
};
};
//Handle split at end:
if ((_inputCount >= _separatorCount) && _lastWasSeperator && {(_input select [(_inputCount - _separatorCount)]) isEqualTo _separator}) then {
_split pushBack "";
};
_split