diff --git a/artifacts/scripting/functions.yml b/artifacts/scripting/functions.yml index 013370fc..b53e9003 100644 --- a/artifacts/scripting/functions.yml +++ b/artifacts/scripting/functions.yml @@ -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: | diff --git a/artifacts/scripting/headers/lib.math.h b/artifacts/scripting/headers/lib.math.h index a854741c..5101828a 100644 --- a/artifacts/scripting/headers/lib.math.h +++ b/artifacts/scripting/headers/lib.math.h @@ -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; diff --git a/artifacts/scripting/headers/lib.strings.h b/artifacts/scripting/headers/lib.strings.h index 942bb3b0..13a76ee8 100644 --- a/artifacts/scripting/headers/lib.strings.h +++ b/artifacts/scripting/headers/lib.strings.h @@ -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. diff --git a/artifacts/scripting/headers/sfall.h b/artifacts/scripting/headers/sfall.h index 99caddb2..680089ea 100644 --- a/artifacts/scripting/headers/sfall.h +++ b/artifacts/scripting/headers/sfall.h @@ -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) diff --git a/sfall/Modules/Scripting/Handlers/Metarule.cpp b/sfall/Modules/Scripting/Handlers/Metarule.cpp index 3abf3f5e..9c65afc0 100644 --- a/sfall/Modules/Scripting/Handlers/Metarule.cpp +++ b/sfall/Modules/Scripting/Handlers/Metarule.cpp @@ -160,7 +160,7 @@ static const SfallMetarule metarules[] = { {"show_window", mf_show_window, 0, 1, -1, {ARG_STRING}}, {"spatial_radius", mf_spatial_radius, 1, 1, 0, {ARG_OBJECT}}, {"string_compare", mf_string_compare, 2, 3, 0, {ARG_STRING, ARG_STRING, ARG_INT}}, - {"string_find", mf_string_find, 2, 3, 0, {ARG_STRING, ARG_STRING, ARG_INT}}, + {"string_pos", mf_string_pos, 2, 3, 0, {ARG_STRING, ARG_STRING, ARG_INT}}, {"string_format", mf_string_format, 2, 8, 0, {ARG_STRING, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY}}, {"string_to_case", mf_string_to_case, 2, 2, -1, {ARG_STRING, ARG_INT}}, {"tile_by_position", mf_tile_by_position, 2, 2, -1, {ARG_INT, ARG_INT}}, diff --git a/sfall/Modules/Scripting/Handlers/Utils.cpp b/sfall/Modules/Scripting/Handlers/Utils.cpp index 60f6c3d3..35aa2dac 100644 --- a/sfall/Modules/Scripting/Handlers/Utils.cpp +++ b/sfall/Modules/Scripting/Handlers/Utils.cpp @@ -198,7 +198,7 @@ void mf_string_compare(OpcodeContext& ctx) { } } -void mf_string_find(OpcodeContext& ctx) { +void mf_string_pos(OpcodeContext& ctx) { const char* const haystack = ctx.arg(0).strValue(); int pos; if (ctx.numArgs() > 2) { diff --git a/sfall/Modules/Scripting/Handlers/Utils.h b/sfall/Modules/Scripting/Handlers/Utils.h index 6c96d413..f78ba2db 100644 --- a/sfall/Modules/Scripting/Handlers/Utils.h +++ b/sfall/Modules/Scripting/Handlers/Utils.h @@ -37,7 +37,7 @@ void op_strlen(OpcodeContext&); void mf_string_compare(OpcodeContext&); -void mf_string_find(OpcodeContext&); +void mf_string_pos(OpcodeContext&); void op_sprintf(OpcodeContext&);