summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2017-03-14 07:25:03 +0100
committerSébastien Helleu <flashcode@flashtux.org>2017-03-25 14:19:48 +0100
commit9a8ec36cbda56dfd255a21d8b185e5361162dbfc (patch)
tree9e8dc904cb82f17fdd7967eac135b802e4d94e48
parentdb0ecc07fe138adf9f44705f6f4167ebd003e2e0 (diff)
downloadweechat-9a8ec36cbda56dfd255a21d8b185e5361162dbfc.zip
core: add cut of string in evaluation of expressions
The syntax is: ${cut:max,suffix,string}. The string is cut after max chars. If the string is cut, the optional suffix is added after.
-rw-r--r--doc/en/weechat_plugin_api.en.adoc12
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc14
-rw-r--r--doc/it/weechat_plugin_api.it.adoc12
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc13
-rw-r--r--src/core/wee-eval.c67
-rw-r--r--src/core/wee-string.c40
-rw-r--r--src/core/wee-string.h2
-rw-r--r--tests/unit/core/test-eval.cpp10
8 files changed, 143 insertions, 27 deletions
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc
index 89708624f..3a15a5cdc 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -2035,11 +2035,19 @@ expanded to last):
`+prefix<TAB>message+` +
`+©+`
-| `+${hide:x,value}+` |
- String with hidden chars (all chars in `value` replaced by `x`). |
+| `+${hide:x,string}+` |
+ String with hidden chars (all chars in `string` replaced by `x`). |
`+${hide:*,password}+` |
`+********+`
+| `+${cut:max,suffix,string}+` +
+ (_WeeChat ≥ 1.8_) |
+ String with `max` chars displayed, and optional `suffix` if string is cut. |
+ `+${cut:4,…,this is a test}+` +
+ `+${cut:2,>>,àéçôî}+` |
+ `+this…+` +
+ `+àé>>+`
+
| `+${re:N}+` |
Regex captured group: `0` = whole string matching, `1` to `99` = group
captured, `+++` = last group captured. |
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 930bc887f..79be5e5ea 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -2077,12 +2077,20 @@ première étendue à la dernière) :
`+préfixe<TAB>message+` +
`+©+`
-| `+${hide:x,valeur}+` |
- Chaîne avec les caractères masqués (tous les caractères dans `valeur`
- remplacés par `x`. |
+| `+${hide:x,chaîne}+` |
+ Chaîne avec les caractères masqués (tous les caractères dans `chaîne`
+ remplacés par `x`). |
`+${hide:*,mot_de_passe}+` |
`+************+`
+| `+${cut:max,suffixe,chaîne}+` +
+ (_WeeChat ≥ 1.8_) |
+ Chaîne avec `max` caractères affichés, et un `suffixe` facultatif si la chaîne est coupée. |
+ `+${cut:4,…,ceci est un test}+` +
+ `+${cut:2,>>,àéçôî}+` |
+ `+ceci…+` +
+ `+àé>>+`
+
| `+${re:N}+` |
Groupe regex capturé : `0` = toute la chaîne correspondante,
`1` à `99` = groupe capturé, `+++` = dernier groupe capturé. |
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index 05d7ac51a..29caf8d25 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -2113,11 +2113,19 @@ expanded to last):
`+prefix<TAB>message+` +
`+©+`
-| `+${hide:x,value}+` |
- String with hidden chars (all chars in `value` replaced `x`). |
+| `+${hide:x,string}+` |
+ String with hidden chars (all chars in `string` replaced `x`). |
`+${hide:*,password}+` |
`+********+`
+| `+${cut:max,suffix,string}+` +
+ (_WeeChat ≥ 1.8_) |
+ String with `max` chars displayed, and optional `suffix` if string is cut. |
+ `+${cut:4,…,this is a test}+` +
+ `+${cut:2,>>,àéçôî}+` |
+ `+this…+` +
+ `+àé>>+`
+
| `+${re:N}+` |
Regex captured group: `0` = whole string matching, `1` to `99` = group
captured, `+++` = last group captured. |
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index 3b581ea8b..17860d881 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -2041,11 +2041,20 @@ char *weechat_string_eval_expression (const char *expr,
`+prefix<TAB>message+` +
`+©+`
-| `+${hide:x,value}+` |
- 隠す文字を含むテキスト (`value` に含まれる文字をすべて `x` で置換) |
+| `+${hide:x,string}+` |
+ 隠す文字を含むテキスト (`string` に含まれる文字をすべて `x` で置換) |
`+${hide:*,password}+` |
`+********+`
+// TRANSLATION MISSING
+| `+${cut:max,suffix,string}+` +
+ (_WeeChat バージョン 1.8 以上で利用可_) |
+ String with `max` chars displayed, and optional `suffix` if string is cut. |
+ `+${cut:4,…,this is a test}+` +
+ `+${cut:2,>>,àéçôî}+` |
+ `+this…+` +
+ `+àé>>+`
+
| `+${re:N}+` |
正規表現のキャプチャグループ: `0` = マッチするすべての文字列、`1` から `99` =
キャプチャされたグループ、`+++` = 最後にキャプチャされたグループ |
diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c
index eb332c8cd..91d3bc37a 100644
--- a/src/core/wee-eval.c
+++ b/src/core/wee-eval.c
@@ -295,15 +295,16 @@ end:
* 2. a string to evaluate (format: eval:xxx)
* 3. a string with escaped chars (format: esc:xxx or \xxx)
* 4. a string with chars to hide (format: hide:char,string)
- * 5. a regex group captured (format: re:N (0.99) or re:+)
- * 6. a color (format: color:xxx)
- * 7. an info (format: info:name,arguments)
- * 8. current date/time (format: date or date:xxx)
- * 9. an environment variable (format: env:XXX)
- * 10. a ternary operator (format: if:condition?value_if_true:value_if_false)
- * 11. an option (format: file.section.option)
- * 12. a buffer local variable
- * 13. a hdata variable (format: hdata.var1.var2 or hdata[list].var1.var2
+ * 5. a string with max chars (format: cut:max,suffix,string)
+ * 6. a regex group captured (format: re:N (0.99) or re:+)
+ * 7. a color (format: color:xxx)
+ * 8. an info (format: info:name,arguments)
+ * 9. current date/time (format: date or date:xxx)
+ * 10. an environment variable (format: env:XXX)
+ * 11. a ternary operator (format: if:condition?value_if_true:value_if_false)
+ * 12. an option (format: file.section.option)
+ * 13. a buffer local variable
+ * 14. a hdata variable (format: hdata.var1.var2 or hdata[list].var1.var2
* or hdata[ptr].var1.var2)
*
* See /help in WeeChat for examples.
@@ -404,7 +405,37 @@ eval_replace_vars_cb (void *data, const char *text)
return (hidden_string) ? hidden_string : strdup ("");
}
- /* 5. regex group captured */
+ /*
+ * 5. cut chars: max number of chars, and add an optional suffix when the
+ * string is cut
+ */
+ if (strncmp (text, "cut:", 4) == 0)
+ {
+ pos = strchr (text + 4, ',');
+ if (!pos)
+ return strdup ("");
+ pos2 = strchr (pos + 1, ',');
+ if (!pos2)
+ return strdup ("");
+ tmp = strndup (text + 4, pos - text - 4);
+ if (!tmp)
+ return strdup ("");
+ number = strtol (tmp, &error, 10);
+ if (!error || error[0] || (number < 0))
+ {
+ free (tmp);
+ return strdup ("");
+ }
+ free (tmp);
+ tmp = strndup (pos + 1, pos2 - pos - 1);
+ if (!tmp)
+ return strdup ("");
+ value = string_cut (pos2 + 1, number, tmp);
+ free (tmp);
+ return value;
+ }
+
+ /* 6. regex group captured */
if (strncmp (text, "re:", 3) == 0)
{
if (eval_regex && eval_regex->result)
@@ -427,7 +458,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ("");
}
- /* 6. color code */
+ /* 7. color code */
if (strncmp (text, "color:", 6) == 0)
{
ptr_value = gui_color_search_config (text + 6);
@@ -437,7 +468,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ((ptr_value) ? ptr_value : "");
}
- /* 7. info */
+ /* 8. info */
if (strncmp (text, "info:", 5) == 0)
{
ptr_value = NULL;
@@ -457,7 +488,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ((ptr_value) ? ptr_value : "");
}
- /* 8. current date/time */
+ /* 9. current date/time */
if ((strncmp (text, "date", 4) == 0) && (!text[4] || (text[4] == ':')))
{
date = time (NULL);
@@ -470,7 +501,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ((rc > 0) ? str_value : "");
}
- /* 9. environment variable */
+ /* 10. environment variable */
if (strncmp (text, "env:", 4) == 0)
{
ptr_value = getenv (text + 4);
@@ -478,7 +509,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (ptr_value);
}
- /* 10: ternary operator: if:condition?value_if_true:value_if_false */
+ /* 11: ternary operator: if:condition?value_if_true:value_if_false */
if (strncmp (text, "if:", 3) == 0)
{
value = NULL;
@@ -542,7 +573,7 @@ eval_replace_vars_cb (void *data, const char *text)
return (value) ? value : strdup ("");
}
- /* 11. option: if found, return this value */
+ /* 12. option: if found, return this value */
if (strncmp (text, "sec.data.", 9) == 0)
{
ptr_value = hashtable_get (secure_hashtable_data, text + 9);
@@ -575,7 +606,7 @@ eval_replace_vars_cb (void *data, const char *text)
}
}
- /* 12. local variable in buffer */
+ /* 13. local variable in buffer */
ptr_buffer = hashtable_get (pointers, "buffer");
if (ptr_buffer)
{
@@ -584,7 +615,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (ptr_value);
}
- /* 13. hdata */
+ /* 14. hdata */
value = NULL;
hdata_name = NULL;
list_name = NULL;
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index cd21a94c7..bd61be900 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -51,6 +51,7 @@
#include "wee-eval.h"
#include "wee-hashtable.h"
#include "wee-utf8.h"
+#include "../gui/gui-chat.h"
#include "../gui/gui-color.h"
#include "../plugins/plugin.h"
@@ -92,6 +93,45 @@ string_strndup (const char *string, int length)
}
/*
+ * Cuts a string after max "length" chars, adds an optional suffix
+ * after the string if it is cut.
+ *
+ * Note: result must be freed after use.
+ */
+
+char *
+string_cut (const char *string, int length, const char *cut_suffix)
+{
+ int length_result, length_cut_suffix;
+ char *result;
+ const char *ptr_string;
+
+ ptr_string = gui_chat_string_add_offset (string, length);
+ if (!ptr_string[0])
+ {
+ /* no cut */
+ return strdup (string);
+ }
+
+ if (cut_suffix && cut_suffix[0])
+ {
+ length_cut_suffix = strlen (cut_suffix);
+ length_result = (ptr_string - string) + length_cut_suffix + 1;
+ result = malloc (length_result);
+ if (!result)
+ return NULL;
+ memcpy (result, string, ptr_string - string);
+ memcpy (result + (ptr_string - string), cut_suffix,
+ length_cut_suffix + 1);
+ return result;
+ }
+ else
+ {
+ return string_strndup (string, ptr_string - string);
+ }
+}
+
+/*
* Converts string to lower case (locale independent).
*/
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index c3d6b933e..51e9631cb 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -36,6 +36,8 @@ struct t_string_dyn
struct t_hashtable;
extern char *string_strndup (const char *string, int length);
+extern char *string_cut (const char *string, int length,
+ const char *cut_suffix);
extern void string_tolower (char *string);
extern void string_toupper (char *string);
extern int string_strcasecmp (const char *string1, const char *string2);
diff --git a/tests/unit/core/test-eval.cpp b/tests/unit/core/test-eval.cpp
index e2ecd401a..92914f8d4 100644
--- a/tests/unit/core/test-eval.cpp
+++ b/tests/unit/core/test-eval.cpp
@@ -218,6 +218,16 @@ TEST(Eval, EvalExpression)
WEE_CHECK_EVAL("********", "${hide:*,password}");
WEE_CHECK_EVAL("\u2603\u2603\u2603", "${hide:${esc:\u2603},abc}");
+ /* test cut of chars */
+ WEE_CHECK_EVAL("", "${cut:0,,}");
+ WEE_CHECK_EVAL("", "${cut:0,+,}");
+ WEE_CHECK_EVAL("", "${cut:0,,test}");
+ WEE_CHECK_EVAL("+", "${cut:0,+,test}");
+ WEE_CHECK_EVAL("te", "${cut:2,,test}");
+ WEE_CHECK_EVAL("te+", "${cut:2,+,test}");
+ WEE_CHECK_EVAL("éà", "${cut:2,,éàô}");
+ WEE_CHECK_EVAL("éà+", "${cut:2,+,éàô}");
+
/* test color */
WEE_CHECK_EVAL(gui_color_get_custom ("green"), "${color:green}");
WEE_CHECK_EVAL(gui_color_get_custom ("*214"), "${color:*214}");