From e6bf54b1d96ce6642e5b7bab839a98c6fa29634b Mon Sep 17 00:00:00 2001 From: commy2 Date: Tue, 9 Aug 2016 01:27:36 +0200 Subject: [PATCH 1/2] add 'CBA_fnc_removeWhitespace' --- addons/strings/CfgFunctions.hpp | 6 +++++ addons/strings/fnc_removeWhitespace.sqf | 32 +++++++++++++++++++++++++ addons/strings/test.sqf | 2 +- addons/strings/test_strings.sqf | 16 ++++++++++++- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 addons/strings/fnc_removeWhitespace.sqf 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..1f3ec15b1 100644 --- a/addons/strings/test_strings.sqf +++ b/addons/strings/test_strings.sqf @@ -299,4 +299,18 @@ _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); + +nil From 30eb1f5a741c380ac057bc77d5a29c2ce744790d Mon Sep 17 00:00:00 2001 From: commy2 Date: Tue, 9 Aug 2016 18:51:21 +0200 Subject: [PATCH 2/2] add test for newline and tabs --- addons/strings/test_strings.sqf | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/addons/strings/test_strings.sqf b/addons/strings/test_strings.sqf index 1f3ec15b1..269b67bba 100644 --- a/addons/strings/test_strings.sqf +++ b/addons/strings/test_strings.sqf @@ -313,4 +313,9 @@ _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