Added "string_pos" function from 4.x

Added a check to PartyControl::OrderAttackPatch().
Updated function documents.
Updated scripting headers.
This commit is contained in:
NovaRain
2023-07-05 22:20:14 +08:00
parent 2941157174
commit 3a08488895
13 changed files with 154 additions and 92 deletions
+21 -61
View File
@@ -13,17 +13,10 @@
#define is_in_string(str, sub) (string_pos(str, sub) != -1)
#define string_starts_with(str, sub) (substr(str, 0, strlen(sub)) == sub)
/**
* Returns position of first occurance of substr in str, or -1 if not found
*/
procedure string_pos(variable str, variable subst) begin
variable lst, n;
lst := string_split(str, subst);
if (len_array(lst) < 2) then
return -1;
return strlen(lst[0]);
end
#define string_format_array(fmt, arr) sprintf_array(fmt, arr)
// Replaces all occurances of substring in a string with another substring
#define string_replace(str, search, replace) (string_join(string_split(str, search), replace))
#define sprintf2(fmt, arg1, arg2) string_format2(fmt, arg1, arg2)
/**
* Join array of strings using delimeter
@@ -41,44 +34,6 @@ procedure string_join(variable array, variable join) begin
return str;
end
/**
* replaces all occurances of substring in a string with another substring
*/
#define string_replace(str, search, replace) (string_join(string_split(str, search), replace))
/**
* sprintf with two arguments
*/
procedure sprintf2(variable str, variable arg1, variable arg2) begin
variable split, len, i, j, arg;
split := string_split(str, "%");
len := len_array(split);
str := "";
if (len > 0) then begin
str := split[0];
j := 0;
for (i := 1; i < len; i++) begin
if (split[i] == "") then begin
if (i < len - 1) then begin
str += "%" + split[i+1];
i++;
end
end else begin
if (j == 0) then
arg := arg1;
else if (j == 1) then
arg := arg2;
else
arg := 0;
str += sprintf("%" + split[i], arg);
j++;
end
end
end
return str;
end
/**
* sprintf with unlimited number of arguments
*/
@@ -151,18 +106,6 @@ procedure string_repeat(variable str, variable count) begin
return out;
end
/**
* Workaround for string_split bug in sfall 3.3
* DEPRECATED as of sfall 3.4 (bug fixed)
*/
procedure string_split_safe(variable str, variable split) begin
variable lst;
str += split;
lst := string_split(str, split);
resize_array(lst, len_array(lst) - 1);
return lst;
end
/**
* The same as sfall string_split, but returns array of integers instead
* Useful in cunjunction with is_in_array()
@@ -186,8 +129,25 @@ procedure string_split_ints(variable str, variable split) begin
return result;
end
// atoi proc wrapper, for use as callback
procedure string_to_int(variable str) begin
return atoi(str);
end
// atof proc wrapper, for use as callback
procedure string_to_float(variable str) begin
return atof(str);
end
// converts any value to a string, for use as callback
procedure to_string(variable val) begin
return ""+val;
end
/**
DEPRECATED, use string_format instead!
String parse functions. Idea taken from KLIMaka on TeamX forums.
Placeholders in format %d% are replaced from string. d refers to variable index (starting from 1).
You can repeat one placeholder multiple times, or use placeholders in any order.