diff --git a/addons/strings/CfgFunctions.hpp b/addons/strings/CfgFunctions.hpp index e70032c7e..1a85951cd 100644 --- a/addons/strings/CfgFunctions.hpp +++ b/addons/strings/CfgFunctions.hpp @@ -57,6 +57,12 @@ class CfgFunctions description = "Trims white-space (space, tab, newline) from the left end of a string."; file = "\x\cba\addons\strings\fnc_leftTrim.sqf"; }; + // CBA_fnc_removeWhitespace + class removeWhitespace + { + description = "Removes whitespace (space, tab, newline) from string."; + file = "\x\cba\addons\strings\fnc_removeWhitespace.sqf"; + }; // CBA_fnc_replace class replace { diff --git a/addons/strings/fnc_removeWhitespace.sqf b/addons/strings/fnc_removeWhitespace.sqf new file mode 100644 index 000000000..cdb0c637c --- /dev/null +++ b/addons/strings/fnc_removeWhitespace.sqf @@ -0,0 +1,32 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_removeWhitespace + +Description: + Removes whitespace (space, tab, newline) from string. + +Parameters: + _string - Any String + _seperate - Seperate leftovers with spaces? (optional, default: false) + +Returns: + String without whitespace + +Example: + (begin example) + " foo bar " call CBA_fnc_removeWhitespace; + // "foobar" + + [" foo bar ", true] call CBA_fnc_removeWhitespace; + // "foo bar" + (end) + +Author: + commy2 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +#include "script_strings.hpp" +SCRIPT(removeWhitespace); + +params [["_string", "", [""]], ["_seperate", false, [true]]]; + +(_string splitString toString WHITE_SPACE) joinString (["", " "] select _seperate) // return diff --git a/addons/strings/test.sqf b/addons/strings/test.sqf index 448827ea5..815dd9cc5 100644 --- a/addons/strings/test.sqf +++ b/addons/strings/test.sqf @@ -17,4 +17,4 @@ LOG("=== Testing Strings ==="); call compile preprocessFileLineNumbers format ["\x\cba\addons\strings\test_%1.sqf", _x]; } forEach TESTS; -nil; +nil diff --git a/addons/strings/test_strings.sqf b/addons/strings/test_strings.sqf index 801423322..269b67bba 100644 --- a/addons/strings/test_strings.sqf +++ b/addons/strings/test_strings.sqf @@ -299,4 +299,23 @@ _result = ["aardwolf", "aardvark"] call CBA_fnc_compare; TEST_OP(_result, ==, +1, _fn); */ -nil; +// ---------------------------------------------------------------------------- +// UNIT TESTS (removeWhitespace) +_fn = "CBA_fnc_removeWhitespace"; + +TEST_DEFINED("CBA_fnc_removeWhitespace",""); + +_str = " foo bar " call CBA_fnc_removeWhitespace; +_expected = "foobar"; +TEST_OP(_str,isEqualTo,_expected,_fn); + +_str = [" foo bar ", true] call CBA_fnc_removeWhitespace; +_expected = "foo bar"; +TEST_OP(_str,isEqualTo,_expected,_fn); + +_str = "tab: newline: +space: " call CBA_fnc_removeWhitespace; +_expected = "tab:newline:space:"; +TEST_OP(_str,isEqualTo,_expected,_fn); + +nil