Skip to content

Strings - Cleanup + Optimize string functions - #1853

Open
LinkIsGrim wants to merge 7 commits into
CBATeam:masterfrom
LinkIsGrim:strings-cleanup-takeover
Open

Strings - Cleanup + Optimize string functions#1853
LinkIsGrim wants to merge 7 commits into
CBATeam:masterfrom
LinkIsGrim:strings-cleanup-takeover

Conversation

@LinkIsGrim

@LinkIsGrim LinkIsGrim commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

When merged this pull request will:

All tests passing. Modified implementations (replace and split) are faster. 2.2x on split, replace speed improvements scale with length/matches. sanitizeHTML and decodeURL get faster due to using replace in implementation.

Probably add HEMTT lints for below eventually as they can be trivially replaced by engine commands?

CBA function engine equivalent fires when
CBA_fnc_strLen count _string always — body is count (_this select 0)
CBA_fnc_trim trim _string no trim pattern passed, or ""
CBA_fnc_trim _string trim [_chars, 0] trim pattern passed
CBA_fnc_leftTrim _string trim [_chars, 1] trim pattern passed (default is whitespace)
CBA_fnc_rightTrim _string trim [_chars, 2] trim pattern passed (default is whitespace)
CBA_fnc_find _haystack find [_needle, _index] both args are known strings — CBA's only extra is returning -1 for non-string input
CBA_fnc_substr _string select [_start] no length, or length <= 0
CBA_fnc_substr _string select [_start, _length] length > 0
CBA_fnc_substring _string select [_start, _end + 1 - _start] always — CBA takes an inclusive end index, select takes a length
CBA_fnc_floatToString toFixed always — the function's own header already says DEPRECATED

@LinkIsGrim LinkIsGrim changed the title Strings - Cleanup + Optimize Strings - Cleanup + Optimize string functions Aug 23, 2026

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
private _forceUnicode = count "д" == 1;
private _forceUnicode = count "д" == 1; // differs from _unicode if forceUnicode was 1

made me read the docs

@PabstMirror PabstMirror added this to the 3.19.1 milestone Aug 24, 2026
---------------------------------------------------------------------------- */

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"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
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 isEqualTo 0) then {"0"} else {str parseNumber (str (_this % _this) + str floor abs _this) + "." + (str (abs _this - floor abs _this) select [2]) + "0"}

private _result = "";
private _offset = count (_find splitString "");
// "1" find "" -> 0
if (_find == "") exitWith {_string};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_find == "") exitWith {_string};
if (_find isEqualTo "") exitWith {_string};

while {_string find _find != -1} do {
private _index = _string find _find;
// building the pattern costs more than this check, and most calls don't match
if (_string find _find == -1) exitWith {_string};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_string find _find == -1) exitWith {_string};
if (_string find _find isEqualTo -1) exitWith {_string};

// starts an escape (\U, \L). Doubling both is what writes them out as themselves.
private _format = _replace;

if (_format find "\" != -1) then {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_format find "\" != -1) then {
if (_format find "\" isNotEqualTo -1) then {

Wouldn't even findIf be faster, even if uglier ? Dunno about in, but regexMatch tends to be faster last I tried.

_format = _format regexReplace ["\\", "\\\\"];
};

if (_format find "$" != -1) then {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_format find "$" != -1) then {
if (_format find "$" isNotEqualTo -1) then {


if (_find == 0) then {
_index = _index + _separatorCount;
if (_found == -1) exitWith {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_found == -1) exitWith {
if (_found isEqualTo -1) exitWith {


_string = [_string, _trim] call CBA_fnc_rightTrim;
// Trim all whitespace characters by default
if (_trim == "") exitWith {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_trim == "") exitWith {
if (_trim isEqualTo "") exitWith {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants