[MonoDevelop] Patch for non utf8 file bug in find in files

John BouAntoun secretsquirrel@optusnet.com.au
Thu, 10 Jun 2004 01:33:54 -0400


--=-F1SOYv355elHtNH26Wz8
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Guys,

Here's a patch for the find in files borking on non utf8 text bug.

When I made the fix I found another bug that means the BuildTextIterator
(in the ForwardTextIteratorBuilder.cs) hangs on zero length files. I add
a new check to prevent BuildTextIterator being called on zero length
files during a search but suspect that the function BuildTextIterator
should probably be fixed also. Didn't get round to this.

In the mean time, I've test this fix and find in files runs along
happily skipping non utf8 text files (using g_utf8_validate) and zero
length files.

Can someone check and commit please (and close the bug)?

JBA



--=-F1SOYv355elHtNH26Wz8
Content-Disposition: attachment; filename=find-in-files-fix.patch
Content-Type: text/x-patch; name=find-in-files-fix.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

Index: MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/Gui/SourceEditorBuffer.cs
===================================================================
--- MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/Gui/SourceEditorBuffer.cs	(revision 1723)
+++ MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/Gui/SourceEditorBuffer.cs	(working copy)
@@ -195,42 +195,61 @@
 		{
 			StreamReader sr = System.IO.File.OpenText (file);
 			LoadText (sr.ReadToEnd (), mime);		
-			sr.Close ();
-			Modified = false;
+			sr.Close ();			
 		}
 		
 		public void LoadFile (string file)
 		{
 			using (NoUndo n = new NoUndo (this)) {
 				StreamReader sr = System.IO.File.OpenText (file);
-				Text = sr.ReadToEnd ();
+				LoadText(sr.ReadToEnd ());
 				sr.Close ();
 			}
-			
-			Modified = false;
-			ScrollToTop ();
 		}
+
+		// needed to make sure the text is valid
+		[DllImport("glib-2.0")]
+		static extern bool g_utf8_validate(string text, int textLength, IntPtr end);
 		
 		public void LoadText (string text, string mime)
 		{
 			SourceLanguage lang = slm.GetLanguageFromMimeType (mime);
 			if (lang != null) 
 				Language = lang;
+
+			LoadText(text);
+
+/* NOTE: Refactored this bit (JBA 10 June 2004)
+			if (g_utf8_validate (text, text.Length, IntPtr.Zero))
+			{
+				System.Console.WriteLine("g_utf8_validate returned true");
+				using (NoUndo n = new NoUndo (this))
+					Text = text;
+			}
 			
-			using (NoUndo n = new NoUndo (this))
-				Text = text;
-			
+			System.Console.WriteLine("moved beyond g_utf8_validate section");
+
 			Modified = false;
 			ScrollToTop ();
+*/
 		}
 		
+
 		public void LoadText (string text)
 		{
-			using (NoUndo n = new NoUndo (this))
-				Text = text;
-			
+			if (g_utf8_validate (text, text.Length, IntPtr.Zero))
+			{
+				using (NoUndo n = new NoUndo (this))
+					Text = text;
+			}
+			else
+			{
+				using (NoUndo n = new NoUndo (this))
+					Text = null;
+			}
+
 			Modified = false;
-			ScrollToTop ();
+			ScrollToTop ();			
 		}
 
 		void ScrollToTop ()
Index: MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/ChangeLog
===================================================================
--- MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/ChangeLog	(revision 1723)
+++ MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/ChangeLog	(working copy)
@@ -1,3 +1,10 @@
+2004-06-10  John BouAntoun  <jba-mono@optusnet.com.au>
+	* Gui/SourceEditorBuffer.cs Added g_utf8_validate check to LoadText, 
+	refactored LoadText and LoadFile usage
+	* Search/DefaultFind.cs Added check for empty string .Text property
+	of a source buffer in find routine. BuildTextIterator hangs on 
+	empty strings for some reason
+
 2004-06-06  John Luke  <jluke@cfl.rr.com>
 
 	* Gui/SourceEditorWidget.cs: set PolicyType.Automatic for scrolling
Index: MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/Search/DefaultFind.cs
===================================================================
--- MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/Search/DefaultFind.cs	(revision 1723)
+++ MonoDevelop/src/AddIns/DisplayBindings/SourceEditor/Search/DefaultFind.cs	(working copy)
@@ -110,7 +110,9 @@
 			if (documentIterator.MoveForward()) {
 				info = documentIterator.Current;
 				// document is valid for searching -> set iterator & fileName
-				if (info != null && info.TextBuffer != null && info.EndOffset >= 0 && info.EndOffset < info.TextBuffer.Length) {
+				// add check to make sure text != string.Empty since this was causing BuildTextIterator to hang (JBA)
+				if (info != null && info.TextBuffer != null && info.TextBuffer.Text != string.Empty && info.EndOffset >= 0 && info.EndOffset < info.TextBuffer.Length) {
+					// TODO: figure out why BuildTextIterator hangs if info.TextBUffer.Text is empty string
 					textIterator = textIteratorBuilder.BuildTextIterator(info);
 				} else {
 					textIterator = null;

--=-F1SOYv355elHtNH26Wz8--