aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/fstring.h
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-10-06 15:24:32 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-10-06 15:24:32 +0100
commitc7f92da47dcb17393132b713f75a4dbd41629819 (patch)
treee8ad8656658ecb9ce99b1ddaad11b99cef56ee22 /src/libutil/fstring.h
parent763ab77075df986497e7b6937d33d5f487ba7fc6 (diff)
downloadrspamd-c7f92da47dcb17393132b713f75a4dbd41629819.tar.gz
rspamd-c7f92da47dcb17393132b713f75a4dbd41629819.zip
Start new fixed strings library.
Diffstat (limited to 'src/libutil/fstring.h')
-rw-r--r--src/libutil/fstring.h144
1 files changed, 56 insertions, 88 deletions
diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h
index 27482877c..9fdf47211 100644
--- a/src/libutil/fstring.h
+++ b/src/libutil/fstring.h
@@ -1,123 +1,91 @@
/*
- * Functions for handling with fixed size strings
+ * Copyright (c) 2009-2015, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+
#ifndef FSTRING_H
#define FSTRING_H
#include "config.h"
#include "mem_pool.h"
-#define update_buf_size(x) (x)->free = (x)->buf->size - \
- ((x)->pos - (x)->buf->begin); (x)->buf->len = (x)->pos - (x)->buf->begin
+/**
+ * Fixed strings library
+ * These strings are NOT null-terminated for speed
+ */
typedef struct f_str_s {
- gchar *begin;
- size_t len;
- size_t size;
+ gsize len;
+ gsize allocated;
+ gchar str[];
} rspamd_fstring_t;
-typedef struct f_str_buf_s {
- rspamd_fstring_t *buf;
- gchar *pos;
- size_t free;
-} rspamd_fstring_buf_t;
-
-typedef struct f_tok_s {
- rspamd_fstring_t word;
- size_t pos;
-} rspamd_fstring_token_t;
-
-/*
- * Search first occurence of character in string
- */
-ssize_t rspamd_fstrchr (rspamd_fstring_t *src, gchar c);
-
-/*
- * Search last occurence of character in string
- */
-ssize_t rspamd_fstrrchr (rspamd_fstring_t *src, gchar c);
-
-/*
- * Search for pattern in orig
- */
-ssize_t rspamd_fstrstr (rspamd_fstring_t *orig, rspamd_fstring_t *pattern);
+typedef struct f_str_tok {
+ gsize len;
+ const gchar *begin;
+} rspamd_ftok_t;
-/*
- * Search for pattern in orig ignoring case
+/**
+ * Create new fixed length string
*/
-ssize_t rspamd_fstrstri (rspamd_fstring_t *orig, rspamd_fstring_t *pattern);
+rspamd_fstring_t* rspamd_fstring_new (void);
-/*
- * Split string by tokens
- * word contains parsed word
+/**
+ * Create new fixed length string with preallocated size
*/
-gint rspamd_fstrtok (rspamd_fstring_t *text, const gchar *sep, rspamd_fstring_token_t *state);
+rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size);
-/*
- * Copy one string into other
+/**
+ * Free fixed length string
*/
-size_t rspamd_fstrcpy (rspamd_fstring_t *dest, rspamd_fstring_t *src);
+void rspamd_fstring_free (rspamd_fstring_t *str);
-/*
- * Concatenate two strings
+/**
+ * Append data to a fixed length string
*/
-size_t rspamd_fstrcat (rspamd_fstring_t *dest, rspamd_fstring_t *src);
+rspamd_fstring_t* rspamd_fstring_append (rspamd_fstring_t *str,
+ const char *in, gsize len) G_GNUC_WARN_UNUSED_RESULT;
-/*
- * Push one character to fstr
- */
-gint rspamd_fstrappend_c (rspamd_fstring_t *dest, gchar c);
-/*
- * Push one character to fstr
+/**
+ * Erase `len` characters at postion `pos`
*/
-gint rspamd_fstrappend_u (rspamd_fstring_t *dest, gunichar c);
+void rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len);
-/*
- * Allocate memory for f_str_t
+/**
+ * Convert fixed string to a zero terminated string. This string should be
+ * freed by a caller
*/
-rspamd_fstring_t * rspamd_fstralloc (rspamd_mempool_t *pool, size_t len);
-
-/*
- * Allocate memory for f_str_t from temporary pool
- */
-rspamd_fstring_t * rspamd_fstralloc_tmp (rspamd_mempool_t *pool, size_t len);
-
-/*
- * Truncate string to its len
- */
-rspamd_fstring_t * rspamd_fstrtruncate (rspamd_mempool_t *pool, rspamd_fstring_t *orig);
-
-/*
- * Enlarge string to new size
- */
-rspamd_fstring_t * rspamd_fstrgrow (rspamd_mempool_t *pool, rspamd_fstring_t *orig, size_t newlen);
-
-/*
- * Return specified character
- */
-#define fstridx(str, pos) *((str)->begin + (pos))
-
-/*
- * Return fast hash value for fixed string
- */
-guint32 rspamd_fstrhash (rspamd_fstring_t *str);
+char * rspamd_fstring_cstr (const rspamd_fstring_t *str);
/*
* Return fast hash value for fixed string converted to lowercase
*/
-guint32 rspamd_fstrhash_lc (rspamd_fstring_t *str, gboolean is_utf);
-/*
- * Make copy of string to 0-terminated string
- */
-gchar * rspamd_fstr_c_str (rspamd_fstring_t *str, rspamd_mempool_t *pool);
+guint32 rspamd_fstrhash_lc (const rspamd_fstring_t *str, gboolean is_utf);
-/*
- * Strip fstr string from space symbols
+/**
+ * Return true if two strings are equal
*/
-void rspamd_fstrstrip (rspamd_fstring_t *str);
-
gboolean rspamd_fstring_equal (const rspamd_fstring_t *s1,
const rspamd_fstring_t *s2);