summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2019-10-12 20:14:36 +0200
committerSébastien Helleu <flashcode@flashtux.org>2019-10-12 20:14:36 +0200
commit8fc8f728d49110ac44d403977f42afbdaf9d371e (patch)
treebbda7dc06b38f535bfeed53c6ecc9d245eab04b2 /src/core
parent9535f4a70be7fa67bf65b4dc88b9681b2b755c4c (diff)
downloadweechat-8fc8f728d49110ac44d403977f42afbdaf9d371e.zip
core: add reverse of string for screen in evaluation of expressions with "revscr:"
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-command.c2
-rw-r--r--src/core/wee-eval.c7
-rw-r--r--src/core/wee-string.c60
-rw-r--r--src/core/wee-string.h1
4 files changed, 66 insertions, 4 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index f1d1b43bf..85939a89c 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -7353,7 +7353,7 @@ command_init ()
" or max chars displayed on screen "
"(format: \"cutscr:max,suffix,string\" or "
"\"cutscr:+max,suffix,string\")\n"
- " 5. a reversed string (format: \"rev:xxx\")\n"
+ " 5. a reversed string (format: \"rev:xxx\" or \"revscr:xxx\")\n"
" 6. a repeated string (format: \"repeat:count,string\")\n"
" 7. length of a string (format: \"length:xxx\" or "
"\"lengthscr:xxx\")\n"
diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c
index ab9cc8405..1bcd96a4b 100644
--- a/src/core/wee-eval.c
+++ b/src/core/wee-eval.c
@@ -295,7 +295,8 @@ 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 reversed string (format: rev:xxx)
+ * 6. a reversed string (format: rev:xxx) or reversed string for screen,
+ * color codes are not reversed (format: revscr:xxx)
* 7. a repeated string (format: repeat:count,string)
* 8. length of a string (format: length:xxx) or length of a string on screen
* (format: lengthscr:xxx); color codes are ignored
@@ -459,9 +460,9 @@ eval_replace_vars_cb (void *data, const char *text)
/* 6. reverse string */
if (strncmp (text, "rev:", 4) == 0)
- {
return string_reverse (text + 4);
- }
+ if (strncmp (text, "revscr:", 7) == 0)
+ return string_reverse_screen (text + 7);
/* 7. repeated string */
if (strncmp (text, "repeat:", 7) == 0)
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 5025958a1..3b082d57f 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -204,6 +204,66 @@ string_reverse (const char *string)
}
/*
+ * Reverses a string for screen: color codes are not reversed.
+ * For example: reverse of "<red>test" is "test<red>" where the color code
+ * "<red>" is kept as-is, so it is still valid (this is not the case with
+ * function string_reverse).
+ *
+ * Note: result must be freed after use.
+ */
+
+char *
+string_reverse_screen (const char *string)
+{
+ int length, color_size, char_size;
+ const char *ptr_string, *ptr_next;
+ 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])
+ {
+ ptr_next = gui_chat_string_next_char (NULL, NULL,
+ (const unsigned char *)ptr_string,
+ 0, 0, 0);
+ if (!ptr_next)
+ ptr_next = ptr_string + strlen (ptr_string);
+ color_size = ptr_next - ptr_string;
+ if (color_size > 0)
+ {
+ /* add the color code as-is */
+ ptr_result -= color_size;
+ memcpy (ptr_result, ptr_string, color_size);
+ ptr_string += color_size;
+ }
+
+ if (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;
+}
+
+/*
* Repeats a string a given number of times.
*
* Note: result must be freed after use.
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index ef34af417..7eaef061e 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_reverse_screen (const char *string);
extern char *string_repeat (const char *string, int count);
extern void string_tolower (char *string);
extern void string_toupper (char *string);