diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2022-09-25 10:53:37 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2022-09-27 15:50:43 +0200 |
commit | 4d74a89cfcdce4d9207f51c786a2a1fc278c5a11 (patch) | |
tree | 57dad8d63de8a0bf1b5f5e6e13d57982da431601 | |
parent | be6a29a596b5da3be4c00943cd5bdceabd5159c5 (diff) | |
download | weechat-4d74a89cfcdce4d9207f51c786a2a1fc278c5a11.zip |
api: add function string_parse_size
32 files changed, 566 insertions, 1 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 5bfc58d10..a2a2110e7 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -28,6 +28,7 @@ New features:: * api: add info "uptime_current" * api: add function crypto_hash_file * api: add support of priority in function hook_line (issue #1821) + * api: add function string_parse_size * buflist: add variable `${hotlist_priority_number}` (integer version of `${hotlist_priority}`) * irc: display SETNAME command in channels and private buffers, add options irc.color.message_setname and irc.look.smart_filter_setname (issue #1805) * irc: add option irc.look.display_pv_nick_change diff --git a/doc/de/weechat_scripting.de.adoc b/doc/de/weechat_scripting.de.adoc index 5ee1587f5..a6737ae33 100644 --- a/doc/de/weechat_scripting.de.adoc +++ b/doc/de/weechat_scripting.de.adoc @@ -589,6 +589,7 @@ Liste der Skript API Funktionen: string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index 5230d5702..5d46dd205 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -1955,6 +1955,51 @@ def string_format_size(size: int) -> str: ... str = weechat.string_format_size(15200) # == "15.2 KB" ---- +==== string_parse_size + +_WeeChat ≥ 3.7._ + +Parse a string with a size and optional unit and return the size in bytes. + +Prototype: + +[source,c] +---- +unsigned long long weechat_string_parse_size (const char *size); +---- + +Arguments: + +* _size_: the size as string: float number followed optional spaces and optional + unit (lower or upper case), which is one of: +** _b_: bytes +** _k_: kilobytes (1k = 1000 bytes) +** _m_: megabytes (1m = 1000k = 1,000,000 bytes) +** _g_: gigabytes (1g = 1000m = 1,000,000,000 bytes) +** _t_: terabytes (1t = 1000g = 1,000,000,000,000 bytes) + +Return value: + +* size in bytes, 0 if error + +C example: + +[source,c] +---- +unsigned long long size = weechat_parse_size ("1.34m"); /* size == 1340000 */ +---- + +Script (Python): + +[source,python] +---- +# prototype +def string_parse_size(size: str) -> int: ... + +# example +size = weechat.string_parse_size("1.34m") # 1340000 +---- + ==== string_color_code_size _WeeChat ≥ 3.0._ diff --git a/doc/en/weechat_scripting.en.adoc b/doc/en/weechat_scripting.en.adoc index 9056027ea..237e2aabd 100644 --- a/doc/en/weechat_scripting.en.adoc +++ b/doc/en/weechat_scripting.en.adoc @@ -573,6 +573,7 @@ List of functions in script API: string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index 55d51e661..790fdf10c 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -1990,6 +1990,53 @@ def string_format_size(size: int) -> str: ... str = weechat.string_format_size(15200) # == "15.2 Ko" ---- +==== string_parse_size + +_WeeChat ≥ 3.7._ + +Analyser une chaîne avec une taille et une unité optionnelle et retourner +la taille en octets. + +Prototype : + +[source,c] +---- +unsigned long long weechat_string_parse_size (const char *size); +---- + +Paramètres : + +* _size_ : la taille sous forme de chaîne : nombre décimal suivi par des espaces + optionnels et une unité optionnelle (en minuscules ou majuscules), qui est une + des suivantes : +** _b_ : octets +** _k_ : kilo-octets (1k = 1000 octets) +** _m_ : méga-octets (1m = 1000k = 1 000 000 octets) +** _g_ : giga-octets (1g = 1000m = 1 000 000 000 octets) +** _t_ : tera-octets (1t = 1000g = 1 000 000 000 000 octets) + +Valeur de retour : + +* taille en octets, 0 si erreur + +Exemple en C : + +[source,c] +---- +unsigned long long size = weechat_parse_size ("1.34m"); /* size == 1340000 */ +---- + +Script (Python) : + +[source,python] +---- +# prototype +def string_parse_size(size: str) -> int: ... + +# exemple +size = weechat.string_parse_size("1.34m") # 1340000 +---- + ==== string_color_code_size _WeeChat ≥ 3.0._ diff --git a/doc/fr/weechat_scripting.fr.adoc b/doc/fr/weechat_scripting.fr.adoc index 0aa19c672..4245b0e4f 100644 --- a/doc/fr/weechat_scripting.fr.adoc +++ b/doc/fr/weechat_scripting.fr.adoc @@ -593,6 +593,7 @@ Liste des fonctions de l'API script : string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 5c1ff0685..e3f94f74e 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -2044,6 +2044,52 @@ def string_format_size(size: int) -> str: ... str = weechat.string_format_size(15200) # == "15.2 KB" ---- +// TRANSLATION MISSING +==== string_parse_size + +_WeeChat ≥ 3.7._ + +Parse a string with a size and optional unit and return the size in bytes. + +Prototipo: + +[source,c] +---- +unsigned long long weechat_string_parse_size (const char *size); +---- + +Argomenti: + +* _size_: the size as string: float number followed optional spaces and optional + unit (lower or upper case), which is one of: +** _b_: bytes +** _k_: kilobytes (1k = 1000 bytes) +** _m_: megabytes (1m = 1000k = 1,000,000 bytes) +** _g_: gigabytes (1g = 1000m = 1,000,000,000 bytes) +** _t_: terabytes (1t = 1000g = 1,000,000,000,000 bytes) + +Valore restituito: + +* size in bytes, 0 if error + +Esempio in C: + +[source,c] +---- +unsigned long long size = weechat_parse_size ("1.34m"); /* size == 1340000 */ +---- + +Script (Python): + +[source,python] +---- +# prototipo +def string_parse_size(size: str) -> int: ... + +# esempio +size = weechat.string_parse_size("1.34m") # 1340000 +---- + ==== string_color_code_size _WeeChat ≥ 3.0._ diff --git a/doc/it/weechat_scripting.it.adoc b/doc/it/weechat_scripting.it.adoc index ac6fd28f0..44bed235c 100644 --- a/doc/it/weechat_scripting.it.adoc +++ b/doc/it/weechat_scripting.it.adoc @@ -603,6 +603,7 @@ Elenco di funzioni nelle API per gli script: string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index 7511c36de..0a1b8516b 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -1981,6 +1981,52 @@ def string_format_size(size: int) -> str: ... str = weechat.string_format_size(15200) # == "15.2 KB" ---- +// TRANSLATION MISSING +==== string_parse_size + +_WeeChat ≥ 3.7._ + +Parse a string with a size and optional unit and return the size in bytes. + +プロトタイプ: + +[source,c] +---- +unsigned long long weechat_string_parse_size (const char *size); +---- + +引数: + +* _size_: the size as string: float number followed optional spaces and optional + unit (lower or upper case), which is one of: +** _b_: bytes +** _k_: kilobytes (1k = 1000 bytes) +** _m_: megabytes (1m = 1000k = 1,000,000 bytes) +** _g_: gigabytes (1g = 1000m = 1,000,000,000 bytes) +** _t_: terabytes (1t = 1000g = 1,000,000,000,000 bytes) + +戻り値: + +* size in bytes, 0 if error + +C 言語での使用例: + +[source,c] +---- +unsigned long long size = weechat_parse_size ("1.34m"); /* size == 1340000 */ +---- + +Script (Python): + +[source,python] +---- +# プロトタイプ +def string_parse_size(size: str) -> int: ... + +# 例 +size = weechat.string_parse_size("1.34m") # 1340000 +---- + ==== string_color_code_size _WeeChat ≥ 3.0._ diff --git a/doc/ja/weechat_scripting.ja.adoc b/doc/ja/weechat_scripting.ja.adoc index acc3cb3be..4fd333797 100644 --- a/doc/ja/weechat_scripting.ja.adoc +++ b/doc/ja/weechat_scripting.ja.adoc @@ -596,6 +596,7 @@ link:weechat_plugin_api.ja.html[WeeChat プラグイン API リファレンス string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/doc/pl/weechat_scripting.pl.adoc b/doc/pl/weechat_scripting.pl.adoc index 8ec71b9b4..ac97343e1 100644 --- a/doc/pl/weechat_scripting.pl.adoc +++ b/doc/pl/weechat_scripting.pl.adoc @@ -580,6 +580,7 @@ Lista funkcji w API skryptów: string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc index d12126d84..36b189a3a 100644 --- a/doc/sr/weechat_plugin_api.sr.adoc +++ b/doc/sr/weechat_plugin_api.sr.adoc @@ -1875,6 +1875,52 @@ def string_format_size(size: int) -> str: ... str = weechat.string_format_size(15200) # == "15.2 KB" ---- +// TRANSLATION MISSING +==== string_parse_size + +_WeeChat ≥ 3.7._ + +Parse a string with a size and optional unit and return the size in bytes. + +Прототип: + +[source,c] +---- +unsigned long long weechat_string_parse_size (const char *size); +---- + +Аргументи: + +* _size_: the size as string: float number followed optional spaces and optional + unit (lower or upper case), which is one of: +** _b_: bytes +** _k_: kilobytes (1k = 1000 bytes) +** _m_: megabytes (1m = 1000k = 1,000,000 bytes) +** _g_: gigabytes (1g = 1000m = 1,000,000,000 bytes) +** _t_: terabytes (1t = 1000g = 1,000,000,000,000 bytes) + +Повратна вредност: + +* size in bytes, 0 if error + +C пример: + +[source,c] +---- +unsigned long long size = weechat_parse_size ("1.34m"); /* size == 1340000 */ +---- + +Script (Python): + +[source,python] +---- +# прототип +def string_parse_size(size: str) -> int: ... + +# пример +size = weechat.string_parse_size("1.34m") # 1340000 +---- + ==== string_color_code_size _WeeChat ≥ 3.0._ diff --git a/doc/sr/weechat_scripting.sr.adoc b/doc/sr/weechat_scripting.sr.adoc index 80264ad31..0f880dba3 100644 --- a/doc/sr/weechat_scripting.sr.adoc +++ b/doc/sr/weechat_scripting.sr.adoc @@ -532,6 +532,7 @@ weechat_hook_timer(1000, 0, 1, $timer_cb, 'test'); string_has_highlight_regex + string_mask_to_regex + string_format_size + + string_parse_size + string_color_code_size + string_remove_color + string_is_command_char + diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 523d0fdff..44d0dd9cf 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -2979,6 +2979,97 @@ string_format_size (unsigned long long size) } /* + * Parses a string with a size and returns the size in bytes. + * + * The format is "123" or "123X" or "123 X" where 123 is any positive number + * as a float (for example: 123 or 4.56) and X the unit, which can be one of + * (lower or upper case are accepted): + * + * b bytes (default if unit is missing) + * k kilobytes (1k = 1000 bytes) + * m megabytes (1m = 1000k = 1,000,000 bytes) + * g gigabytes (1g = 1000m = 1,000,000,000 bytes) + * t terabytes (1t = 1000g = 1,000,000,000,000 bytes) + * + * Note: decimals of the float number are ignored if the unit is bytes + * (eg: "5.9" or "5.9B" returns 5). + * + * Returns the parsed size, 0 if error. + */ + +unsigned long long +string_parse_size (const char *size) +{ + const char *pos; + char *str_number, *error; + double number; + unsigned long long result; + + str_number = NULL; + result = 0; + + if (!size || !size[0]) + goto end; + + pos = size; + while ((pos[0] == '.') || isdigit (pos[0])) + { + pos++; + } + + if (pos == size) + goto end; + + str_number = string_strndup (size, pos - size); + if (!str_number) + goto end; + + number = strtod (str_number, &error); + if (!error || error[0]) + goto end; + + while (pos[0] == ' ') + { + pos++; + } + + if (pos[0] && pos[1]) + goto end; + + switch (pos[0]) + { + case '\0': + result = number; + break; + case 'b': + case 'B': + result = number; + break; + case 'k': + case 'K': + result = number * 1000.0; + break; + case 'm': + case 'M': + result = number * 1000.0 * 1000.0; + break; + case 'g': + case 'G': + result = number * 1000.0 * 1000.0 * 1000.0; + break; + case 't': + case 'T': + result = number * 1000.0 * 1000.0 * 1000.0 * 1000.0; + break; + } + +end: + if (str_number) + free (str_number); + return result; +} + +/* * Encodes a string in base16 (hexadecimal). * * Argument "length" is number of bytes in "from" to convert (commonly diff --git a/src/core/wee-string.h b/src/core/wee-string.h index c0b29106b..f1e9109c0 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -111,6 +111,7 @@ extern char *string_iconv_from_internal (const char *charset, const char *string); extern int string_fprintf (FILE *file, const char *data, ...); extern char *string_format_size (unsigned long long size); +extern unsigned long long string_parse_size (const char *size); extern int string_base16_encode (const char *from, int length, char *to); extern int string_base16_decode (const char *from, char *to); extern int string_base32_encode (const char *from, int length, char *to); diff --git a/src/plugins/guile/weechat-guile-api.c b/src/plugins/guile/weechat-guile-api.c index 490d4f8ad..ce0d8c75a 100644 --- a/src/plugins/guile/weechat-guile-api.c +++ b/src/plugins/guile/weechat-guile-api.c @@ -432,6 +432,20 @@ weechat_guile_api_string_format_size (SCM size) } SCM +weechat_guile_api_string_parse_size (SCM size) +{ + unsigned long long value; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + if (!scm_is_string (size)) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + value = weechat_string_parse_size (API_SCM_TO_STRING(size)); + + API_RETURN_LONG(value); +} + +SCM weechat_guile_api_string_color_code_size (SCM string) { int size; @@ -5118,6 +5132,7 @@ weechat_guile_api_module_init (void *data) API_DEF_FUNC(string_has_highlight_regex, 2); API_DEF_FUNC(string_mask_to_regex, 1); API_DEF_FUNC(string_format_size, 1); + API_DEF_FUNC(string_parse_size, 1); API_DEF_FUNC(string_color_code_size, 1); API_DEF_FUNC(string_remove_color, 2); API_DEF_FUNC(string_is_command_char, 1); diff --git a/src/plugins/javascript/weechat-js-api.cpp b/src/plugins/javascript/weechat-js-api.cpp index 829b31057..5019ccbe7 100644 --- a/src/plugins/javascript/weechat-js-api.cpp +++ b/src/plugins/javascript/weechat-js-api.cpp @@ -378,6 +378,19 @@ API_FUNC(string_format_size) API_RETURN_STRING_FREE(result); } +API_FUNC(string_parse_size) +{ + unsigned long long value; + + API_INIT_FUNC(1, "string_parse_size", "s", API_RETURN_LONG(0)); + + v8::String::Utf8Value size(args[0]); + + value = weechat_string_parse_size (*size); + + API_RETURN_LONG(value); +} + API_FUNC(string_color_code_size) { int size; @@ -5060,6 +5073,7 @@ WeechatJsV8::loadLibs() API_DEF_FUNC(string_has_highlight_regex); API_DEF_FUNC(string_mask_to_regex); API_DEF_FUNC(string_format_size); + API_DEF_FUNC(string_parse_size); API_DEF_FUNC(string_color_code_size); API_DEF_FUNC(string_remove_color); API_DEF_FUNC(string_is_command_char); diff --git a/src/plugins/lua/weechat-lua-api.c b/src/plugins/lua/weechat-lua-api.c index e97d3a9e2..6364e86b7 100644 --- a/src/plugins/lua/weechat-lua-api.c +++ b/src/plugins/lua/weechat-lua-api.c @@ -423,6 +423,22 @@ API_FUNC(string_format_size) API_RETURN_STRING_FREE(result); } +API_FUNC(string_parse_size) +{ + const char *size; + unsigned long long value; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + if (lua_gettop (L) < 1) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + size = lua_tostring (L, -1); + + value = weechat_string_parse_size (size); + + API_RETURN_LONG(value); +} + API_FUNC(string_color_code_size) { const char *string; @@ -5416,6 +5432,7 @@ const struct luaL_Reg weechat_lua_api_funcs[] = { API_DEF_FUNC(string_has_highlight_regex), API_DEF_FUNC(string_mask_to_regex), API_DEF_FUNC(string_format_size), + API_DEF_FUNC(string_parse_size), API_DEF_FUNC(string_color_code_size), API_DEF_FUNC(string_remove_color), API_DEF_FUNC(string_is_command_char), diff --git a/src/plugins/perl/weechat-perl-api.c b/src/plugins/perl/weechat-perl-api.c index 33fd6677c..d25c8f600 100644 --- a/src/plugins/perl/weechat-perl-api.c +++ b/src/plugins/perl/weechat-perl-api.c @@ -383,6 +383,20 @@ API_FUNC(string_format_size) API_RETURN_STRING_FREE(result); } +API_FUNC(string_parse_size) +{ + unsigned long long value; + dXSARGS; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + if (items < 1) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + value = weechat_string_parse_size (SvPV_nolen (ST (0))); /* size */ + + API_RETURN_LONG(value); +} + API_FUNC(string_color_code_size) { int size; @@ -5367,6 +5381,7 @@ weechat_perl_api_init (pTHX) API_DEF_FUNC(string_has_highlight_regex); API_DEF_FUNC(string_mask_to_regex); API_DEF_FUNC(string_format_size); + API_DEF_FUNC(string_parse_size); API_DEF_FUNC(string_color_code_size); API_DEF_FUNC(string_remove_color); API_DEF_FUNC(string_is_command_char); diff --git a/src/plugins/php/weechat-php-api.c b/src/plugins/php/weechat-php-api.c index 7a7ee4233..afcbf193a 100644 --- a/src/plugins/php/weechat-php-api.c +++ b/src/plugins/php/weechat-php-api.c @@ -496,6 +496,23 @@ API_FUNC(string_format_size) API_RETURN_STRING_FREE(result); } +API_FUNC(string_parse_size) +{ + zend_string *z_size; + char *size; + unsigned long long value; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + if (zend_parse_parameters (ZEND_NUM_ARGS(), "S", &z_size) == FAILURE) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + size = ZSTR_VAL(z_size); + + value = weechat_string_parse_size ((const char *)size); + + API_RETURN_LONG(value); +} + API_FUNC(string_color_code_size) { zend_string *z_string; diff --git a/src/plugins/php/weechat-php-api.h b/src/plugins/php/weechat-php-api.h index 563e09c1a..59d49840f 100644 --- a/src/plugins/php/weechat-php-api.h +++ b/src/plugins/php/weechat-php-api.h @@ -59,6 +59,7 @@ PHP_FUNCTION(weechat_string_has_highlight); PHP_FUNCTION(weechat_string_has_highlight_regex); PHP_FUNCTION(weechat_string_mask_to_regex); PHP_FUNCTION(weechat_string_format_size); +PHP_FUNCTION(weechat_string_parse_size); PHP_FUNCTION(weechat_string_color_code_size); PHP_FUNCTION(weechat_string_remove_color); PHP_FUNCTION(weechat_string_is_command_char); diff --git a/src/plugins/php/weechat-php.c b/src/plugins/php/weechat-php.c index 72a2c1036..f7bed5d51 100644 --- a/src/plugins/php/weechat-php.c +++ b/src/plugins/php/weechat-php.c @@ -117,6 +117,7 @@ const zend_function_entry weechat_functions[] = { PHP_FE(weechat_string_has_highlight_regex, arginfo_weechat_string_has_highlight_regex) PHP_FE(weechat_string_mask_to_regex, arginfo_weechat_string_mask_to_regex) PHP_FE(weechat_string_format_size, arginfo_weechat_string_format_size) + PHP_FE(weechat_string_parse_size, arginfo_weechat_string_parse_size) PHP_FE(weechat_string_color_code_size, arginfo_weechat_string_color_code_size) PHP_FE(weechat_string_remove_color, arginfo_weechat_string_remove_color) PHP_FE(weechat_string_is_command_char, arginfo_weechat_string_is_command_char) diff --git a/src/plugins/php/weechat-php.stub.php b/src/plugins/php/weechat-php.stub.php index c57ccedab..50c836268 100644 --- a/src/plugins/php/weechat-php.stub.php +++ b/src/plugins/php/weechat-php.stub.php @@ -25,6 +25,7 @@ function weechat_string_has_highlight(string $p0, string $p1): int {} function weechat_string_has_highlight_regex(string $p0, string $p1): int {} function weechat_string_mask_to_regex(string $p0): string {} function weechat_string_format_size(int $p0): string {} +function weechat_string_parse_size(string $p0): int {} function weechat_string_color_code_size(string $p0): int {} function weechat_string_remove_color(string $p0, string $p1): string {} function weechat_string_is_command_char(string $p0): int {} diff --git a/src/plugins/php/weechat-php_arginfo.h b/src/plugins/php/weechat-php_arginfo.h index a36899771..4f81116e9 100644 --- a/src/plugins/php/weechat-php_arginfo.h +++ b/src/plugins/php/weechat-php_arginfo.h @@ -57,6 +57,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_weechat_string_format_size, 0, 1 ZEND_ARG_TYPE_INFO(0, p0, IS_LONG, 0) ZEND_END_ARG_INFO() +#define arginfo_weechat_string_parse_size arginfo_weechat_charset_set + #define arginfo_weechat_string_color_code_size arginfo_weechat_charset_set #define arginfo_weechat_string_remove_color arginfo_weechat_iconv_to_internal diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 42a940656..3e2471dd2 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -633,6 +633,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->string_split_command = &string_split_command; new_plugin->string_free_split_command = &string_free_split_command; new_plugin->string_format_size = &string_format_size; + new_plugin->string_parse_size = &string_parse_size; new_plugin->string_color_code_size = &gui_color_code_size; new_plugin->string_remove_color = &gui_color_decode; new_plugin->string_base_encode = &string_base_encode; diff --git a/src/plugins/python/weechat-python-api.c b/src/plugins/python/weechat-python-api.c index b823f1967..e8585c368 100644 --- a/src/plugins/python/weechat-python-api.c +++ b/src/plugins/python/weechat-python-api.c @@ -369,6 +369,21 @@ API_FUNC(string_format_size) API_RETURN_STRING_FREE(result); } +API_FUNC(string_parse_size) +{ + char *size; + unsigned long long value; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + size = NULL; + if (!PyArg_ParseTuple (args, "s", &size)) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + value = weechat_string_parse_size (size); + + API_RETURN_LONG(value); +} + API_FUNC(string_color_code_size) { char *string; @@ -5281,6 +5296,7 @@ PyMethodDef weechat_python_funcs[] = API_DEF_FUNC(string_has_highlight_regex), API_DEF_FUNC(string_mask_to_regex), API_DEF_FUNC(string_format_size), + API_DEF_FUNC(string_parse_size), API_DEF_FUNC(string_color_code_size), API_DEF_FUNC(string_remove_color), API_DEF_FUNC(string_is_command_char), diff --git a/src/plugins/python/weechat.pyi b/src/plugins/python/weechat.pyi index 5c2f1cb2d..de277d548 100644 --- a/src/plugins/python/weechat.pyi +++ b/src/plugins/python/weechat.pyi @@ -122,6 +122,11 @@ def string_format_size(size: int) -> str: ... +def string_parse_size(size: str) -> int: + """`string_parse_size in WeeChat plugin API reference <https://weechat.org/doc/api/#_string_parse_size>`_""" + ... + + def string_color_code_size(string: str) -> int: """`string_color_code_size in WeeChat plugin API reference <https://weechat.org/doc/api/#_string_color_code_size>`_""" ... diff --git a/src/plugins/ruby/weechat-ruby-api.c b/src/plugins/ruby/weechat-ruby-api.c index 80acd781d..a1a8edf05 100644 --- a/src/plugins/ruby/weechat-ruby-api.c +++ b/src/plugins/ruby/weechat-ruby-api.c @@ -449,6 +449,25 @@ weechat_ruby_api_string_format_size (VALUE class, VALUE size) } static VALUE +weechat_ruby_api_string_parse_size (VALUE class, VALUE size) +{ + char *c_size; + unsigned long long value; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + if (NIL_P (size)) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + Check_Type (size, T_STRING); + + c_size = StringValuePtr (size); + + value = weechat_string_parse_size (c_size); + + API_RETURN_LONG(value); +} + +static VALUE weechat_ruby_api_string_color_code_size (VALUE class, VALUE string) { char *c_string; @@ -6562,6 +6581,7 @@ weechat_ruby_api_init (VALUE ruby_mWeechat) API_DEF_FUNC(string_has_highlight_regex, 2); API_DEF_FUNC(string_mask_to_regex, 1); API_DEF_FUNC(string_format_size, 1); + API_DEF_FUNC(string_parse_size, 1); API_DEF_FUNC(string_color_code_size, 1); API_DEF_FUNC(string_remove_color, 2); API_DEF_FUNC(string_is_command_char, 1); diff --git a/src/plugins/tcl/weechat-tcl-api.c b/src/plugins/tcl/weechat-tcl-api.c index 24cfc64e5..c7494aa67 100644 --- a/src/plugins/tcl/weechat-tcl-api.c +++ b/src/plugins/tcl/weechat-tcl-api.c @@ -538,6 +538,24 @@ API_FUNC(string_format_size) API_RETURN_STRING_FREE(result); } +API_FUNC(string_parse_size) +{ + Tcl_Obj *objp; + char *size; + unsigned long long value; + int i; + + API_INIT_FUNC(1, "string_parse_size", API_RETURN_LONG(0)); + if (objc < 2) + API_WRONG_ARGS(API_RETURN_LONG(0)); + + size = Tcl_GetStringFromObj (objv[1], &i); + + value = weechat_string_parse_size (size); + + API_RETURN_LONG(value); +} + API_FUNC(string_color_code_size) { Tcl_Obj *objp; @@ -5867,6 +5885,7 @@ void weechat_tcl_api_init (Tcl_Interp *interp) API_DEF_FUNC(string_has_highlight_regex); API_DEF_FUNC(string_mask_to_regex); API_DEF_FUNC(string_format_size); + API_DEF_FUNC(string_parse_size); API_DEF_FUNC(string_color_code_size); API_DEF_FUNC(string_remove_color); API_DEF_FUNC(string_is_command_char); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 5078b4de2..1bfa9a9dc 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -68,7 +68,7 @@ struct timeval; * please change the date with current one; for a second change at same * date, increment the 01, otherwise please keep 01. */ -#define WEECHAT_PLUGIN_API_VERSION "20220816-01" +#define WEECHAT_PLUGIN_API_VERSION "20220925-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -338,6 +338,7 @@ struct t_weechat_plugin char **(*string_split_command) (const char *command, char separator); void (*string_free_split_command) (char **split_command); char *(*string_format_size) (unsigned long long size); + unsigned long long (*string_parse_size) (const char *size); int (*string_color_code_size) (const char *string); char *(*string_remove_color) (const char *string, const char *replacement); int (*string_base_encode) (int base, const char *from, int length, @@ -1302,6 +1303,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); (weechat_plugin->string_free_split_command)(__split_command) #define weechat_string_format_size(__size) \ (weechat_plugin->string_format_size)(__size) +#define weechat_string_parse_size(__size) \ + (weechat_plugin->string_parse_size)(__size) #define weechat_string_color_code_size(__string) \ (weechat_plugin->string_color_code_size)(__string) #define weechat_string_remove_color(__string, __replacement) \ diff --git a/tests/scripts/python/testapi.py b/tests/scripts/python/testapi.py index 75a119b8d..6d3833a1a 100644 --- a/tests/scripts/python/testapi.py +++ b/tests/scripts/python/testapi.py @@ -72,6 +72,16 @@ def test_strings(): check(weechat.string_format_size(1) == '1 byte') check(weechat.string_format_size(2097152) == '2.10 MB') check(weechat.string_format_size(420000000) == '420.00 MB') + check(weechat.string_parse_size('') == 0) + check(weechat.string_parse_size('*') == 0) + check(weechat.string_parse_size('z') == 0) + check(weechat.string_parse_size('1ba') == 0) + check(weechat.string_parse_size('1') == 1) + check(weechat.string_parse_size('12b') == 12) + check(weechat.string_parse_size('123 b') == 123) + check(weechat.string_parse_size('1.34k') == 1340) + check(weechat.string_parse_size('1.5m') == 1500000) + check(weechat.string_parse_size('2.1g') == 2100000000) check(weechat.string_color_code_size('') == 0) check(weechat.string_color_code_size('test') == 0) str_color = weechat.color('yellow,red') diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index d3f0b5ea4..d7b4612ea 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -1907,6 +1907,84 @@ TEST(CoreString, FormatSize) /* * Tests functions: + * string_parse_size + */ + +TEST(CoreString, ParseSize) +{ + CHECK(string_parse_size (NULL) == 0ULL); + + CHECK(string_parse_size ("") == 0ULL); + CHECK(string_parse_size ("*") == 0ULL); + CHECK(string_parse_size ("b") == 0ULL); + CHECK(string_parse_size ("k") == 0ULL); + CHECK(string_parse_size ("m") == 0ULL); + CHECK(string_parse_size ("g") == 0ULL); + CHECK(string_parse_size ("t") == 0ULL); + CHECK(string_parse_size ("z") == 0ULL); + CHECK(string_parse_size ("0z") == 0ULL); + + CHECK(string_parse_size ("0") == 0ULL); + CHECK(string_parse_size ("0b") == 0ULL); + CHECK(string_parse_size ("0B") == 0ULL); + + CHECK(string_parse_size ("1") == 1ULL); + CHECK(string_parse_size ("1b") == 1ULL); + CHECK(string_parse_size ("1B") == 1ULL); + CHECK(string_parse_size ("1 b") == 1ULL); + CHECK(string_parse_size ("1 B") == 1ULL); + + CHECK(string_parse_size ("2") == 2ULL); + CHECK(string_parse_size ("2b") == 2ULL); + CHECK(string_parse_size ("2B") == 2ULL); + + CHECK(string_parse_size ("42") == 42ULL); + CHECK(string_parse_size ("42b") == 42ULL); + CHECK(string_parse_size ("42B") == 42ULL); + + /* decimals ignored for bytes */ + CHECK(string_parse_size ("42.9") == 42ULL); + CHECK(string_parse_size ("42.9b") == 42ULL); + CHECK(string_parse_size ("42.9B") == 42ULL); + + CHECK(string_parse_size ("999") == 999ULL); + CHECK(string_parse_size ("999b") == 999ULL); + CHECK(string_parse_size ("999B") == 999ULL); + + CHECK(string_parse_size ("1200") == 1200ULL); + CHECK(string_parse_size ("1200b") == 1200ULL); + CHECK(string_parse_size ("1200B") == 1200ULL); + + CHECK(string_parse_size ("1k") == 1000ULL); + CHECK(string_parse_size ("1K") == 1000ULL); + + CHECK(string_parse_size ("1.34k") == 1340ULL); + CHECK(string_parse_size ("1.34K") == 1340ULL); + + CHECK(string_parse_size ("14.08k") == 14080ULL); + CHECK(string_parse_size ("14.08K") == 14080ULL); + + CHECK(string_parse_size ("1m") == 1000000ULL); + CHECK(string_parse_size ("1M") == 1000000ULL); + + CHECK(string_parse_size ("1.5m") == 1500000ULL); + CHECK(string_parse_size ("1.5M") == 1500000ULL); + + CHECK(string_parse_size ("1g") == 1000000000ULL); + CHECK(string_parse_size ("1G") == 1000000000ULL); + + CHECK(string_parse_size ("1.2345g") == 1234500000ULL); + CHECK(string_parse_size ("1.2345G") == 1234500000ULL); + + CHECK(string_parse_size ("1t") == 1000000000000ULL); + CHECK(string_parse_size ("1T") == 1000000000000ULL); + + CHECK(string_parse_size ("1.23456789t") == 1234567890000ULL); + CHECK(string_parse_size ("1.23456789T") == 1234567890000ULL); +} + +/* + * Tests functions: * string_base16_encode * string_base16_decode */ |