From: Vsevolod Stakhov Date: Wed, 7 Oct 2015 10:44:15 +0000 (+0100) Subject: Add fixed strings assigning utility function. X-Git-Tag: 1.0.5~66^2~21 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=60ce496100bbf7316c392a9b2b0a072edb6cfe07;p=rspamd.git Add fixed strings assigning utility function. --- diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index a03121f42..6566f5af9 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -71,6 +71,24 @@ rspamd_fstring_new_init (const gchar *init, gsize len) return s; } +rspamd_fstring_t * +rspamd_fstring_assign (rspamd_fstring_t *str, const gchar *init, gsize len) +{ + gsize avail = str->allocated; + + if (avail < len) { + str = rspamd_fstring_grow (str, len); + } + + if (len > 0) { + memcpy (str->str, init, len); + } + + str->len = len; + + return str; +} + void rspamd_fstring_free (rspamd_fstring_t *str) { @@ -312,4 +330,4 @@ rspamd_fstring_cmp (const rspamd_fstring_t *s1, } return s1->len - s2->len; -} \ No newline at end of file +} diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h index 70d6624d1..4d9c3678d 100644 --- a/src/libutil/fstring.h +++ b/src/libutil/fstring.h @@ -48,17 +48,26 @@ typedef struct f_str_tok { /** * Create new fixed length string */ -rspamd_fstring_t* rspamd_fstring_new (void); +rspamd_fstring_t* rspamd_fstring_new (void) + G_GNUC_WARN_UNUSED_RESULT; /** * Create new fixed length string with preallocated size */ -rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size); +rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size) + G_GNUC_WARN_UNUSED_RESULT; /** * Create new fixed length string and initialize it with the initial data */ -rspamd_fstring_t *rspamd_fstring_new_init (const gchar *init, gsize len); +rspamd_fstring_t *rspamd_fstring_new_init (const gchar *init, gsize len) + G_GNUC_WARN_UNUSED_RESULT; + +/** + * Assign new value to fixed string + */ +rspamd_fstring_t *rspamd_fstring_assign (rspamd_fstring_t *str, + const gchar *init, gsize len) G_GNUC_WARN_UNUSED_RESULT; /** * Free fixed length string @@ -81,7 +90,8 @@ void rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len); * Convert fixed string to a zero terminated string. This string should be * freed by a caller */ -char * rspamd_fstring_cstr (const rspamd_fstring_t *str); +char * rspamd_fstring_cstr (const rspamd_fstring_t *str) + G_GNUC_WARN_UNUSED_RESULT; /* * Return fast hash value for fixed string converted to lowercase @@ -100,6 +110,9 @@ gboolean rspamd_fstring_equal (const rspamd_fstring_t *s1, gint rspamd_fstring_casecmp (const rspamd_fstring_t *s1, const rspamd_fstring_t *s2); +/** + * Compare two fixed strings + */ gint rspamd_fstring_cmp (const rspamd_fstring_t *s1, const rspamd_fstring_t *s2);