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

core: add reverse of string in evaluation of expressions with "rev:" (closes #1200)

This commit is contained in:
Sébastien Helleu
2018-06-02 15:48:31 +02:00
parent ab9a0ec2e6
commit 1abf4040f1
30 changed files with 425 additions and 295 deletions
+9 -8
View File
@@ -7392,17 +7392,18 @@ command_init ()
" or max chars displayed on screen "
"(format: \"cutscr:max,suffix,string\" or "
"\"cutscr:+max,suffix,string\")\n"
" 5. a color (format: \"color:xxx\", see \"Plugin API "
" 5. a reversed string (format: \"rev:xxx\")\n"
" 6. a color (format: \"color:xxx\", see \"Plugin API "
"reference\", function \"color\")\n"
" 6. an info (format: \"info:name,arguments\", arguments are "
" 7. an info (format: \"info:name,arguments\", arguments are "
"optional)\n"
" 7. current date/time (format: \"date\" or \"date:format\")\n"
" 8. an environment variable (format: \"env:XXX\")\n"
" 9. a ternary operator (format: "
" 8. current date/time (format: \"date\" or \"date:format\")\n"
" 9. an environment variable (format: \"env:XXX\")\n"
" 10. a ternary operator (format: "
"\"if:condition?value_if_true:value_if_false\")\n"
" 10. an option (format: \"file.section.option\")\n"
" 11. a local variable in buffer\n"
" 12. a hdata name/variable (the value is automatically converted "
" 11. an option (format: \"file.section.option\")\n"
" 12. a local variable in buffer\n"
" 13. a hdata name/variable (the value is automatically converted "
"to string), by default \"window\" and \"buffer\" point to current "
"window/buffer.\n"
"Format for hdata can be one of following:\n"
+25 -18
View File
@@ -293,15 +293,16 @@ end:
* 5. a string with max chars (format: cut:max,suffix,string or
* cut:+max,suffix,string) or max chars on screen
* (format: cutscr:max,suffix,string or cutscr:+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
* 6. a reversed string (format: rev:xxx)
* 7. a regex group captured (format: re:N (0.99) or re:+)
* 8. a color (format: color:xxx)
* 9. an info (format: info:name,arguments)
* 10. current date/time (format: date or date:xxx)
* 11. an environment variable (format: env:XXX)
* 12. a ternary operator (format: if:condition?value_if_true:value_if_false)
* 13. an option (format: file.section.option)
* 14. a buffer local variable
* 15. a hdata variable (format: hdata.var1.var2 or hdata[list].var1.var2
* or hdata[ptr].var1.var2)
*
* See /help in WeeChat for examples.
@@ -448,7 +449,13 @@ eval_replace_vars_cb (void *data, const char *text)
return value;
}
/* 6. regex group captured */
/* 6. reverse string */
if (strncmp (text, "rev:", 4) == 0)
{
return string_reverse (text + 4);
}
/* 7. regex group captured */
if (strncmp (text, "re:", 3) == 0)
{
if (eval_context->regex && eval_context->regex->result)
@@ -479,7 +486,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ("");
}
/* 7. color code */
/* 8. color code */
if (strncmp (text, "color:", 6) == 0)
{
ptr_value = gui_color_search_config (text + 6);
@@ -489,7 +496,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ((ptr_value) ? ptr_value : "");
}
/* 8. info */
/* 9. info */
if (strncmp (text, "info:", 5) == 0)
{
ptr_value = NULL;
@@ -509,7 +516,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ((ptr_value) ? ptr_value : "");
}
/* 9. current date/time */
/* 10. current date/time */
if ((strncmp (text, "date", 4) == 0) && (!text[4] || (text[4] == ':')))
{
date = time (NULL);
@@ -522,7 +529,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup ((rc > 0) ? str_value : "");
}
/* 10. environment variable */
/* 11. environment variable */
if (strncmp (text, "env:", 4) == 0)
{
ptr_value = getenv (text + 4);
@@ -530,7 +537,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (ptr_value);
}
/* 11: ternary operator: if:condition?value_if_true:value_if_false */
/* 12: ternary operator: if:condition?value_if_true:value_if_false */
if (strncmp (text, "if:", 3) == 0)
{
value = NULL;
@@ -594,7 +601,7 @@ eval_replace_vars_cb (void *data, const char *text)
return (value) ? value : strdup ("");
}
/* 12. option: if found, return this value */
/* 13. option: if found, return this value */
if (strncmp (text, "sec.data.", 9) == 0)
{
ptr_value = hashtable_get (secure_hashtable_data, text + 9);
@@ -627,7 +634,7 @@ eval_replace_vars_cb (void *data, const char *text)
}
}
/* 13. local variable in buffer */
/* 14. local variable in buffer */
ptr_buffer = hashtable_get (eval_context->pointers, "buffer");
if (ptr_buffer)
{
@@ -636,7 +643,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (ptr_value);
}
/* 14. hdata */
/* 15. hdata */
value = NULL;
hdata_name = NULL;
list_name = NULL;
+40
View File
@@ -162,6 +162,46 @@ string_cut (const char *string, int length, int count_suffix, int screen,
}
}
/*
* Reverses a string.
*
* Note: result must be freed after use.
*/
char *
string_reverse (const char *string)
{
int length, char_size;
const char *ptr_string;
char *result, *ptr_result;
if (!string)
return NULL;
if (!string[0])
return strdup (string);
length = strlen (string);
result = malloc (length + 1);
if (!result)
return NULL;
ptr_string = string;
ptr_result = result + length;
ptr_result[0] = '\0';
while (ptr_string && ptr_string[0])
{
char_size = utf8_char_size (ptr_string);
ptr_result -= char_size;
memcpy (ptr_result, ptr_string, char_size);
ptr_string += char_size;
}
return result;
}
/*
* Converts string to lower case (locale independent).
*/
+1
View File
@@ -39,6 +39,7 @@ struct t_hashtable;
extern char *string_strndup (const char *string, int length);
extern char *string_cut (const char *string, int length, int count_suffix,
int screen, const char *cut_suffix);
extern char *string_reverse (const char *string);
extern void string_tolower (char *string);
extern void string_toupper (char *string);
extern int string_strcasecmp (const char *string1, const char *string2);