/
Bybaleshqa
/
Fsfall
Обзор
Документация
Войти
/
Bybaleshqa
/
Fsfall
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
Releases
scripting/headers/lib.strings.h
202 строки
5 KB
Mr.Stalin
Обновление файлов для релиза.
22 сен 2024, 02:05
22 сен 2024, 02:05
c980793
Код
Авторство
О чём код?
/** This library contains procedures to work with strings. @author phobos2077 */ #ifndef LIB_STRINGS_H #define LIB_STRINGS_H #include "sfall.h" /** * Returns true if given string is 0 or empty string. */ procedure string_null_or_empty(variable str) begin return str == 0 orelse str == ""; end /* Checks if *str* contains *sub* as part of it anywhere in the string. */ #define string_contains(str, sub) (string_find(str, sub) != -1) /* Checks if *str* contains *sub* at the beginning of the string. */ #define string_starts_with(str, sub) (string_find(str, sub) == 0) /** * Returns position of first occurance of sub in str, or -1 if not found */ procedure string_pos(variable str, variable sub) begin return string_find(str, sub); end /** * Returns position of last occurance of sub in str, or -1 if not found */ procedure string_rpos(variable str, variable sub) begin variable pos = string_find(str, sub); while (pos != -1) do begin variable _pos = string_find_from(str, sub, pos + 1); // next if (_pos == -1) then return pos; pos = _pos; end return pos; end /** * Joins *array* of strings into a new string using *join* as delimeter. */ procedure string_join(variable array, variable join) begin variable len := len_array(array); if (len == 0) then return ""; variable i, str := array[0]; for (i := 1; i < len; i++) begin str += join + array[i]; end return str; end /* Replaces all occurances of *search* in *str* with *replace* string. */ #define string_replace(str, search, replace) (string_join(string_split(str, search), replace)) /** * Like *sprintf* but takes parameters from a given list array. */ procedure sprintf_array(variable str, variable list) begin variable split, len; split := string_split(str, "%"); len := len_array(split); if (len == 0) then return str; str := split[0]; variable i, j := 0; for (i := 1; i < len; i++) begin if (split[i] == "") then begin if (i < len - 1) then begin i++; str += "%" + split[i]; end end else begin str += sprintf("%" + split[i], list[j]); j++; end end return str; end /** * The same as sfall string_split, but returns array of integers instead. */ procedure string_split_ints(variable str, variable split) begin if (str == "" orelse typeof(str) != VALTYPE_STR) then return temp_array_list(0); // empty variable list := string_split(str, split); variable arr := temp_array_list(len_array(list)); variable val, n := 0; foreach (val in list) begin if (val != "") then begin arr[n] := atoi(val); n++; end end resize_array(arr, n); return arr; end /* UNFINISHED, don't use! procedure string_get_tokens(variable str, variable delim) begin variable lst, line, token, maxlen, len, count; count := 1; maxlen := 4; token := tokenize(str, 0, delim); //if (token != 0) then //count := 1; line := token; while (line != str) do begin count += 1; len := strlen(token); if (len > maxlen) then maxlen := len; token := tokenize(str, line, delim); if (token != 0) then line += delim + token; end //end return count; 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. Example: parse_str_2("Hello, %2%. I have only $%1% to spare.", money_amount, dude_name); Will return: Hello, Killiano. I have only $1200 to spare. The *_list function takes a temp_array as second parameter. */ /* #define _TOKENIZE_BEGIN \ variable token, rest, line, result, n; \ line := tokenize(str, 0, '%'); \ result := line; \ while line != str do begin \ token := tokenize(str, line, '%'); \ line += "%" + token; \ if token == "" then result += "%"; \ else begin #define _TOKENIZE_END \ end \ rest := tokenize(str, line, '%'); \ if (rest != 0) then begin \ line += "%" + rest; \ result += rest; \ end \ end \ return result; procedure parse_str_list(variable str, variable list) begin _TOKENIZE_BEGIN n := atoi(token) - 1; if (n >= 0 and n < len_array(list)) then result += list[n]; _TOKENIZE_END end procedure parse_str_2(variable str, variable x1, variable x2) begin _TOKENIZE_BEGIN n := atoi(token); if (n == 1) then result += x1; else if (n == 2) then result += x2; _TOKENIZE_END end procedure parse_str_4(variable str, variable x1, variable x2, variable x3, variable x4) begin _TOKENIZE_BEGIN n := atoi(token); if (n == 1) then result += x1; else if (n == 2) then result += x2; else if (n == 3) then result += x3; else if (n == 4) then result += x4; _TOKENIZE_END end #undef _TOKENIZE_BEGIN #undef _TOKENIZE_END */ #endif