Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'CBA_fnc_removeWhitespace' #457

Merged
merged 2 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions addons/strings/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
32 changes: 32 additions & 0 deletions addons/strings/fnc_removeWhitespace.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_removeWhitespace

Description:
Removes whitespace (space, tab, newline) from string.

Parameters:
_string - Any String <STRING>
_seperate - Seperate leftovers with spaces? (optional, default: false) <BOOLEAN>

Returns:
String without whitespace <STRING>

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
2 changes: 1 addition & 1 deletion addons/strings/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ LOG("=== Testing Strings ===");
call compile preprocessFileLineNumbers format ["\x\cba\addons\strings\test_%1.sqf", _x];
} forEach TESTS;

nil;
nil
16 changes: 15 additions & 1 deletion addons/strings/test_strings.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test removal of tabs and newlines as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

nil