/pidgin/main: 4a2cf3314f4b: The empty word case was a bad idea

Tomasz Wasilczyk twasilczyk at pidgin.im
Wed Mar 26 09:31:35 EDT 2014


Changeset: 4a2cf3314f4bd0c1c8ec36407162ebb9ea7c5abc
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-03-26 14:31 +0100
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/4a2cf3314f4b

Description:

The empty word case was a bad idea

diffstat:

 libpurple/tests/test_trie.c |   8 +++-----
 libpurple/trie.c            |  25 +++++--------------------
 2 files changed, 8 insertions(+), 25 deletions(-)

diffs (93 lines):

diff --git a/libpurple/tests/test_trie.c b/libpurple/tests/test_trie.c
--- a/libpurple/tests/test_trie.c
+++ b/libpurple/tests/test_trie.c
@@ -96,15 +96,13 @@ START_TEST(test_trie_replace_empty)
 
 	trie = purple_trie_new();
 
-	purple_trie_add(trie, "", (gpointer)0x4001);
-	purple_trie_add(trie, "test", (gpointer)0x4002);
+	purple_trie_add(trie, "test", (gpointer)0x4001);
 
-	in = "the test!";
+	in = "";
 
 	out = purple_trie_replace(trie, in, test_trie_replace_cb, (gpointer)2);
 
-	assert_string_equal("[2:4001][2:4001][2:4001][2:4001][2:4001][2:4001]"
-		"[2:4001][2:4001][2:4001]", out);
+	assert_string_equal("", out);
 
 	g_object_unref(trie);
 	g_free(out);
diff --git a/libpurple/trie.c b/libpurple/trie.c
--- a/libpurple/trie.c
+++ b/libpurple/trie.c
@@ -254,7 +254,6 @@ purple_trie_states_build(PurpleTrie *tri
 	PurpleMemoryPool *reclist_mpool;
 	PurpleTrieRecordList *reclist, *it;
 	gulong cur_len;
-	PurpleTrieRecordList *empty_word = NULL;
 
 	g_return_val_if_fail(priv != NULL, FALSE);
 
@@ -274,14 +273,6 @@ purple_trie_states_build(PurpleTrie *tri
 	 * node -- the prefix of the word with len of cur_len */
 	for (it = reclist; it != NULL; it = it->next) {
 		it->extra_data = root;
-		if (it->rec->word_len == 0)
-			empty_word = it;
-	}
-
-	/* a special case for the empty word */
-	if (empty_word) {
-		root->found_word = empty_word->rec;
-		reclist = purple_record_list_remove(reclist, empty_word);
 	}
 
 	/* Iterate over indexes of words -- every loop iteration checks certain
@@ -294,13 +285,7 @@ purple_trie_states_build(PurpleTrie *tri
 			PurpleTrieState *prefix = it->extra_data;
 			PurpleTrieState *lon_suf_parent;
 
-			if (character == '\0') {
-				purple_debug_warning("trie", "found "
-					"a collision of empty words");
-				/* it->next is still valid, see below */
-				reclist = purple_record_list_remove(reclist, it);
-				continue;
-			}
+			g_assert(character != '\0');
 
 			if (prefix->children && prefix->children[character]) {
 				/* Word's prefix is already in the trie, added
@@ -414,11 +399,9 @@ purple_trie_replace(PurpleTrie *trie, co
 			gsize str_old_len;
 
 			/* let's get back to the beginning of the word */
+			g_assert(out->len >= state->found_word->word_len - 1);
 			str_old_len = out->len;
-			if (state->found_word->word_len > 0) {
-				g_assert(out->len >= state->found_word->word_len - 1);
-				out->len -= state->found_word->word_len - 1;
-			}
+			out->len -= state->found_word->word_len - 1;
 
 			was_replaced = replace_cb(out, state->found_word->word,
 				state->found_word->data, user_data);
@@ -454,6 +437,7 @@ purple_trie_add(PurpleTrie *trie, const 
 
 	g_return_if_fail(priv != NULL);
 	g_return_if_fail(word != NULL);
+	g_return_if_fail(word[0] != '\0');
 
 	/* Every change in a trie invalidates longest_suffix map.
 	 * These prefixes could be updated instead of cleaning the whole graph.
@@ -464,6 +448,7 @@ purple_trie_add(PurpleTrie *trie, const 
 		sizeof(PurpleTrieRecord), sizeof(gpointer));
 	rec->word = purple_memory_pool_strdup(priv->records_str_mempool, word);
 	rec->word_len = strlen(word);
+	g_assert(rec->word_len > 0);
 	rec->data = data;
 
 	priv->records = purple_record_list_prepend(priv->records_obj_mempool,



More information about the Commits mailing list