string_find renamed to string_pos + header update

- Renamed get_clamped to math_clamp for consistency (libs should use namespace prefixes)
- Nuked above/below macros and renamed unsigned_comp to insigned_int_compare, because it doesn't work with floats
- Replaced sprintf2 and string_pos with macros (due to new metarules)
- Add a few entries to functions.yml
This commit is contained in:
phobos2077
2023-07-04 21:06:35 +02:00
parent 74ef65335d
commit 664fb84dc5
7 changed files with 33 additions and 76 deletions
+19 -3
View File
@@ -599,7 +599,7 @@
opcode: 0x8212
- name: Math
parent: Variables
parent: Utility
items:
- name: log
detail: float log(float x)
@@ -1068,7 +1068,7 @@
`fixedParam`: the value that is passed to the `timed_event_p_proc` procedure for the `fixed_param` function
macro: sfall.h
- name: Variables
- name: Utility
items:
- name: sprintf
detail: string sprintf(string format, any value)
@@ -1088,7 +1088,7 @@
opcode: 0x8238
- name: Strings
parent: Variables
parent: Utility
items:
- name: string_split
detail: array string_split(string text, split)
@@ -1116,12 +1116,28 @@
(DEPRECATED) Returns a pointer to a string variable or to a text
- __NOTE:__ this function is intended for use only in `HOOK_DESCRIPTIONOBJ`. Starting from sfall 4.4/3.8.40, you can return normal strings directly in the hook without calling the function
macro: sfall.h
- name: string_pos
detail: string string_pos(string haystack, string needle)
doc: Returns position of a first occurance of a needle string in a haystack string, or -1 if not found.
macro: sfall.h
- name: string_pos_from
detail: string string_pos_from(string haystack, string needle, int pos)
doc: Returns position of a first occurance of a needle string in a haystack string (starting from a given 0-based position), or -1 if not found.
macro: sfall.h
- name: string_format
detail: string string_format(string format, any val1, any val2, ...)
doc: |
Formats given values using standard syntax of C `printf` function (google "printf" for format details). However, it is limited to formatting up to 7 values.
- The format string is limited to 1024 characters
macro: sfall.h
- name: string_format_array
detail: string string_format_array(string format, int array)
doc: The same as string_format, but accepts an array of parameters.
macro: lib.strings.h
- name: string_replace
detail: string string_replace(string str, string search, string replace)
doc: Replaces all occurances of a given search string in a string with a given replacement string.
macro: lib.strings.h
- name: string_to_case
detail: string sfall_func2("string_to_case", string text, int toCase)
doc: |
+3 -8
View File
@@ -5,13 +5,8 @@
Numbers...
*/
#define above(a, b) (unsigned_comp(a, b) > 0)
#define above_equal(a, b) (unsigned_comp(a, b) >= 0)
#define below(a, b) (unsigned_comp(a, b) < 0)
#define below_equal(a, b) (unsigned_comp(a, b) <= 0)
// for sfall 4.2.3/3.8.23
pure procedure unsigned_comp(variable a, variable b) begin
pure procedure unsigned_int_compare(variable a, variable b) begin
if ((a bwxor b) == 0) then return 0; // a == b
return 1 if ((b == 0) orElse a div b) else -1;
end
@@ -21,10 +16,10 @@ end
#define math_max(x, y) (x if x > y else y)
#define math_min(x, y) (x if x < y else y)
#define in_range(x, from, to) (x >= from and x <= to)
#define math_in_range(x, from, to) (x >= from and x <= to)
procedure get_clamped(variable val, variable a, variable b) begin
procedure math_clamp(variable val, variable a, variable b) begin
variable min, max;
if (a < b) then begin
min := a;
+6 -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()
@@ -188,6 +131,8 @@ 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.
+2 -1
View File
@@ -391,7 +391,8 @@
#define spatial_radius(obj) sfall_func1("spatial_radius", obj)
#define string_compare(str1, str2) sfall_func2("string_compare", str1, str2)
#define string_compare_locale(str1, str2, codePage) sfall_func3("string_compare", str1, str2, codePage)
#define string_find(haystack, needle, pos) sfall_func3("string_find", haystack, needle, pos)
#define string_pos(haystack, needle) sfall_func2("string_pos", haystack, needle)
#define string_pos_from(haystack, needle, pos) sfall_func3("string_pos", haystack, needle, pos)
#define string_format1(format, a1) sfall_func2("string_format", format, a1)
#define string_format2(format, a1, a2) sfall_func3("string_format", format, a1, a2)
#define string_format3(format, a1, a2, a3) sfall_func4("string_format", format, a1, a2, a3)