Skip to content

Commit

Permalink
Strings cleanup (#770)
Browse files Browse the repository at this point in the history
* Cleanup

* Cleanup

* Add newline

* More newlines

* Change typeName to isEqualType
  • Loading branch information
Neviothr authored and commy2 committed Nov 18, 2017
1 parent 2405e1e commit 815dac2
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion addons/strings/fnc_capitalize.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ params ["_string"];
private _charCount = count _string;
if (_charCount > 0) then {
// Take first Char and Upper case
private _string1 = (toUpper _string) select [0,1];
private _string1 = (toUpper _string) select [0, 1];
// Take rest and lower it
private _string2 = (toLower _string) select [1];
// Compile String
Expand Down
33 changes: 14 additions & 19 deletions addons/strings/fnc_find.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,26 @@ SCRIPT(find);

// ----------------------------------------------------------------------------

params ["_haystack","_needle", ["_initialIndex",0]];
params ["_haystack", "_needle", ["_initialIndex", 0]];

private _start = -1;
private _ret = -1;
if !(_haystack isEqualType "") exitWith {-1};
if !(_needle isEqualType "") exitWith {-1};

if (typeName _haystack != "STRING") exitWith {
-1
};
if (typeName _needle != "STRING") exitWith {
-1
};
private _start = -1;
private _return = -1;

if(_initialIndex < 1) then {
_ret = _haystack find _needle;
if (_initialIndex < 1) then {
_return = _haystack find _needle;
} else {
if(_initialIndex > (count _haystack) ) exitWith {
-1
};
if (_initialIndex > (count _haystack)) exitWith {-1};
private _tempString = [_haystack, _initialIndex] call CBA_fnc_substr;
_ret = _tempString find _needle;
if(_ret > -1) then {
_ret = _ret + _initialIndex;
_return = _tempString find _needle;

if (_return > -1) then {
_return = _return + _initialIndex;
} else {
_ret = -1;
_return = -1;
};
};

_ret
_return
2 changes: 1 addition & 1 deletion addons/strings/fnc_floatToString.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Author:
Nou
---------------------------------------------------------------------------- */

str parseNumber (str (_this%_this) + str floor abs _this) + "." + (str (abs _this-floor abs _this) select [2]) + "0";
str parseNumber (str (_this % _this) + str floor abs _this) + "." + (str (abs _this - floor abs _this) select [2]) + "0";
2 changes: 1 addition & 1 deletion addons/strings/fnc_formatElapsedTime.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SCRIPT(formatElapsedTime);

// -----------------------------------------------------------------------------

params ["_seconds", ["_format","H:MM:SS"]];
params ["_seconds", ["_format", "H:MM:SS"]];

private ["_minutes", "_hours", "_secondsDecimals"];

Expand Down
2 changes: 1 addition & 1 deletion addons/strings/fnc_formatNumber.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Author:

SCRIPT(formatNumber);

params ["_number", ["_integerWidth",DEFAULT_INTEGER_WIDTH], ["_decimalPlaces",DEFAULT_DECIMAL_PLACES], ["_separateThousands",DEFAULT_SEPARATE_THOUSANDS]];
params ["_number", ["_integerWidth", DEFAULT_INTEGER_WIDTH], ["_decimalPlaces", DEFAULT_DECIMAL_PLACES], ["_separateThousands", DEFAULT_SEPARATE_THOUSANDS]];

private _isNegative = _number < 0;
private _return = (abs _number) toFixed _decimalPlaces;
Expand Down
4 changes: 2 additions & 2 deletions addons/strings/fnc_leftTrim.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SCRIPT(leftTrim);

params ["_string"];

private ["_chars","_charCount"];
private ["_chars", "_charCount"];

// Convert String to Array for Find White Spaces
_chars = ToArray _string;
Expand All @@ -45,7 +45,7 @@ if (_charCount > 0) then {

// find Last White Space
for "_i" from 0 to (_charCount - 1) do {
if !((_chars select _i) in WHITE_SPACE) exitWith { _numWhiteSpaces = _i };
if !((_chars select _i) in WHITE_SPACE) exitWith {_numWhiteSpaces = _i};
};
// if a White space exist than they are deselected
if (_numWhiteSpaces > 0) then {
Expand Down
2 changes: 2 additions & 0 deletions addons/strings/fnc_replace.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ SCRIPT(replace);

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

private _result = "";
private _offset = count (_find splitString "");

while {_string find _find != -1} do {
private _index = _string find _find;

_result = _result + (_string select [0, _index]) + _replace;
_string = _string select [_index + _offset];
};
Expand Down
9 changes: 6 additions & 3 deletions addons/strings/fnc_rightTrim.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ SCRIPT(rightTrim);

params ["_string"];

private ["_char","_charCount", "_charCount2", "_pos","_numWhiteSpaces"];
private ["_char", "_charCount", "_charCount2", "_pos", "_numWhiteSpaces"];
// Convert String to Array for Find White Spaces
_char = toArray _string;

// Count String Lenth
_charCount = count _string;

// substract 1 for faster for(L46)
_charCount2 = _charCount - 1;

// find White Spaces and count than
for "_i" from _charCount2 to 0 step -1 do {
if !((_char select _i) in WHITE_SPACE) exitWith { _numWhiteSpaces = _charCount2 - _i };
if !((_char select _i) in WHITE_SPACE) exitWith {_numWhiteSpaces = _charCount2 - _i};
};

// exit if every tab is White Space
if (isNil "_numWhiteSpaces") exitWith {""};

// select Only None White Space Part
_string select [0,_charCount - _numWhiteSpaces]; // Return.
_string select [0, _charCount - _numWhiteSpaces]; // Return.
8 changes: 6 additions & 2 deletions addons/strings/fnc_split.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ SCRIPT(split);
// ----------------------------------------------------------------------------

private ["_split", "_index", "_inputCount", "_separatorCount", "_find", "_lastWasSeperator"];
params [["_input",""], ["_separator",""]];
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 ""};
Expand All @@ -46,12 +47,15 @@ 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;
Expand All @@ -65,5 +69,5 @@ while {_index < _inputCount} do {
if ((_inputCount >= _separatorCount) && _lastWasSeperator && {(_input select [(_inputCount - _separatorCount)]) isEqualTo _separator}) then {
_split pushBack "";
};
_split

_split
4 changes: 3 additions & 1 deletion addons/strings/fnc_substr.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ SCRIPT(substr);

// ----------------------------------------------------------------------------
params ["_string", "_startIndex", "_length"];

// Check if _length is set else extract string to end
if (isNil "_length" || {_length <= 0}) exitWith { _string select [_startIndex]; };
if (isNil "_length" || {_length <= 0}) exitWith {_string select [_startIndex];};

// Cut out String
_string select [_startIndex, _length]; // Return
5 changes: 4 additions & 1 deletion addons/strings/fnc_substring.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ Author:
SCRIPT(substring);

// ----------------------------------------------------------------------------
params["_string","_startIndex","_endIndex"];
params ["_string", "_startIndex", "_endIndex"];

// Check if _start is Larger than _endIndex to Prevent Issues
if (_startIndex > _endIndex) exitWith {""};

// Calculate Differenz between _start and _end for select lenth value
_endIndex = _endIndex + 1 - _startIndex;

// Cut out String
_string select [_startIndex, _endIndex]; // Return
1 change: 0 additions & 1 deletion addons/strings/script_component.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define COMPONENT strings
#include "\x\cba\addons\main\script_mod.hpp"


#ifdef DEBUG_ENABLED_STRINGS
#define DEBUG_MODE_FULL
#endif
Expand Down

0 comments on commit 815dac2

Please sign in to comment.