]> jfr.im git - irc/weechat/weechat.git/commitdiff
api: add function string_parse_size
authorSébastien Helleu <redacted>
Sun, 25 Sep 2022 08:53:37 +0000 (10:53 +0200)
committerSébastien Helleu <redacted>
Tue, 27 Sep 2022 13:50:43 +0000 (15:50 +0200)
32 files changed:
ChangeLog.adoc
doc/de/weechat_scripting.de.adoc
doc/en/weechat_plugin_api.en.adoc
doc/en/weechat_scripting.en.adoc
doc/fr/weechat_plugin_api.fr.adoc
doc/fr/weechat_scripting.fr.adoc
doc/it/weechat_plugin_api.it.adoc
doc/it/weechat_scripting.it.adoc
doc/ja/weechat_plugin_api.ja.adoc
doc/ja/weechat_scripting.ja.adoc
doc/pl/weechat_scripting.pl.adoc
doc/sr/weechat_plugin_api.sr.adoc
doc/sr/weechat_scripting.sr.adoc
src/core/wee-string.c
src/core/wee-string.h
src/plugins/guile/weechat-guile-api.c
src/plugins/javascript/weechat-js-api.cpp
src/plugins/lua/weechat-lua-api.c
src/plugins/perl/weechat-perl-api.c
src/plugins/php/weechat-php-api.c
src/plugins/php/weechat-php-api.h
src/plugins/php/weechat-php.c
src/plugins/php/weechat-php.stub.php
src/plugins/php/weechat-php_arginfo.h
src/plugins/plugin.c
src/plugins/python/weechat-python-api.c
src/plugins/python/weechat.pyi
src/plugins/ruby/weechat-ruby-api.c
src/plugins/tcl/weechat-tcl-api.c
src/plugins/weechat-plugin.h
tests/scripts/python/testapi.py
tests/unit/core/test-core-string.cpp

index 5bfc58d10e830b9cd42a60366f03e88ff3bf5224..a2a2110e7d5c64db7e67ce0dc91181cbb4afd45b 100644 (file)
@@ -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
index 5ee1587f5cc6415b49f72137934d17036068ecdb..a6737ae3348e920623d73ad2d155c152ecec1dc9 100644 (file)
@@ -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 +
index 5230d57027c186a59c495246cea5602b99c391b3..5d46dd20563ef034e9319821b3d5e53f7b3e860c 100644 (file)
@@ -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._
index 9056027ea062dbcbf772e67cde8d1233fb0a690d..237e2aabd43b9a44d632b4e6adc4338df467312c 100644 (file)
@@ -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 +
index 55d51e661f6f248043e2d8841a7b57f1885d25e8..790fdf10c5bd9abd9a3804905346b884f36e42d7 100644 (file)
@@ -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._
index 0aa19c672a2f593740929231ac8dae648dc38500..4245b0e4f7d720bff01ff20c89c0e45791c4f57d 100644 (file)
@@ -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 +
index 5c1ff0685eae8e14a99103e11ae92da3861e4e5c..e3f94f74e53ba37e0a0057af2cf32e8f73733bc6 100644 (file)
@@ -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._
index ac6fd28f06f58b6f7c37c52607c1cc9fd4e137f5..44bed235c9a88fe2216beeb88ce601d7d51620a7 100644 (file)
@@ -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 +
index 7511c36dea38ba2c334d154c8274d5d8674292a3..0a1b8516b1daab19e1f71b3facf1426abb6e3cfc 100644 (file)
@@ -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._
index acc3cb3be034ff0227bfabeadd75cb8a961c1243..4fd3337970d637c7aa10e6d5faaa34be3fcd583d 100644 (file)
@@ -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 +
index 8ec71b9b41b20808a383461d5feb2ab8107ec156..ac97343e1d1dd21b59c0099d2cf3234a67b6f672 100644 (file)
@@ -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 +
index d12126d8450199448b787f032feb5f10a04c565d..36b189a3ac24dd1ae801b2f551c3d937d41bc06d 100644 (file)
@@ -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._
index 80264ad31aa9a081698e5de756470b23dcd97ea1..0f880dba34bb16cc006a1c877380a92480eae21f 100644 (file)
@@ -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 +
index 523d0fdff11fcf9f1e0058a8b66c6ae5f31ee0d1..44d0dd9cf498685cd546289628dce5920e220ea8 100644 (file)
@@ -2978,6 +2978,97 @@ string_format_size (unsigned long long size)
     return strdup (str_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).
  *
index c0b29106b2aebbaae5deb9bdab92f7511aae28dc..f1e9109c0566fa843a4586912a049c86aa75d8e4 100644 (file)
@@ -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);
index 490d4f8ad508c4ec860c9bbc2288965b48f02797..ce0d8c75a25e3751f7abf00da7ced54caf6783e8 100644 (file)
@@ -431,6 +431,20 @@ weechat_guile_api_string_format_size (SCM size)
     API_RETURN_STRING_FREE(result);
 }
 
+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)
 {
@@ -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);
index 829b31057d9928fe3c722809a9a037568858b8ba..5019ccbe7529e19adb97a9e4f5206df046964204 100644 (file)
@@ -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);
index e97d3a9e29359d366bafb805a934282aa36a8dbb..6364e86b7eda908cc612a201bd1c84fcee3b852b 100644 (file)
@@ -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),
index 33fd6677c9b7ce6e40bb68a185e29f8f7b4449c9..d25c8f600fb7c74284b23a4136c96aeacc1aa3b0 100644 (file)
@@ -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);
index 7a7ee423360a7a29b74e1d38fc88fecc02fbf6fe..afcbf193a4e037edf0837cab9a28d4c3bb2544f1 100644 (file)
@@ -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;
index 563e09c1a860104104d4e52672df5e64a4717ea5..59d49840fbfd24d965fa85e17ac0a783be333bd0 100644 (file)
@@ -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);
index 72a2c1036e7b793e5c2a1cbac85f704432e0294d..f7bed5d5123faddee73f19cc5d1cffe4b97a81e5 100644 (file)
@@ -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)
index c57ccedab0629a7d827ad6de5d3ab6944c8175f0..50c836268f3c71a349aef7f1cc4db8bbb65049f2 100644 (file)
@@ -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 {}
index a36899771455b6c7b27037ac85812b87972ecdd0..4f81116e93aa29263a65c383c3e14e0bd5613cd5 100644 (file)
@@ -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
index 42a9406564db78f7eef066e1ea32b900ebcfce6f..3e2471dd2088e76d2505a03305b519f8079cc952 100644 (file)
@@ -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;
index b823f19677f205d53bdac73359c5ad126b0c5b79..e8585c368de98fe31672d39899af2dd42da4348d 100644 (file)
@@ -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),
index 5c2f1cb2d0511dd75de41d0cb14c30198b9b31ce..de277d54854a41e02d1c9daccb1217d1321ead38 100644 (file)
@@ -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>`_"""
     ...
index 80acd781df52a0e50b0906e7f86f818652f0f143..a1a8edf05ee29b3e1f45ca74bfbbe757cd0498cd 100644 (file)
@@ -448,6 +448,25 @@ weechat_ruby_api_string_format_size (VALUE class, VALUE size)
     API_RETURN_STRING_FREE(result);
 }
 
+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)
 {
@@ -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);
index 24cfc64e556486841b5a11bf10ad1e34ba3113f4..c7494aa67fa2ca81659f3f35950eea7f44a15cb5 100644 (file)
@@ -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);
index 5078b4de2cdccb2e80baeaabddfe56eb1d5bfc68..1bfa9a9dcf4fa8279bf8b47bbb842baa0adf2435 100644 (file)
@@ -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)            \
index 75a119b8d07459f2e4bdda287e5ab07ad3e8e325..6d3833a1a1477d836b1a70dd477b31886da1e062 100644 (file)
@@ -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')
index d3f0b5ea40411a7c541a0aa4f1584587089258be..d7b4612ea85e31ebddec826f580c17929e8ceafd 100644 (file)
@@ -1905,6 +1905,84 @@ TEST(CoreString, FormatSize)
     WEE_FORMAT_SIZE("42.00 TB", 42 * ONE_TB);
 }
 
+/*
+ * 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