diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2018-10-09 21:12:02 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2018-10-09 21:12:02 +0200 |
commit | 8da3458f4cb6c806dc399bf828f97ad60dadaf1d (patch) | |
tree | 0971f7567b96e247047a0a4bc4e022c1e9e356e1 /src/core | |
parent | 82697714e1b51968dc852c9194deb2e0bf159596 (diff) | |
download | weechat-8da3458f4cb6c806dc399bf828f97ad60dadaf1d.zip |
core: add repeat of string in evaluation of expressions with "repeat:count,string" (closes #958)
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-command.c | 19 | ||||
-rw-r--r-- | src/core/wee-eval.c | 56 | ||||
-rw-r--r-- | src/core/wee-string.c | 39 | ||||
-rw-r--r-- | src/core/wee-string.h | 1 |
4 files changed, 89 insertions, 26 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index a24f131d0..934f3006a 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -7404,17 +7404,18 @@ command_init () "(format: \"cutscr:max,suffix,string\" or " "\"cutscr:+max,suffix,string\")\n" " 5. a reversed string (format: \"rev:xxx\")\n" - " 6. a color (format: \"color:xxx\", see \"Plugin API " + " 6. a repeated string (format: \"repeat:count,string\")\n" + " 7. a color (format: \"color:xxx\", see \"Plugin API " "reference\", function \"color\")\n" - " 7. an info (format: \"info:name,arguments\", arguments are " + " 8. an info (format: \"info:name,arguments\", arguments are " "optional)\n" - " 8. current date/time (format: \"date\" or \"date:format\")\n" - " 9. an environment variable (format: \"env:XXX\")\n" - " 10. a ternary operator (format: " + " 9. current date/time (format: \"date\" or \"date:format\")\n" + " 10. an environment variable (format: \"env:XXX\")\n" + " 11. a ternary operator (format: " "\"if:condition?value_if_true:value_if_false\")\n" - " 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 " + " 12. an option (format: \"file.section.option\")\n" + " 13. a local variable in buffer\n" + " 14. 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" @@ -7448,6 +7449,8 @@ command_init () " /eval -n ${cut:+3,+,test} ==> te+\n" " /eval -n ${date:%H:%M:%S} ==> 07:46:40\n" " /eval -n ${if:${info:term_width}>80?big:small} ==> big\n" + " /eval -n ${rev:Hello} ==> olleH\n" + " /eval -n ${repeat:5,-} ==> -----\n" "\n" "Examples (conditions):\n" " /eval -n -c ${window.buffer.number} > 2 ==> 0\n" diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 9ba2dd9d2..c7037a455 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -294,15 +294,16 @@ end: * cut:+max,suffix,string) or max chars on screen * (format: cutscr:max,suffix,string or cutscr:+max,suffix,string) * 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 + * 7. a repeated string (format: repeat:count,string) + * 8. a regex group captured (format: re:N (0.99) or re:+) + * 9. a color (format: color:xxx) + * 10. an info (format: info:name,arguments) + * 11. current date/time (format: date or date:xxx) + * 12. an environment variable (format: env:XXX) + * 13. a ternary operator (format: if:condition?value_if_true:value_if_false) + * 14. an option (format: file.section.option) + * 15. a buffer local variable + * 16. a hdata variable (format: hdata.var1.var2 or hdata[list].var1.var2 * or hdata[ptr].var1.var2) * * See /help in WeeChat for examples. @@ -456,7 +457,26 @@ eval_replace_vars_cb (void *data, const char *text) return string_reverse (text + 4); } - /* 7. regex group captured */ + /* 7. repeated string */ + if (strncmp (text, "repeat:", 7) == 0) + { + pos = strchr (text + 7, ','); + if (!pos) + return strdup (""); + tmp = strndup (text + 7, pos - text - 7); + if (!tmp) + return strdup (""); + number = strtol (tmp, &error, 10); + if (!error || error[0] || (number < 0)) + { + free (tmp); + return strdup (""); + } + free (tmp); + return string_repeat (pos + 1, number); + } + + /* 8. regex group captured */ if (strncmp (text, "re:", 3) == 0) { if (eval_context->regex && eval_context->regex->result) @@ -487,7 +507,7 @@ eval_replace_vars_cb (void *data, const char *text) return strdup (""); } - /* 8. color code */ + /* 9. color code */ if (strncmp (text, "color:", 6) == 0) { ptr_value = gui_color_search_config (text + 6); @@ -497,7 +517,7 @@ eval_replace_vars_cb (void *data, const char *text) return strdup ((ptr_value) ? ptr_value : ""); } - /* 9. info */ + /* 10. info */ if (strncmp (text, "info:", 5) == 0) { ptr_value = NULL; @@ -517,7 +537,7 @@ eval_replace_vars_cb (void *data, const char *text) return strdup ((ptr_value) ? ptr_value : ""); } - /* 10. current date/time */ + /* 11. current date/time */ if ((strncmp (text, "date", 4) == 0) && (!text[4] || (text[4] == ':'))) { date = time (NULL); @@ -530,7 +550,7 @@ eval_replace_vars_cb (void *data, const char *text) return strdup ((rc > 0) ? str_value : ""); } - /* 11. environment variable */ + /* 12. environment variable */ if (strncmp (text, "env:", 4) == 0) { ptr_value = getenv (text + 4); @@ -538,7 +558,7 @@ eval_replace_vars_cb (void *data, const char *text) return strdup (ptr_value); } - /* 12: ternary operator: if:condition?value_if_true:value_if_false */ + /* 13: ternary operator: if:condition?value_if_true:value_if_false */ if (strncmp (text, "if:", 3) == 0) { value = NULL; @@ -605,7 +625,7 @@ eval_replace_vars_cb (void *data, const char *text) return (value) ? value : strdup (""); } - /* 13. option: if found, return this value */ + /* 14. option: if found, return this value */ if (strncmp (text, "sec.data.", 9) == 0) { ptr_value = hashtable_get (secure_hashtable_data, text + 9); @@ -638,7 +658,7 @@ eval_replace_vars_cb (void *data, const char *text) } } - /* 14. local variable in buffer */ + /* 15. local variable in buffer */ ptr_buffer = hashtable_get (eval_context->pointers, "buffer"); if (ptr_buffer) { @@ -647,7 +667,7 @@ eval_replace_vars_cb (void *data, const char *text) return strdup (ptr_value); } - /* 15. hdata */ + /* 16. hdata */ value = NULL; hdata_name = NULL; list_name = NULL; diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 8770d1eb2..9c525ff6c 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -203,6 +203,45 @@ string_reverse (const char *string) } /* + * Repeats a string a given number of times. + * + * Note: result must be freed after use. + */ + +char * +string_repeat (const char *string, int count) +{ + int length_string, length_result, i; + char *result; + + if (!string) + return NULL; + + if (!string[0] || (count <= 0)) + return strdup (""); + + if (count == 1) + return strdup (string); + + length_string = strlen (string); + length_result = (length_string * count) + 1; + result = malloc (length_result); + if (!result) + return NULL; + + i = 0; + while (count > 0) + { + memcpy (result + i, string, length_string); + count--; + i += length_string; + } + result[length_result - 1] = '\0'; + + return result; +} + +/* * Converts string to lower case (locale independent). */ diff --git a/src/core/wee-string.h b/src/core/wee-string.h index d891b17dd..605aa1598 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -40,6 +40,7 @@ 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 char *string_repeat (const char *string, int count); extern void string_tolower (char *string); extern void string_toupper (char *string); extern int string_strcasecmp (const char *string1, const char *string2); |