Pixel-Composer/scripts/string_functions/string_functions.gml

84 lines
2.0 KiB
Plaintext
Raw Normal View History

2023-09-17 13:23:24 +02:00
function string_to_array(str) { #region
2022-12-21 02:30:23 +01:00
var amo = string_length(str);
var arr = array_create(amo);
for( var i = 0; i < amo; i++ )
arr[i] = string_char_at(str, i + 1);
return arr;
2023-09-17 13:23:24 +02:00
} #endregion
2023-02-14 11:40:24 +01:00
2023-09-17 13:23:24 +02:00
function string_partial_match(str, key) { #region
var amo = string_length(str);
var run = 1;
var consec = 0;
var conMax = 0;
2023-02-14 11:40:24 +01:00
var misMatch = 0;
var kchr = string_char_at(key, run);
var ch;
2023-02-14 11:40:24 +01:00
for( var i = 1; i <= amo; i++ ) {
ch = string_char_at(str, i);
2023-02-14 11:40:24 +01:00
if(ch == kchr) {
consec++;
conMax = max(conMax, consec);
run++;
if(run > string_length(key)) return conMax - (misMatch + (amo - i));
kchr = string_char_at(key, run);
2023-02-14 11:40:24 +01:00
} else {
consec = 0;
2023-02-15 10:43:24 +01:00
misMatch += amo - i;
2023-02-14 11:40:24 +01:00
}
}
return -9999;
2023-09-17 13:23:24 +02:00
} #endregion
2023-02-17 04:48:54 +01:00
2024-01-23 11:01:19 +01:00
function string_real(val, digMax = 999, decMin = 5) { #region
2023-02-17 04:48:54 +01:00
if(is_string(val)) return val;
2023-02-17 11:31:33 +01:00
if(is_struct(val)) return string(val);
2023-02-17 04:48:54 +01:00
if(is_array(val)) {
var s = "[";
2024-04-16 10:34:05 +02:00
var i = 0, n = array_length(val);
repeat( n ) { s += (i? ", " : "") + string_real(val[i]); i++; }
2023-02-17 04:48:54 +01:00
return s + "]";
}
2024-05-17 10:05:24 +02:00
if(val == 0 || is_handle(val) || !is_numeric(val)) return "0";
2023-02-28 09:43:01 +01:00
var pres, p = 1;
2024-01-23 11:01:19 +01:00
var presMax = min(decMin, digMax - ceil(log10(ceil(abs(val)))));
2023-02-28 09:43:01 +01:00
for( pres = 0; pres < presMax; pres++ ) {
if(frac(val * p) == 0)
2023-02-17 04:48:54 +01:00
break;
2023-02-28 09:43:01 +01:00
p *= 10;
2023-02-17 04:48:54 +01:00
}
return string_format(val, -1, pres);
2023-09-17 13:23:24 +02:00
} #endregion
2023-02-23 07:02:19 +01:00
2023-09-17 13:23:24 +02:00
function string_char_last(str, shift = 0) { #region
2023-11-08 08:38:04 +01:00
INLINE
2023-09-17 13:23:24 +02:00
return string_char_at(str, string_length(str) - shift);
2023-10-12 14:14:08 +02:00
} #endregion
2023-09-17 13:23:24 +02:00
function filename_name_only(name) { #region
2023-02-23 07:02:19 +01:00
name = filename_name(name);
return string_replace(name, filename_ext(name), "")
2023-10-12 14:14:08 +02:00
} #endregion
function string_to_var(str) { INLINE return string_replace_all(string_lower(str), " ", "_"); }
2024-06-03 09:34:59 +02:00
function string_quote(str) { INLINE return $"\"{str}\""; }
function array_to_string(arr) {
if(!is_array(arr)) return string(arr);
if(array_empty(arr)) return "[]";
var s = "[";
for (var i = 0, n = array_length(arr); i < n - 1; i++)
s += array_to_string(arr[i]) + ", ";
s += array_to_string(arr[i])
return s + "]";
}