Pixel-Composer/scripts/string_function/string_function.gml

61 lines
1.3 KiB
Text
Raw Normal View History

2022-12-21 02:30:23 +01:00
function string_to_array(str) {
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-02-14 11:40:24 +01:00
}
function string_partial_match(str, key) {
var amo = string_length(str);
var run = 1;
var consec = 0;
var conMax = 0;
var misMatch = 0;
var kchr = string_char_at(key, run);
for( var i = 1; i <= amo; i++ ) {
var ch = string_char_at(str, i);
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);
} 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-02-17 04:48:54 +01:00
}
2023-02-28 09:43:01 +01:00
function string_real(val, digMax = 999) {
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 = "[";
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(val); i < n; i++ )
2023-02-17 04:48:54 +01:00
s += (i? ", " : "") + string_real(val[i]);
return s + "]";
}
2023-02-28 09:43:01 +01:00
if(val == 0) return "0";
var pres, p = 1;
var presMax = min(5, digMax - ceil(log10(ceil(abs(val)))));
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-02-23 07:02:19 +01:00
}
function filename_name_only(name) {
name = filename_name(name);
return string_replace(name, filename_ext(name), "")
2022-12-21 02:30:23 +01:00
}