banshee r5037 - in trunk/banshee: . src/Libraries/Hyena/Hyena src/Libraries/Hyena/Hyena/Tests



Author: gburt
Date: Thu Feb 12 22:55:59 2009
New Revision: 5037
URL: http://svn.gnome.org/viewvc/banshee?rev=5037&view=rev

Log:
2009-02-12  Gabriel Burt  <gabriel burt gmail com>

	* src/Libraries/Hyena/Hyena/StringUtil.cs: Patch from John Millikin
	revamping the illegal_chars array, removing a bunch of chars that should
	be fine, and making sure it escapes all chars that are invalid in FAT32,
	HFS, and ext3 (BGO #458224).  Changes by me to handle null input, and move
	the Regex creation to static init instead of lazy load.

	* src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs: Tests for the
	revamped EscapeFilename method.

Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs
   trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs

Modified: trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs	Thu Feb 12 22:55:59 2009
@@ -205,22 +205,39 @@
             return sb.ToString ().Normalize (NormalizationForm.FormKC);
         }
         
-        private static string invalid_path_characters = "\"\\:'~`! #$%^&*_-+|?/><[]";
-        private static Regex invalid_path_regex;
-        
-        public static string EscapeFilename (string input)
+        private static Regex invalid_path_regex = BuildInvalidPathRegex ();
+
+        private static Regex BuildInvalidPathRegex ()
         {
-            if (invalid_path_regex == null) {
-                string regex_str = "[";
-                for (int i = 0; i < invalid_path_characters.Length; i++) {
-                    regex_str += "\\" + invalid_path_characters[i];
-                }
-                regex_str += "]+";
+            char [] invalid_path_characters = new char [] {
+                // Control characters: there's no reason to ever have one of these in a track name anyway,
+                // and they're invalid in all Windows filesystems.
+                '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
+                '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F',
+                '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
+                '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F',
                 
-                invalid_path_regex = new Regex (regex_str, RegexOptions.Compiled);
+                // Invalid in FAT32 / NTFS: " \ / : * | ? < >
+                // Invalid in HFS   :
+                // Invalid in ext3  /
+                '"', '\\', '/', ':', '*', '|', '?', '<', '>'
+            };
+
+            string regex_str = "[";
+            for (int i = 0; i < invalid_path_characters.Length; i++) {
+                regex_str += "\\" + invalid_path_characters[i];
             }
+            regex_str += "]+";
+            
+            return new Regex (regex_str, RegexOptions.Compiled);
+        }
+        
+        public static string EscapeFilename (string input)
+        {
+            if (input == null)
+                return "";
             
-            return invalid_path_regex.Replace (input, String.Empty);
+            return invalid_path_regex.Replace (input, "_");
         }
     }
 }

Modified: trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs	Thu Feb 12 22:55:59 2009
@@ -236,6 +236,49 @@
             AssertSearchKey ("/", "");
         }
     }
+
+    [TestFixture]
+    public class EscapeFilenameTests
+    {
+        private void AssertProduces (string input, string output)
+        {
+            Assert.AreEqual (output, StringUtil.EscapeFilename (input));
+        }
+
+        private void AssertProducesSame (string input)
+        {
+            AssertProduces (input, input);
+        }
+        
+        [Test]
+        public void TestEmpty ()
+        {
+            AssertProduces (null,   "");
+            AssertProduces ("",     "");
+            AssertProduces (" ",    " ");
+            AssertProduces ("   ",  "   ");
+        }
+
+        [Test]
+        public void TestNotChanged ()
+        {
+            AssertProducesSame ("a");
+            AssertProducesSame ("aaa");
+            AssertProducesSame ("Foo Bar");
+            AssertProducesSame ("03-Nur getrÃumt");
+            AssertProducesSame ("äå");
+            AssertProducesSame ("nÇ hÇo");
+        }
+
+        [Test]
+        public void TestStripped ()
+        {
+            AssertProduces ("Foo*bar", "Foo_bar");
+            AssertProduces ("</foo:bar?>", "_foo_bar_");
+            AssertProduces ("</:?>", "_");
+            AssertProduces ("Greetings! -* äå?", "Greetings! -_ äå_");
+        }
+    }
 }
 
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]