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

Strings - Cleanup #1592

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 30 additions & 12 deletions addons/strings/fnc_capitalize.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Description:
Upper case the first letter of the string, lower case the rest.

Parameters:
_string - String to capitalize [String]
_string - String to capitalize <STRING>

Returns:
Capitalized string [String].
Capitalized string <STRING>

Examples:
(begin example)
Expand All @@ -25,16 +25,34 @@ Author:
---------------------------------------------------------------------------- */
SCRIPT(capitalize);

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

private _charCount = count _string;
if (_charCount > 0) then {
// Take first Char and Upper case
private _string1 = (toUpper _string) select [0, 1];
// Take rest and lower it
private _string2 = (toLower _string) select [1];
// Compile String
_string = _string1 + _string2;
if (_string isEqualTo "") exitWith {""};

// Detect if and how unicode support was forced
private _unicode = count "д" == 1;
private _forceUnicode = count "д" == 1;

// Force unicode support
forceUnicode 0;
johnb432 marked this conversation as resolved.
Show resolved Hide resolved

// Take first character and convert to upper case
private _string1 = toUpper (_string select [0, 1]);

// Take rest and convert to lower case
private _string2 = toLower (_string select [1]);

// Compile string
_string = _string1 + _string2;

// Revert unicode support if necessary
switch (true) do {
// Force unicode (already has been, so don't do it again)
case (_forceUnicode): {};
// Unicode flag is reset right after any of the supported commands executed or the end of script, whichever comes earlier
case (_unicode): {forceUnicode 1};
// Reset unicode flag
default {forceUnicode -1};
};

_string
_string // return
21 changes: 11 additions & 10 deletions addons/strings/fnc_decodeURL.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Parameters:
_string - URL encoded text <STRING>

Returns:
_return - Human readable text <STRING>
Human readable text <STRING>

Examples:
(begin example)
Expand All @@ -19,28 +19,29 @@ Examples:
Author:
commy2
---------------------------------------------------------------------------- */
SCRIPT(decodeURL);

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

if (_string isEqualTo "") exitWith {""};

private _cache = missionNamespace getVariable [QGVAR(URLCache), objNull];
private _return = _cache getVariable _string;
if (isNil QGVAR(URLCache)) then {
GVAR(URLCache) = createHashMap;
};

private _return = GVAR(URLCache) get _string;

if (isNil "_return") then {
_return = _string;

// Only replace if there is at least one character to replace
if ("%" in _return) then {
{
_return = ([_return] + _x) call CBA_fnc_replace;
} forEach UTF8_TABLE;
};
if (isNull _cache) then {
_cache = [] call CBA_fnc_createNamespace;
missionNamespace setVariable [QGVAR(URLCache), _cache];
};

_cache setVariable [_string, _return];
GVAR(URLCache) set [_string, _return];
};

_return
_return // return
26 changes: 6 additions & 20 deletions addons/strings/fnc_find.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ Function: CBA_fnc_find

Description:
Finds a string within another string.
Reliably supports strings with ANSI characters only.

Parameters:
_haystack - String in which to search [String or ASCII char array]
_needle - String to search for [String or ASCII char array]
_haystack - String in which to search <STRING>
_needle - String to search for <STRING>
_initialIndex - Initial character index within _haystack to start the
search at [Number: 0+, defaults to 0].
search at, should be >= 0 (optional, default: 0) <SCALAR>

Returns:
First position of string. Returns -1 if not found [Number]
First position of string. Returns -1 if not found <SCALAR>

Examples:
(begin example)
Expand All @@ -36,19 +37,4 @@ params ["_haystack", "_needle", ["_initialIndex", 0]];
if !(_haystack isEqualType "") exitWith {-1};
if !(_needle isEqualType "") exitWith {-1};

private _return = -1;

if (_initialIndex < 1) then {
_return = _haystack find _needle;
} else {
if (_initialIndex > count _haystack) exitWith {};

private _tempString = [_haystack, _initialIndex] call CBA_fnc_substr;
_return = _tempString find _needle;

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

_return
_haystack find [_needle, _initialIndex max 0] // return
9 changes: 3 additions & 6 deletions addons/strings/fnc_floatToString.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ Description:
This function is as barebones as possible. Inline macro version of this
function can be used with FLOAT_TO_STRING(num).

Limitations:


Parameters:
_number - Number to format [Number]
_number - Number to format <SCALAR>

Returns:
The number formatted into a string.
The number formatted into a string <STRING>

Examples:
(begin example)
Expand All @@ -29,4 +26,4 @@ Author:
Nou
---------------------------------------------------------------------------- */

if (_this == 0) then {"0"} else {str parseNumber (str (_this % _this) + str floor abs _this) + "." + (str (abs _this - floor abs _this) select [2]) + "0"};
if (_this == 0) then {"0"} else {str parseNumber (str (_this % _this) + str floor abs _this) + "." + (str (abs _this - floor abs _this) select [2]) + "0"}
36 changes: 18 additions & 18 deletions addons/strings/fnc_formatElapsedTime.sqf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define DEBUG_MODE_NORMAL
#include "script_component.hpp"
/* -----------------------------------------------------------------------------
Function: CBA_fnc_formatElapsedTime

Expand All @@ -7,62 +9,60 @@ Description:
Intended to show time elapsed, rather than time-of-day.

Parameters:
_seconds - Number of seconds to format, for example from 'time' command [number]
_format - Format to put time into [String: "H:MM:SS", "M:SS",
"H:MM:SS.mmm" or "M:SS.mmm"; defaults to "H:MM:SS"]
_seconds - Number of seconds to format, for example from 'time' command <SCALAR>
_format - Format to put time into "H:MM:SS", "M:SS",
"H:MM:SS.mmm" or "M:SS.mmm" (optional, default: "H:MM:SS") <STRING>

Returns:
Formatted time [String]
Formatted time <STRING>

Author:
Spooner
---------------------------------------------------------------------------- */

#define DEBUG_MODE_NORMAL
#include "script_component.hpp"

SCRIPT(formatElapsedTime);

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

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

// Discover all the digits to use.
// Discover all the digits to use
private _hours = floor (_seconds / 3600);
_seconds = _seconds - (_hours * 3600);
private _minutes = floor (_seconds / 60);
_seconds = _seconds - (_minutes * 60);

// Add the milliseconds if required.
// Add the milliseconds if required
_elapsed = switch (_format) do {
case "H:MM:SS": {
format ["%1:%2:%3",
_hours,
[_minutes, 2] call CBA_fnc_formatNumber,
[floor _seconds, 2] call CBA_fnc_formatNumber];
[floor _seconds, 2] call CBA_fnc_formatNumber
]
};
case "M:SS": {
format ["%1:%2",
_minutes,
[floor _seconds, 2] call CBA_fnc_formatNumber];
[floor _seconds, 2] call CBA_fnc_formatNumber
]
};
case "H:MM:SS.mmm": {
format ["%1:%2:%3",
_hours,
[_minutes, 2] call CBA_fnc_formatNumber,
[_seconds, 2, 3] call CBA_fnc_formatNumber];
[_seconds, 2, 3] call CBA_fnc_formatNumber
]
};
case "M:SS.mmm": {
format ["%1:%2",
_minutes,
[_seconds, 2, 3] call CBA_fnc_formatNumber];
[_seconds, 2, 3] call CBA_fnc_formatNumber
]
};
default {
private "_msg";
_msg = format ["%1: %2", _msg, _format];
ERROR(_msg);
_msg;
_msg
};
};

_elapsed; // Return.
_elapsed // return
32 changes: 18 additions & 14 deletions addons/strings/fnc_formatNumber.sqf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "script_component.hpp"
/* -----------------------------------------------------------------------------
Function: CBA_fnc_formatNumber

Expand All @@ -20,16 +21,16 @@ Limitations:
output might not be as EXPECTED after about eight significant figures.

Parameters:
_number - Number to format [Number]
_number - Number to format <SCALAR>
_integerWidth - Minimum width of integer part of number, padded with 0s,
[Number: >= 0, defaults to 1]
should be >= 0 (optional, default: 1) <SCALAR>
_decimalPlaces - Number of decimal places, padded with trailing 0s,
if necessary [Number: >= 0, defaults to 0]
if necessary, should be >= 0 (optional, default: 0) <SCALAR>
_separateThousands - True to separate each three digits with a comma
[Boolean, defaults to false]
<BOOL> (default: false)

Returns:
The number formatted into a string.
The number formatted into a string <STRING>

Examples:
(begin example)
Expand All @@ -55,8 +56,6 @@ Examples:
Author:
Spooner, PabstMirror
---------------------------------------------------------------------------- */
#define DEBUG_MODE_NORMAL
#include "script_component.hpp"

#define DEFAULT_INTEGER_WIDTH 1
#define DEFAULT_DECIMAL_PLACES 0
Expand All @@ -70,25 +69,30 @@ private _isNegative = _number < 0;
private _return = (abs _number) toFixed _decimalPlaces;
private _dotIndex = if (_decimalPlaces == 0) then {count _return} else {_return find "."};


while {_integerWidth > _dotIndex} do { // pad with leading zeros
// Pad with leading zeros
while {_integerWidth > _dotIndex} do {
_return = "0" + _return;
_dotIndex = _dotIndex + 1;
};

// toFixed always adds zero left of decimal point, remove it
if ((_integerWidth == 0) && {_return select [0, 1] == "0"}) then {
_return = _return select [1]; // toFixed always adds zero left of decimal point, remove it
_return = _return select [1];
};
if (_separateThousands) then { // add localized thousands seperator "1,000"

// Add localized thousands seperator "1,000"
if (_separateThousands) then {
private _thousandsSeparator = localize "STR_CBA_FORMAT_NUMBER_THOUSANDS_SEPARATOR";

for "_index" from (_dotIndex - 3) to 1 step -3 do {
_return = (_return select [0, _index]) + _thousandsSeparator + (_return select [_index]);
_dotIndex = _dotIndex + 1;
};
};

// Re-add negative sign if there is at least one decimal place != 0.
if (_isNegative && {toArray _return arrayIntersect toArray "123456789" isNotEqualTo []}) then {
// Re-add negative sign if there is at least one decimal place != 0
if (_isNegative && {((toArray _return) arrayIntersect (toArray "123456789")) isNotEqualTo []}) then {
_return = "-" + _return;
};

_return
_return // return
21 changes: 5 additions & 16 deletions addons/strings/fnc_leftTrim.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Description:
See <CBA_fnc_rightTrim> and <CBA_fnc_trim>.

Parameters:
_string - String to trim [String]
_trim - Characters to trim [String] (default: "")
_string - String to trim <STRING>
_trim - Characters to trim (optional, default: "") <STRING>

Returns:
Trimmed string [String]
Trimmed string <STRING>

Example:
(begin example)
Expand All @@ -28,20 +28,9 @@ SCRIPT(leftTrim);

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

private _chars = toArray _string;
private _numChars = count _chars;

// Trim all whitespace characters by default
if (_trim == "") then {
_trim = WHITE_SPACE;
} else {
_trim = toArray _trim;
_trim = toString WHITE_SPACE;
};

// 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])
_string trim [_trim, 1] // return
4 changes: 3 additions & 1 deletion addons/strings/fnc_prettyFormat.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Parameters:
_array - Array to format <ARRAY>
_indents - Indentation string (optional, default: " ") <STRING>
_lineBreak - Seperator string (optional, default: endl) <STRING>
_depth - Initial indentation count (optional, default: 0) <NUMBER>
_depth - Initial indentation count (optional, default: 0) <SCALAR>

Returns:
Formatted string <STRING>
Expand Down Expand Up @@ -48,6 +48,8 @@ Author:
Terra, Dystopian, commy2

---------------------------------------------------------------------------- */
SCRIPT(prettyFormat);

params [
["_array", [], [[]]],
["_indent", " ", [""]],
Expand Down
2 changes: 1 addition & 1 deletion addons/strings/fnc_removeWhitespace.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Description:

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

Returns:
String without whitespace <STRING>
Expand Down
Loading
Loading