[Kazehakase-devel 1708] Re: URL補完

アーカイブの一覧に戻る

Hidetaka Iwai tyuyu****@sings*****
2004年 10月 24日 (日) 01:45:22 JST


岩井@札幌 です。

Hiroyuki Ikezoe <poinc****@ikezo*****> wrote:
Message-ID: <20041****@ikezo*****>

> > > completion_funcが呼び出される前に正規表現を作っておいて、
> > > completion_funcの中ではそいつを使いまわす、ってことができればなんと
> > > かな
> > > りそうですね。
> > 
> > あまり賢くないですが、GHashTable を使って egg_regex を cache してみま
> > した。ちょっとは速くなると思いますが、いかがでしょう?
> 
> ちゃんと読んでないですけど、ハッシュを作らなくても、keyが変わった時だけ
> egg_regex_newするようにするだけで十分な感じもするんですが、実際試したわ
> けではないので見当はずれかもしれません。

さっきのメール出した後に気が付きました... orz 
このタイミングで g_hash_table_remove() してしまうなら、そもそもハッシュ
は要らないです。

というわけで今度こそどうでしょう?

--
 Hidetaka Iwai
 tyuyu****@sings*****

-------------- next part --------------
? compile
? src/gmon.out
? src/actions/semantic.cache
? src/utils/semantic.cache
Index: src/actions/kz-history-action.c
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/actions/kz-history-action.c,v
retrieving revision 1.13
diff -u -r1.13 kz-history-action.c
--- src/actions/kz-history-action.c	23 Oct 2004 12:07:30 -0000	1.13
+++ src/actions/kz-history-action.c	23 Oct 2004 16:41:33 -0000
@@ -21,6 +21,7 @@
 #include "kz-history-action.h"
 #include "kazehakase.h"
 #include "eggregex.h"
+#include "migemo.h"
 
 #include <string.h>
 #include <glib/gi18n.h>
@@ -62,6 +63,8 @@
 static KzEntryActionClass *parent_class = NULL;
 
 static gint history_action_signals[LAST_SIGNAL] = { 0 };
+static EggRegex *completion_egg_regex = NULL;
+static char* previous_key = NULL;
 
 KZ_OBJECT_GET_TYPE(kz_history_action, "KzHistoryAction", KzHistoryAction,
 		   kz_history_action_class_init, kz_history_action_init,
@@ -311,11 +314,7 @@
 	char *item = NULL;
 	gboolean ret = FALSE;
 	GtkTreeModel *model;
-#if USE_MIGEMO
-	char *matched = NULL;
-	gboolean use_migemo;
-#endif /* USE_MIGEMO */
-
+	GError *e = NULL;
 	model = gtk_entry_completion_get_model (completion);
 
 	gtk_tree_model_get (model, iter,
@@ -324,47 +323,70 @@
 
 	if (!item) return ret;
 
-#if USE_MIGEMO
-	KZ_CONF_GET("Global", "use_migemo", use_migemo, BOOL);
-
-	if (use_migemo)
+	if (previous_key && (previous_key != key))
 	{
+		egg_regex_free(completion_egg_regex);
+		completion_egg_regex = NULL;
+		previous_key = NULL;
+	}
 
-#warning FIXME! implement more proper migemo API
+	if(!completion_egg_regex)
+	{
+#if USE_MIGEMO
+		gboolean use_migemo;
+#endif /* USE_MIGEMO */
 
-		matched = migemo_get_matched_text(item, key);
-		if (matched)
+#if USE_MIGEMO
+		KZ_CONF_GET("Global", "use_migemo", use_migemo, BOOL);
+		if (use_migemo)
 		{
-			g_free(matched);
-			ret = TRUE;
+			gchar* regex = migemo_get_regex(key);
+			if (regex)
+			{
+				completion_egg_regex = egg_regex_new(regex,
+								     EGG_REGEX_MULTILINE |
+								     EGG_REGEX_EXTENDED,
+								     0, &e);
+				if (e)
+				{
+					g_warning("%s", e->message);
+					g_error_free(e);
+					e = NULL;
+				}
+				g_free(regex);
+			}
 		}
-	}
-	else
+		else
 #endif /* USE_MIGEMO */
-	{
-		EggRegex *egg_regex;
-		GError *e = NULL;
-
-		egg_regex = egg_regex_new(key,
-					  EGG_REGEX_CASELESS |
-					  EGG_REGEX_MULTILINE |
-					  EGG_REGEX_EXTENDED,
-					  0, &e);
-
-		if (e)
 		{
-			g_error_free(e);
-			goto END;
+			completion_egg_regex = egg_regex_new(key,
+							     EGG_REGEX_MULTILINE |
+							     EGG_REGEX_EXTENDED,
+							     0, &e);
 
+			if (e)
+			{
+				g_warning("%s", e->message);
+				g_error_free(e);
+				e = NULL;
+			}
 		}
 
-		if (egg_regex_match(egg_regex, item, -1, 0) > 0)
+                /* reference */
+		if(completion_egg_regex)
 		{
-			ret = TRUE;
+			previous_key = (char*)key;
+		}
+		else
+		{
+			goto END;
 		}
-		egg_regex_free(egg_regex);
 	}
 
+	if (egg_regex_match(completion_egg_regex, item, -1, 0) > 0)
+	{
+		ret = TRUE;
+	}
  END:
 	g_free (item);
 	return ret;
Index: src/utils/migemo.c
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/utils/migemo.c,v
retrieving revision 1.12
diff -u -r1.12 migemo.c
--- src/utils/migemo.c	30 Sep 2004 00:06:08 -0000	1.12
+++ src/utils/migemo.c	23 Oct 2004 16:42:08 -0000
@@ -166,4 +166,69 @@
 	return matched_text;
 }
 
+gchar *
+migemo_get_regex(const gchar *text)
+{
+	GIOChannel *in;
+	GIOChannel *out;
+	gchar *write_buf = NULL, *read_buf = NULL;
+	gchar *euc_text = NULL;
+	gchar *regex;
+	gsize bytes;
+	GError *e = NULL;
+	
+	g_return_val_if_fail(text, NULL);
+
+	if (!migemo && !migemo_init())
+		return NULL;
+
+	euc_text = g_convert(text, strlen(text),
+			     "EUC-JP", "UTF-8",
+			     NULL, NULL, &e);
+	if (e)
+	{
+		g_error_free(e);
+		return NULL;
+	}
+	in  = g_io_channel_unix_new(migemo->input);
+	out = g_io_channel_unix_new(migemo->output);
+	
+	g_io_channel_set_encoding(in, NULL, NULL);
+	g_io_channel_set_encoding(out, NULL, NULL);
+	
+	write_buf = g_strconcat (euc_text, "\n", NULL);
+	g_free(euc_text);
+
+	g_io_channel_write_chars(in,
+				 write_buf,
+				 strlen(write_buf),
+				 &bytes,
+				 NULL);
+	g_io_channel_flush(in, NULL);
+	g_free(write_buf);
+	
+	g_io_channel_read_line(out,
+			       &read_buf,
+			       &bytes,
+			       NULL,
+			       &e);
+	g_io_channel_unref(in);
+	g_io_channel_unref(out);
+
+	if (e)
+	{
+		g_warning("%s", e->message);
+		g_error_free(e);
+		e = NULL;
+	}
+
+	if (!read_buf) return NULL;
+	regex = g_convert(read_buf, bytes,
+			  "UTF-8", "EUC-JP",
+			  NULL, NULL, NULL);
+	g_free(read_buf);
+
+	return regex;
+}
+
 #endif /* USE_MIGEMO */
Index: src/utils/migemo.h
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/utils/migemo.h,v
retrieving revision 1.8
diff -u -r1.8 migemo.h
--- src/utils/migemo.h	30 Jul 2004 07:16:20 -0000	1.8
+++ src/utils/migemo.h	23 Oct 2004 16:42:08 -0000
@@ -49,6 +49,7 @@
 void     migemo_exit                 (void);
 gchar   *migemo_get_matched_text     (const gchar *body,
 				      const gchar *text);
+gchar   *migemo_get_regex            (const gchar *text);
 
 G_END_DECLS
 


Kazehakase-devel メーリングリストの案内
アーカイブの一覧に戻る