summaryrefslogtreecommitdiff
path: root/camel/camel-string-utils.c
blob: f9f584bf29c7db29e7744844a992e036b9345e56 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Jeffrey Stedfast <fejj@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "camel-string-utils.h"

gint
camel_strcase_equal (gconstpointer a,
                     gconstpointer b)
{
	return (g_ascii_strcasecmp ((const gchar *) a, (const gchar *) b) == 0);
}

guint
camel_strcase_hash (gconstpointer v)
{
	const gchar *p = (gchar *) v;
	guint h = 0, g;

	for (; *p != '\0'; p++) {
		h = (h << 4) + g_ascii_toupper (*p);
		if ((g = h & 0xf0000000)) {
			h = h ^ (g >> 24);
			h = h ^ g;
		}
	}

	return h;
}

gchar *
camel_strstrcase (const gchar *haystack,
                  const gchar *needle)
{
	/* find the needle in the haystack neglecting case */
	const gchar *ptr;
	guint len;

	g_return_val_if_fail (haystack != NULL, NULL);
	g_return_val_if_fail (needle != NULL, NULL);

	len = strlen (needle);
	if (len > strlen (haystack))
		return NULL;

	if (len == 0)
		return (gchar *) haystack;

	for (ptr = haystack; *(ptr + len - 1) != '\0'; ptr++)
		if (!g_ascii_strncasecmp (ptr, needle, len))
			return (gchar *) ptr;

	return NULL;
}

const gchar *
camel_strdown (gchar *str)
{
	register gchar *s = str;

	while (*s) {
		if (*s >= 'A' && *s <= 'Z')
			*s += 0x20;
		s++;
	}

	return str;
}

/* working stuff for pstrings */
static GMutex string_pool_lock;
static GHashTable *string_pool = NULL;

typedef struct _StringPoolNode StringPoolNode;

struct _StringPoolNode {
	gchar *string;
	gulong ref_count;
};

static StringPoolNode *
string_pool_node_new (gchar *string)
{
	StringPoolNode *node;

	node = g_slice_new (StringPoolNode);
	node->string = string;  /* takes ownership */
	node->ref_count = 1;

	return node;
}

static void
string_pool_node_free (StringPoolNode *node)
{
	g_free (node->string);

	g_slice_free (StringPoolNode, node);
}

static guint
string_pool_node_hash (const StringPoolNode *node)
{
	return g_str_hash (node->string);
}

static gboolean
string_pool_node_equal (const StringPoolNode *node_a,
                        const StringPoolNode *node_b)
{
	return g_str_equal (node_a->string, node_b->string);
}

static void
string_pool_init (void)
{
	if (G_UNLIKELY (string_pool == NULL))
		string_pool = g_hash_table_new_full (
			(GHashFunc) string_pool_node_hash,
			(GEqualFunc) string_pool_node_equal,
			(GDestroyNotify) string_pool_node_free,
			(GDestroyNotify) NULL);
}

/**
 * camel_pstring_add:
 * @string: string to add to the string pool
 * @own: whether the string pool will own the memory pointed to by
 *       @string, if @string is not yet in the pool
 *
 * Add @string to the pool.
 *
 * The %NULL and empty strings are special cased to constant values.
 *
 * Unreference the returned string with camel_pstring_free().
 *
 * Returns: a canonicalized copy of @string
 **/
const gchar *
camel_pstring_add (gchar *string,
                   gboolean own)
{
	StringPoolNode static_node = { string, };
	StringPoolNode *node;
	const gchar *interned;

	if (string == NULL)
		return NULL;

	if (*string == '\0') {
		if (own)
			g_free (string);
		return "";
	}

	g_mutex_lock (&string_pool_lock);

	string_pool_init ();

	node = g_hash_table_lookup (string_pool, &static_node);

	if (node != NULL) {
		node->ref_count++;
		if (own)
			g_free (string);
	} else {
		if (!own)
			string = g_strdup (string);
		node = string_pool_node_new (string);
		g_hash_table_add (string_pool, node);
	}

	interned = node->string;

	g_mutex_unlock (&string_pool_lock);

	return interned;
}

/**
 * camel_pstring_peek:
 * @string: string to fetch from the string pool
 *
 * Returns the canonicalized copy of @string without increasing its
 * reference count in the string pool.  If necessary, @string is first
 * added to the string pool.
 *
 * The %NULL and empty strings are special cased to constant values.
 *
 * Returns: a canonicalized copy of @string
 *
 * Since: 2.24
 **/
const gchar *
camel_pstring_peek (const gchar *string)
{
	StringPoolNode static_node = { (gchar *) string, };
	StringPoolNode *node;
	const gchar *interned;

	if (string == NULL)
		return NULL;

	if (*string == '\0')
		return "";

	g_mutex_lock (&string_pool_lock);

	string_pool_init ();

	node = g_hash_table_lookup (string_pool, &static_node);

	if (node == NULL) {
		node = string_pool_node_new (g_strdup (string));
		g_hash_table_add (string_pool, node);
	}

	interned = node->string;

	g_mutex_unlock (&string_pool_lock);

	return interned;
}

/**
 * camel_pstring_strdup:
 * @string: string to copy
 *
 * Create a new pooled string entry for @strings.  A pooled string
 * is a table where common strings are canonicalized.  They are also
 * reference counted and freed when no longer referenced.
 *
 * The %NULL and empty strings are special cased to constant values.
 *
 * Unreference the returned string with camel_pstring_free().
 *
 * Returns: a canonicalized copy of @string
 **/
const gchar *
camel_pstring_strdup (const gchar *string)
{
	return camel_pstring_add ((gchar *) string, FALSE);
}

/**
 * camel_pstring_free:
 * @string: string to free
 *
 * Unreferences a pooled string.  If the string's reference count drops to
 * zero it will be deallocated.  %NULL and the empty string are special cased.
 **/
void
camel_pstring_free (const gchar *string)
{
	StringPoolNode static_node = { (gchar *) string, };
	StringPoolNode *node;

	if (string_pool == NULL)
		return;

	if (string == NULL || *string == '\0')
		return;

	g_mutex_lock (&string_pool_lock);

	node = g_hash_table_lookup (string_pool, &static_node);

	if (node == NULL) {
		g_warning ("%s: String not in pool: %s", G_STRFUNC, string);
	} else if (node->string != string) {
		g_warning ("%s: String is not ours: %s", G_STRFUNC, string);
	} else if (node->ref_count == 0) {
		g_warning ("%s: Orphaned pool node: %s", G_STRFUNC, string);
	} else {
		node->ref_count--;
		if (node->ref_count == 0)
			g_hash_table_remove (string_pool, node);
	}

	g_mutex_unlock (&string_pool_lock);
}

/**
 * camel_pstring_dump_stat:
 *
 * Dumps to stdout memory statistic about the string pool.
 *
 * Since: 3.6
 **/
void
camel_pstring_dump_stat (void)
{
	g_mutex_lock (&string_pool_lock);

	g_print ("   String Pool Statistics: ");

	if (string_pool == NULL) {
		g_print ("Not used yet\n");
	} else {
		GHashTableIter iter;
		gchar *format_size;
		guint64 bytes = 0;
		gpointer key;

		g_hash_table_iter_init (&iter, string_pool);

		while (g_hash_table_iter_next (&iter, &key, NULL))
			bytes += strlen (((StringPoolNode *) key)->string);

		format_size = g_format_size_full (
			bytes, G_FORMAT_SIZE_LONG_FORMAT);

		g_print (
			"Holds %d strings totaling %s\n",
			g_hash_table_size (string_pool),
			format_size);

		g_free (format_size);
	}

	g_mutex_unlock (&string_pool_lock);
}