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 support for arbitrary character trimming 2 #889

Merged
merged 3 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 18 additions & 23 deletions addons/strings/fnc_leftTrim.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
Function: CBA_fnc_leftTrim

Description:
Trims white-space (space, tab, newline) from the left end of a string.
Trims specified characters (all whitespace by default) from the left end of a string.

See <CBA_fnc_rightTrim> and <CBA_fnc_trim>.

Parameters:
_string - String to trim [String]
_trim - Characters to trim [String] (default: "")

Returns:
Trimmed string [String]
Expand All @@ -19,7 +20,7 @@ Example:
(end)

Author:
Spooner, joko // Jonas
Spooner, joko // Jonas, SilentSpike
---------------------------------------------------------------------------- */

#include "script_component.hpp"
Expand All @@ -29,28 +30,22 @@ SCRIPT(leftTrim);

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

params ["_string"];
params ["_string", ["_trim", "", [""]]];

private ["_chars", "_charCount"];
private _chars = toArray _string;
private _numChars = count _chars;

// Convert String to Array for Find White Spaces
_chars = ToArray _string;
// count String input
_charCount = count _string;

if (_charCount > 0) then {
private "_numWhiteSpaces";
// Set Base number for White Spaces
_numWhiteSpaces = _charCount;

// find Last White Space
for "_i" from 0 to (_charCount - 1) do {
if !((_chars select _i) in WHITE_SPACE) exitWith {_numWhiteSpaces = _i};
};
// if a White space exist than they are deselected
if (_numWhiteSpaces > 0) then {
_string = _string select [_numWhiteSpaces];
};
// Trim all whitespace characters by default
if (_trim == "") then {
_trim = WHITE_SPACE;
} else {
_trim = toArray _trim;
};

_string; // Return.
// We have to process the string in array form because it could differ in length (if there are non-ASCII characters)
private _trimIndex = count _chars;
{
if !(_x in _trim) exitWith { _trimIndex = _forEachIndex; };
} forEach _chars;

toString (_chars select [_trimIndex, _numChars - _trimIndex])
40 changes: 22 additions & 18 deletions addons/strings/fnc_rightTrim.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
Function: CBA_fnc_rightTrim

Description:
Trims white-space (space, tab, newline) from the right end of a string.
Trims specified characters (all whitespace by default) from the right end of a string.

See <CBA_fnc_leftTrim> and <CBA_fnc_trim>.

Parameters:
_string - String to trim [String]
_trim - Characters to trim [String] (default: "")

Returns:
Trimmed string [String]
Expand All @@ -19,7 +20,7 @@ Example:
(end)

Author:
Spooner, joko // Jonas
Spooner, joko // Jonas, SilentSpike
---------------------------------------------------------------------------- */

#include "script_component.hpp"
Expand All @@ -29,25 +30,28 @@ SCRIPT(rightTrim);

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

params ["_string"];
params ["_string", ["_trim", "", [""]]];

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

// Count String Lenth
_charCount = count _string;
// Trim from the right
reverse _chars;

// 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};
// Trim all whitespace characters by default
if (_trim == "") then {
_trim = WHITE_SPACE;
} else {
_trim = toArray _trim;
};

// exit if every tab is White Space
if (isNil "_numWhiteSpaces") exitWith {""};
// We have to process the string in array form because it could differ in length (if there are non-ASCII characters)
private _trimIndex = count _chars;
{
if !(_x in _trim) exitWith { _trimIndex = _forEachIndex; };
} forEach _chars;

// Convert string back to original order
reverse _chars;

// select Only None White Space Part
_string select [0, _charCount - _numWhiteSpaces]; // Return.
toString (_chars select [0, _numChars - _trimIndex])
12 changes: 6 additions & 6 deletions addons/strings/fnc_trim.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
Function: CBA_fnc_trim

Description:
Trims white-space (space, tab, newline) from the both ends of a string.
Trims specified characters (all whitespace by default) from the both ends of a string.

See <CBA_fnc_leftTrim> and <CBA_fnc_rightTrim>.

Parameters:
_string - String to trim [String]
_trim - Characters to trim [String] (default: "")

Returns:
Trimmed string [String]
Expand All @@ -19,7 +20,7 @@ Example:
(end)

Author:
Spooner
Spooner, SilentSpike
---------------------------------------------------------------------------- */

#include "script_component.hpp"
Expand All @@ -28,9 +29,8 @@ SCRIPT(trim);

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

params ["_string"];
params ["_string", ["_trim", "", [""]]];

// Rtrim first for efficiency.
_string = [_string] call CBA_fnc_rightTrim;
_string = [_string, _trim] call CBA_fnc_rightTrim;

[_string] call CBA_fnc_leftTrim; // Return.
[_string, _trim] call CBA_fnc_leftTrim