1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 14:26:39 +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
+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).
*/