diff --git a/addons/strings/fnc_capitalize.sqf b/addons/strings/fnc_capitalize.sqf index 82c1a1fb5..698403345 100644 --- a/addons/strings/fnc_capitalize.sqf +++ b/addons/strings/fnc_capitalize.sqf @@ -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 Returns: - Capitalized string [String]. + Capitalized string Examples: (begin example) @@ -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; + +// 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 diff --git a/addons/strings/fnc_decodeURL.sqf b/addons/strings/fnc_decodeURL.sqf index a98f91bd7..13bceb497 100644 --- a/addons/strings/fnc_decodeURL.sqf +++ b/addons/strings/fnc_decodeURL.sqf @@ -9,7 +9,7 @@ Parameters: _string - URL encoded text Returns: - _return - Human readable text + Human readable text Examples: (begin example) @@ -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 diff --git a/addons/strings/fnc_find.sqf b/addons/strings/fnc_find.sqf index fe504343f..ea0fa47bc 100644 --- a/addons/strings/fnc_find.sqf +++ b/addons/strings/fnc_find.sqf @@ -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 + _needle - String to search for _initialIndex - Initial character index within _haystack to start the - search at [Number: 0+, defaults to 0]. + search at, should be >= 0 (optional, default: 0) Returns: - First position of string. Returns -1 if not found [Number] + First position of string. Returns -1 if not found Examples: (begin example) @@ -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 diff --git a/addons/strings/fnc_floatToString.sqf b/addons/strings/fnc_floatToString.sqf index adb55f029..c637734d4 100644 --- a/addons/strings/fnc_floatToString.sqf +++ b/addons/strings/fnc_floatToString.sqf @@ -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 Returns: - The number formatted into a string. + The number formatted into a string Examples: (begin example) @@ -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"} diff --git a/addons/strings/fnc_formatElapsedTime.sqf b/addons/strings/fnc_formatElapsedTime.sqf index 2d658c84b..27b642fe2 100644 --- a/addons/strings/fnc_formatElapsedTime.sqf +++ b/addons/strings/fnc_formatElapsedTime.sqf @@ -1,3 +1,5 @@ +#define DEBUG_MODE_NORMAL +#include "script_component.hpp" /* ----------------------------------------------------------------------------- Function: CBA_fnc_formatElapsedTime @@ -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 + _format - Format to put time into "H:MM:SS", "M:SS", + "H:MM:SS.mmm" or "M:SS.mmm" (optional, default: "H:MM:SS") Returns: - Formatted time [String] + Formatted time 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 diff --git a/addons/strings/fnc_formatNumber.sqf b/addons/strings/fnc_formatNumber.sqf index ea693625e..6b644de1a 100644 --- a/addons/strings/fnc_formatNumber.sqf +++ b/addons/strings/fnc_formatNumber.sqf @@ -1,3 +1,4 @@ +#include "script_component.hpp" /* ----------------------------------------------------------------------------- Function: CBA_fnc_formatNumber @@ -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 _integerWidth - Minimum width of integer part of number, padded with 0s, - [Number: >= 0, defaults to 1] + should be >= 0 (optional, default: 1) _decimalPlaces - Number of decimal places, padded with trailing 0s, - if necessary [Number: >= 0, defaults to 0] + if necessary, should be >= 0 (optional, default: 0) _separateThousands - True to separate each three digits with a comma - [Boolean, defaults to false] + (default: false) Returns: - The number formatted into a string. + The number formatted into a string Examples: (begin example) @@ -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 @@ -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 diff --git a/addons/strings/fnc_leftTrim.sqf b/addons/strings/fnc_leftTrim.sqf index e81cf1e40..bb242a2dc 100644 --- a/addons/strings/fnc_leftTrim.sqf +++ b/addons/strings/fnc_leftTrim.sqf @@ -9,11 +9,11 @@ Description: See and . Parameters: - _string - String to trim [String] - _trim - Characters to trim [String] (default: "") + _string - String to trim + _trim - Characters to trim (optional, default: "") Returns: - Trimmed string [String] + Trimmed string Example: (begin example) @@ -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 diff --git a/addons/strings/fnc_prettyFormat.sqf b/addons/strings/fnc_prettyFormat.sqf index bfef37775..3b08e4a72 100644 --- a/addons/strings/fnc_prettyFormat.sqf +++ b/addons/strings/fnc_prettyFormat.sqf @@ -9,7 +9,7 @@ Parameters: _array - Array to format _indents - Indentation string (optional, default: " ") _lineBreak - Seperator string (optional, default: endl) - _depth - Initial indentation count (optional, default: 0) + _depth - Initial indentation count (optional, default: 0) Returns: Formatted string @@ -48,6 +48,8 @@ Author: Terra, Dystopian, commy2 ---------------------------------------------------------------------------- */ +SCRIPT(prettyFormat); + params [ ["_array", [], [[]]], ["_indent", " ", [""]], diff --git a/addons/strings/fnc_removeWhitespace.sqf b/addons/strings/fnc_removeWhitespace.sqf index c376dc47f..36120fd20 100644 --- a/addons/strings/fnc_removeWhitespace.sqf +++ b/addons/strings/fnc_removeWhitespace.sqf @@ -8,7 +8,7 @@ Description: Parameters: _string - Any String - _seperate - Seperate leftovers with spaces? (optional, default: false) + _seperate - Seperate leftovers with spaces? (optional, default: false) Returns: String without whitespace diff --git a/addons/strings/fnc_replace.sqf b/addons/strings/fnc_replace.sqf index 4e3d321f5..99be9906b 100644 --- a/addons/strings/fnc_replace.sqf +++ b/addons/strings/fnc_replace.sqf @@ -6,12 +6,12 @@ Description: Replaces substrings within a string. Case-dependent. Parameters: - _string - String to make replacement in [String] - _pattern - Substring to replace [String] - _replacement - String to replace the _pattern with [String] + _string - String to make replacement in + _pattern - Substring to replace + _replacement - String to replace the _pattern with Returns: - String with replacements made [String] + String with replacements made Example: (begin example) @@ -25,7 +25,9 @@ Author: SCRIPT(replace); params [["_string", "", [""]], ["_find", "", [""]], ["_replace", "", [""]]]; -if (_find == "") exitWith {_string}; // "1" find "" -> 0 + +// "1" find "" -> 0 +if (_find == "") exitWith {_string}; private _result = ""; private _offset = count (_find splitString ""); diff --git a/addons/strings/fnc_rightTrim.sqf b/addons/strings/fnc_rightTrim.sqf index e23559b79..b6b698c30 100644 --- a/addons/strings/fnc_rightTrim.sqf +++ b/addons/strings/fnc_rightTrim.sqf @@ -9,11 +9,11 @@ Description: See and . Parameters: - _string - String to trim [String] - _trim - Characters to trim [String] (default: "") + _string - String to trim + _trim - Characters to trim (optional, default: "") Returns: - Trimmed string [String] + Trimmed string Example: (begin example) @@ -28,26 +28,9 @@ SCRIPT(rightTrim); params ["_string", ["_trim", "", [""]]]; -private _chars = toArray _string; -private _numChars = count _chars; - -// Trim from the right -reverse _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; - -// Convert string back to original order -reverse _chars; - -toString (_chars select [0, _numChars - _trimIndex]) +_string trim [_trim, 2] // return diff --git a/addons/strings/fnc_sanitizeHTML.sqf b/addons/strings/fnc_sanitizeHTML.sqf index 8aa90d89c..cc732b130 100644 --- a/addons/strings/fnc_sanitizeHTML.sqf +++ b/addons/strings/fnc_sanitizeHTML.sqf @@ -19,6 +19,7 @@ Example: Author: commy2 --------------------------------------------------------------------------- */ +SCRIPT(sanitizeHTML); params ["_string"]; diff --git a/addons/strings/fnc_split.sqf b/addons/strings/fnc_split.sqf index 8074417f9..a3af3ec49 100644 --- a/addons/strings/fnc_split.sqf +++ b/addons/strings/fnc_split.sqf @@ -6,12 +6,12 @@ Description: Splits a string into substrings using a separator. Inverse of . Parameters: - _string - String to split up [String] + _string - String to split up _separator - String to split around. If an empty string, "", then split - every character into a separate string [String, defaults to ""] + every character into a separate string (optional, default: "") Returns: - The split string [Array of Strings] + The split string Examples: (begin example) @@ -35,19 +35,23 @@ private _inputCount = count _input; private _separatorCount = count _separator; // Corner cases -if (_separatorCount == 0 && _inputCount == 0) exitWith {[]}; +if (_separatorCount == 0 && {_inputCount == 0}) exitWith {[]}; if (_separatorCount == 0) exitWith {_input splitString ""}; -if (_inputCount > 0 && _separatorCount > _inputCount) exitWith {[_input]}; -if (_input == _separator) exitWith {["",""]}; +if (_inputCount > 0 && {_separatorCount > _inputCount}) exitWith {[_input]}; +if (_input == _separator) exitWith {["", ""]}; private _lastWasSeperator = true; + while {_index < _inputCount} do { private _find = (_input select [_index]) find _separator; if (_find == 0) then { _index = _index + _separatorCount; - if (_lastWasSeperator) then {_split pushBack "";}; + if (_lastWasSeperator) then { + _split pushBack ""; + }; + _lastWasSeperator = true; } else { _lastWasSeperator = false; @@ -61,9 +65,10 @@ while {_index < _inputCount} do { }; }; }; -//Handle split at end: + +// Handle split at end if ((_inputCount >= _separatorCount) && _lastWasSeperator && {(_input select [(_inputCount - _separatorCount)]) isEqualTo _separator}) then { _split pushBack ""; }; -_split +_split // return diff --git a/addons/strings/fnc_strLen.sqf b/addons/strings/fnc_strLen.sqf index 78c81b52b..c92dc032a 100644 --- a/addons/strings/fnc_strLen.sqf +++ b/addons/strings/fnc_strLen.sqf @@ -4,12 +4,13 @@ Function: CBA_fnc_strLen Description: Counts the number of characters in a string. + Reliably supports strings with ANSI characters only. Parameters: - _string - String to measure [String] + _string - String to measure Returns: - Number of characters in string [Number] + Number of characters in string Examples: (begin example) diff --git a/addons/strings/fnc_substr.sqf b/addons/strings/fnc_substr.sqf index 891a77040..119e14abb 100644 --- a/addons/strings/fnc_substr.sqf +++ b/addons/strings/fnc_substr.sqf @@ -4,16 +4,17 @@ Function: CBA_fnc_substr Description: Retrieves a substring of this instance. - The substring starts at a specified character position and has a specified length. + Reliably supports strings with ANSI characters only. + Parameters: - _string - String to extract from [String] - _startIndex - Index to start the substring extraction [Number] - _length - length of the extracted substring [Number](Optional) if is not set than from _startIndex to end + _string - String to extract from + _startIndex - Index to start the substring extraction + _length - Length of the extracted substring, <= 0 means whole string is selected (optional, default: 0) Returns: - String extracted [String] + Extracted string Example: (begin example) @@ -26,10 +27,12 @@ Author: --------------------------------------------------------------------------- */ SCRIPT(substr); -params ["_string", "_startIndex", "_length"]; +params ["_string", "_startIndex", ["_length", 0]]; // Check if _length is set else extract string to end -if (isNil "_length" || {_length <= 0}) exitWith {_string select [_startIndex];}; +if (_length <= 0) exitWith { + _string select [_startIndex] // return +}; -// Cut out String +// Cut out string _string select [_startIndex, _length] // return diff --git a/addons/strings/fnc_substring.sqf b/addons/strings/fnc_substring.sqf index c01039dd7..90c320a4a 100644 --- a/addons/strings/fnc_substring.sqf +++ b/addons/strings/fnc_substring.sqf @@ -4,14 +4,15 @@ Function: CBA_fnc_substring Description: Extracts the index-based substring from a string. + Reliably supports strings with ANSI characters only. Parameters: - _string - String to extract from [String] - _startIndex - Index to start the substring extraction [Number] - _endIndex - Index to end the substring extraction [Number] + _string - String to extract from + _startIndex - Index to start the substring extraction + _endIndex - Index to end the substring extraction Returns: - String extracted [String] + Extracted string Example: (begin example) @@ -26,11 +27,11 @@ SCRIPT(substring); params ["_string", "_startIndex", "_endIndex"]; -// Check if _start is Larger than _endIndex to Prevent Issues +// Check if _start is larger than _endIndex to prevent issues if (_startIndex > _endIndex) exitWith {""}; -// Calculate Differenz between _start and _end for select lenth value +// Calculate difference between _start and _end for select length value _endIndex = _endIndex + 1 - _startIndex; -// Cut out String +// Cut out string _string select [_startIndex, _endIndex] // return diff --git a/addons/strings/fnc_trim.sqf b/addons/strings/fnc_trim.sqf index 6fa74cc62..9e8cf800f 100644 --- a/addons/strings/fnc_trim.sqf +++ b/addons/strings/fnc_trim.sqf @@ -8,11 +8,11 @@ Description: See and . Parameters: - _string - String to trim [String] - _trim - Characters to trim [String] (default: "") + _string - String to trim + _trim - Characters to trim (optional, default: "") Returns: - Trimmed string [String] + Trimmed string Example: (begin example) @@ -27,6 +27,9 @@ SCRIPT(trim); params ["_string", ["_trim", "", [""]]]; -_string = [_string, _trim] call CBA_fnc_rightTrim; +// Trim all whitespace characters by default +if (_trim == "") exitWith { + trim _string // return +}; -[_string, _trim] call CBA_fnc_leftTrim +_string trim [_trim, 0] // return