[banshee] Improve the parsing of the encoding in podcast feeds by using a RegEx



commit a6b580488d37c97eec12143db59df5ad4fa7f964
Author: Bertrand Lorentz <bertrand lorentz gmail com>
Date:   Thu May 21 22:50:39 2009 +0200

    Improve the parsing of the encoding in podcast feeds by using a RegEx
    
    The parsing now properly handles encoding values surrounded by single
    quotes. This should fix another part of bgo#574605.
---
 src/Libraries/Migo/Migo.Net/AsyncWebClient.cs |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/Libraries/Migo/Migo.Net/AsyncWebClient.cs b/src/Libraries/Migo/Migo.Net/AsyncWebClient.cs
index 61c3dd9..a8a3380 100644
--- a/src/Libraries/Migo/Migo.Net/AsyncWebClient.cs
+++ b/src/Libraries/Migo/Migo.Net/AsyncWebClient.cs
@@ -30,6 +30,7 @@ using System;
 using System.IO;
 using System.Net;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading;
 using System.ComponentModel;
 
@@ -48,6 +49,8 @@ namespace Migo.Net
         private int range = 0;
         private int timeout = (120 * 1000); // 2 minutes
         private DateTime ifModifiedSince = DateTime.MinValue;
+        private static Regex encoding_regexp = new Regex (@"encoding=[""']([^""']+)[""']", 
+                                                    RegexOptions.Compiled | RegexOptions.IgnoreCase);
         
         private string fileName;
 
@@ -724,11 +727,9 @@ namespace Migo.Net
     
                         // Workaround if the string is a XML to set the encoding from it
                         if (s.StartsWith("<?xml")) {
-                            string auxStr = "encoding=";
-                            int startIndex = s.IndexOf (auxStr) + auxStr.Length + 1;
-                            if (startIndex > auxStr.Length + 1) {
-                                int endIndex = s.IndexOf ('\"', startIndex);
-                                string encodingStr = s.Substring (startIndex, endIndex - startIndex);
+                            Match match = encoding_regexp.Match (s);
+                            if (match.Success && match.Groups.Count > 0) {
+                                string encodingStr = match.Groups[1].Value;
                                 try {
                                     Encoding enc = Encoding.GetEncoding (encodingStr);
                                     if (!enc.Equals (Encoding)) {
@@ -737,7 +738,8 @@ namespace Migo.Net
                                 } catch (ArgumentException) {}
                             }
                         }
-                    } catch {
+                    } catch (Exception ex) {
+                        Hyena.Log.DebugException (ex);
                         s = String.Empty;
                     }
                 



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