1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-03 02:05:41 +02:00

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.
This commit is contained in:
Sébastien Helleu
2017-03-14 07:25:03 +01:00
parent db0ecc07fe
commit 9a8ec36cbd
8 changed files with 143 additions and 27 deletions
+10 -2
View File
@@ -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. |
+11 -3
View File
@@ -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é. |
+10 -2
View File
@@ -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. |
+11 -2
View File
@@ -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` =
キャプチャされたグループ、`+++` = 最後にキャプチャされたグループ |
+49 -18
View File
File diff suppressed because it is too large Load Diff
+40
View File
@@ -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"
@@ -91,6 +92,45 @@ string_strndup (const char *string, int length)
return result;
}
/*
* 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).
*/
+2
View File
@@ -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);
+10
View File
@@ -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}");