diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2018-06-02 15:48:31 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2018-06-02 16:01:14 +0200 |
commit | 1abf4040f11ecf626dc79a3d0fb810b8efd6252e (patch) | |
tree | d561ec2e482eef9b8b9981edf2231e3a1b03486c /src/core | |
parent | ab9a0ec2e63d47a3663b08690848ec7b87d3d4fe (diff) | |
download | weechat-1abf4040f11ecf626dc79a3d0fb810b8efd6252e.zip |
core: add reverse of string in evaluation of expressions with "rev:" (closes #1200)
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-command.c | 17 | ||||
-rw-r--r-- | src/core/wee-eval.c | 43 | ||||
-rw-r--r-- | src/core/wee-string.c | 40 | ||||
-rw-r--r-- | src/core/wee-string.h | 1 |
4 files changed, 75 insertions, 26 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index e02968bb2..ff7821ccc 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -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" diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 2cba33a22..149344975 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -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; diff --git a/src/core/wee-string.c b/src/core/wee-string.c index f1231bb6b..6a2939da9 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -163,6 +163,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). */ diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 9d7257e70..1679a33ad 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -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); |