Pixel-Composer/scripts/string_splice/string_splice.gml

52 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-01-09 03:39:40 +01:00
function string_splice(str, delim = " ", keep = false, empty = true) {
2022-01-13 05:24:03 +01:00
var st = [];
var ss = str;
var sp;
2023-07-12 16:28:32 +02:00
if(!is_array(delim)) delim = [ delim ];
2022-01-13 05:24:03 +01:00
2023-07-12 16:28:32 +02:00
while(1) {
sp = 99999;
var found = false;
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(delim); i < n; i++ ) {
2023-07-12 16:28:32 +02:00
var pos = string_pos(delim[i], ss);
if(pos) {
sp = min(sp, pos);
found = true;
}
}
2022-01-13 05:24:03 +01:00
2023-07-12 16:28:32 +02:00
if(!found) { //no delim left
2024-01-09 03:39:40 +01:00
if(empty || string_length(ss)) array_push(st, ss);
2023-01-04 02:30:04 +01:00
break;
2022-01-13 05:24:03 +01:00
} else {
2023-07-12 16:28:32 +02:00
var _ss = string_copy(ss, 1, keep? sp : sp - 1);
2024-01-09 03:39:40 +01:00
if(empty || string_length(ss)) array_push(st, _ss);
2022-01-13 05:24:03 +01:00
}
2023-07-12 16:28:32 +02:00
2022-01-13 05:24:03 +01:00
ss = string_copy(ss, sp + 1, string_length(ss) - sp);
2023-07-12 16:28:32 +02:00
}
2022-01-13 05:24:03 +01:00
return st;
2022-11-18 03:20:31 +01:00
}
function string_title(str) {
str = string_replace_all(str, "_", " ");
2022-11-18 03:20:31 +01:00
var ch = string_char_at(str, 1);
ch = string_upper(ch);
2022-11-18 03:20:31 +01:00
var rs = string_copy(str, 2, string_length(str) - 1);
return ch + rs;
}
function string_titlecase(str) {
var ch = "", ind = 1, len = string_length(str);
var tt = "";
var sp = true;
repeat(len) {
ch = string_char_at(str, ind++);
tt += sp? string_upper(ch) : ch;
sp = ch == " ";
}
return tt;
2022-01-13 05:24:03 +01:00
}