[Kazehakase-devel 2018] migemoを使用しているあたりのリファクタリング

Back to archive index

Kouhei Sutou kou****@cozmi*****
2005年 3月 10日 (木) 22:39:49 JST


須藤です.

ちょっと前にsrc/mozilla/kz-mozembed.cppのmigemoを使った検索
の部分をいじった時に,コードの重複が気になったので書き換えて
みました.
-------------- next part --------------
Index: src/mozilla/kz-mozembed.cpp
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/mozilla/kz-mozembed.cpp,v
retrieving revision 1.42.2.5
diff -u -p -r1.42.2.5 kz-mozembed.cpp
--- src/mozilla/kz-mozembed.cpp	28 Feb 2005 04:40:02 -0000	1.42.2.5
+++ src/mozilla/kz-mozembed.cpp	10 Mar 2005 13:35:27 -0000
@@ -1377,6 +1377,35 @@ kz_moz_embed_get_selection_source(KzEmbe
 }
 #endif
 
+#if USE_MIGEMO
+static gboolean
+kz_moz_embed_get_body_string(KzMozEmbed *mozembed, gchar **body_string,
+			     gboolean backward, gboolean whole)
+{
+	nsAutoString text;
+	nsresult rv;
+
+	if (whole)
+		rv = mozembed->priv->wrapper->GetBodyString(text);
+	else
+		rv = mozembed->priv->wrapper->GetStringSelection(text, backward);
+	if (NS_FAILED(rv))
+		return FALSE;
+	
+	*body_string = g_strdup(NS_ConvertUCS2toUTF8(text).get());
+	return TRUE;
+}
+
+static void
+set_migemo_keyword(KzMozEmbed *mozembed, const char *keyword,
+		   gchar *body_string, gboolean backward)
+{
+	g_free(mozembed->priv->migemo_keyword);
+	mozembed->priv->migemo_keyword =
+		migemo_get_matched_text(body_string, keyword, backward);
+}
+#endif
+
 static gboolean
 kz_moz_embed_find (KzEmbed *kzembed, const char *keyword,
 		   gboolean backward)
@@ -1398,30 +1427,23 @@ kz_moz_embed_find (KzEmbed *kzembed, con
 
 	if (use_migemo)
 	{
-		nsAutoString text;
 		gchar *body_string;
 
-		rv = mozembed->priv->wrapper->GetStringSelection(text, backward);
-		if (NS_FAILED(rv))
+		if (!kz_moz_embed_get_body_string(mozembed, &body_string,
+						  backward, FALSE))
 			goto START_SEARCH;
-		body_string = g_strdup(NS_ConvertUCS2toUTF8(text).get());
-
-		if (mozembed->priv->migemo_keyword)
-			g_free(mozembed->priv->migemo_keyword);
-		mozembed->priv->migemo_keyword =
-			migemo_get_matched_text(body_string, keyword, backward);
+		set_migemo_keyword(mozembed, keyword, body_string, backward);
 
 		if (!mozembed->priv->migemo_keyword)
 		{
-			gchar *last_keyword = NULL;
 			g_free(body_string);
-			rv = mozembed->priv->wrapper->GetBodyString(text);
-			if (NS_FAILED(rv))
+			if (!kz_moz_embed_get_body_string(mozembed,
+							  &body_string,
+							  backward,
+							  TRUE))
 				goto START_SEARCH;
-			mozembed->priv->wrapper->GetBodyString(text);
-			body_string = g_strdup(NS_ConvertUCS2toUTF8(text).get());
-			mozembed->priv->migemo_keyword =
-				migemo_get_matched_text(body_string, keyword, backward);
+			set_migemo_keyword(mozembed, keyword,
+					   body_string, backward);
 		}
 		
 		if (mozembed->priv->migemo_keyword)
@@ -1483,18 +1505,12 @@ kz_moz_embed_incremental_search (KzEmbed
 
 	if (use_migemo)
 	{
-		nsAutoString text;
 		gchar *body_string;
 
-		rv = mozembed->priv->wrapper->GetBodyString(text);
-		if (NS_FAILED(rv))
+		if (!kz_moz_embed_get_body_string(mozembed, &body_string,
+						  backward, TRUE))
 			goto START_SEARCH;
-		body_string = g_strdup(NS_ConvertUCS2toUTF8(text).get());
-
-		if (mozembed->priv->migemo_keyword)
-			g_free(mozembed->priv->migemo_keyword);
-		mozembed->priv->migemo_keyword =
-			migemo_get_matched_text(body_string, keyword, backward);
+		set_migemo_keyword(mozembed, keyword, body_string, backward);
 
 		if (mozembed->priv->migemo_keyword) 
 		{


Kazehakase-devel メーリングリストの案内
Back to archive index