1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
* Copyright (c) 2002-2012 Balabit
* Copyright (c) 1998-2012 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#ifndef STR_UTILS_H_INCLUDED
#define STR_UTILS_H_INCLUDED 1
#include "syslog-ng.h"
#include <string.h>
/* functions that should be implemented by GLib but they aren't */
GString *g_string_assign_len(GString *s, const gchar *val, gint len);
gchar *g_string_steal(GString *s);
static inline GString *
g_string_append_unichar_optimized(GString *string, gunichar wc)
{
if (wc < 0x80)
g_string_append_c(string, (gchar) wc);
else
g_string_append_unichar(string, wc);
return string;
}
/*
* DO NOT USE THIS MACRO UNLESS STRICTLY NECESSARY FOR PERFORMANCE REASONS.
*
* This macro tries hard to zero-terminate the string without moving it to a
* new buffer.
*
* It is expected to be used at sites where the length is known and where
* the input string is expected to be properly NUL terminated but not always.
*
* The macro checks if the string is indeed NUL terminated and if it is it
* just assigns the original pointer to dest. If it's not, it allocates a
* new string via g_alloca() so it is automatically freed when the current
* scope exits.
*
* This does not work as an inline function as we use g_alloca() and that
* would be freed when the inline function exits (e.g. when it is in fact
* not inlined).
*/
#define APPEND_ZERO(dest, value, value_len) \
do { \
gchar *__buf; \
if (G_UNLIKELY(value[value_len] != 0)) \
{ \
/* value is NOT zero terminated */ \
\
__buf = g_alloca(value_len + 1); \
memcpy(__buf, value, value_len); \
__buf[value_len] = 0; \
} \
else \
{ \
/* value is zero terminated */ \
__buf = (gchar *) value; \
} \
dest = __buf; \
} while (0)
gchar *__normalize_key(const gchar *buffer);
gchar *normalize_flag(const gchar *buffer);
/* This version of strchr() is optimized for cases where the string we are
* looking up characters in is often zero or one character in length. In
* those cases we can avoid the strchr() call, which can make a tight loop
* doing a lot of strchr() calls a lot faster, especially as this function
* is inlined.
*
* Naming: I originally wanted to use the original function as a prefix
* (strchr), however that would potentially pollute the namespace of the
* library that we are optimizing. So I've added an underscore prefix in
* order not to clash, with that it is still obvious that it intends to
* behave the same as strchr().
*
* NOTE: don't use this unless strchr() really shows up in your profile.
*/
static inline const char *
_strchr_optimized_for_single_char_haystack(const char *str, int c)
{
if (str[0] == c)
return str;
else if (str[0] == '\0')
return NULL;
if (str[1] == '\0')
{
if (c != '\0')
return NULL;
else
return &str[1];
}
return strchr(str + 1, c);
}
/*
* strsplit() splits the `str` into `maxtokens` pieces.
* This version skips multiple `delims`.
*
*/
static inline
gchar **strsplit(const gchar *str, char delim, gint maxtokens)
{
if (!str || delim == '\0')
return NULL;
const gchar *delim_pos = NULL;
const gchar *remainder = str;
GPtrArray *array = g_ptr_array_new();
if (maxtokens < 1)
maxtokens = G_MAXINT;
for (delim_pos = strchr(remainder, delim); delim_pos && maxtokens; maxtokens--)
{
const gchar *d = delim_pos;
while (d && *d++ == delim);
if (!d)
break;
--d;
gint len = delim_pos - remainder;
if (len > 0)
g_ptr_array_add(array, g_strndup(remainder, len));
remainder = d;
delim_pos = strchr(remainder, delim);
}
if (remainder)
g_ptr_array_add(array, g_strdup(remainder));
g_ptr_array_add(array, NULL);
return (gchar **) g_ptr_array_free(array, FALSE);
}
#endif
|