/pidgin/main: f97feb9a67f2: Trie: better comments, fix a small f...

Tomasz Wasilczyk twasilczyk at pidgin.im
Tue Mar 25 20:48:48 EDT 2014


Changeset: f97feb9a67f249204a15c6d2e0dffbfb185ec06c
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-03-26 01:48 +0100
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/f97feb9a67f2

Description:

Trie: better comments, fix a small flaw in algorithm

diffstat:

 libpurple/trie.c |  20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)

diffs (53 lines):

diff --git a/libpurple/trie.c b/libpurple/trie.c
--- a/libpurple/trie.c
+++ b/libpurple/trie.c
@@ -331,6 +331,7 @@ purple_trie_replace(PurpleTrie *trie, co
 	state = priv->root_state;
 	while (src[i] != '\0') {
 		guchar character = src[i];
+		gboolean copy_char;
 
 		/* change state after processing a character */
 		while (TRUE) {
@@ -345,10 +346,12 @@ purple_trie_replace(PurpleTrie *trie, co
 			if (state == priv->root_state)
 				break;
 
+			/* Let's try a bit shorter suffix. */
 			state = state->longest_suffix;
 		}
 
 		/* if we reached a "found" state, let's process it */
+		copy_char = FALSE;
 		if (state->found_word) {
 			gboolean was_replaced;
 
@@ -356,12 +359,27 @@ purple_trie_replace(PurpleTrie *trie, co
 				state->found_word->data, user_data);
 
 			if (was_replaced || priv->reset_on_match) {
+				if (!was_replaced) {
+					g_string_append_len(out,
+						state->found_word->word,
+						state->found_word->word_len);
+				}
+
+				/* skip a whole word, not a character */
 				i += state->found_word->word_len;
+
 				state = priv->root_state;
 			} else
-				i++;
+				copy_char = TRUE;
 		} else
+			copy_char = TRUE;
+
+		/* We skipped a character without finding any records,
+		 * let's just copy it to the output. */
+		if (copy_char) {
+			g_string_append_c(out, character);
 			i++;
+		}
 	}
 
 	return g_string_free(out, FALSE);



More information about the Commits mailing list