Merge pull request #3997 from lateralusX/jlorenss/win-api-family-support-safearray
[mono.git] / mcs / class / System / System.Net.Mail / MailAddress.cs
index b40e5991c7d70922fc4c0a4400100623863ba009..5ffaf6b4bd110ee2dac9486133082dba434a1783 100644 (file)
@@ -28,8 +28,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System.Text;
 
 namespace System.Net.Mail {
@@ -39,6 +37,9 @@ namespace System.Net.Mail {
 
                string address;
                string displayName;
+               string host;
+               string user;
+               string to_string;
                //Encoding displayNameEncoding;
 
                #endregion // Fields
@@ -49,77 +50,131 @@ namespace System.Net.Mail {
                {
                }
 
-               public MailAddress (string address, string displayName) : this (address, displayName, Encoding.Default)
+               public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
                {
                }
 
-               public MailAddress (string address, string name, Encoding displayNameEncoding)
+               [MonoTODO ("We don't do anything with displayNameEncoding")]
+               public MailAddress (string address, string displayName, Encoding displayNameEncoding)
                {
+                       if (address == null)
+                               throw new ArgumentNullException ("address");
+                       if (address.Length == 0)
+                               throw new ArgumentException ("address");
+
+                       if (displayName != null)
+                               this.displayName = displayName.Trim ();
+                       ParseAddress (address);
+               }
+
+               void ParseAddress (string address)
+               {
+                       // 1. Quotes for display name
+                       address = address.Trim ();
+                       int idx = address.IndexOf ('"');
+                       if (idx != -1) {
+                               if (idx != 0 || address.Length == 1)
+                                       throw CreateFormatException ();
+
+                               int closing = address.LastIndexOf ('"');
+                               if (closing == idx)
+                                       throw CreateFormatException ();
+
+                               if (this.displayName == null)
+                                       this.displayName = address.Substring (idx + 1, closing - idx - 1).Trim ();
+                               address = address.Substring (closing + 1).Trim ();
+                       }
+
+                       // 2. <email>
+                       idx = address.IndexOf ('<');
+                       if (idx >= 0) {
+                               if (this.displayName == null)
+                                       this.displayName = address.Substring (0, idx).Trim ();
+                               if (address.Length - 1 == idx)
+                                       throw CreateFormatException ();
+
+                               int end = address.IndexOf ('>', idx + 1);
+                               if (end == -1)
+                                       throw CreateFormatException ();
+
+                               address = address.Substring (idx + 1, end - idx - 1).Trim ();
+                       }
                        this.address = address;
-                       this.displayName = name;
-                       //this.displayNameEncoding = displayNameEncoding;
+                       // 3. email
+                       idx = address.IndexOf ('@');
+                       if (idx <= 0)
+                               throw CreateFormatException ();
+                       if (idx != address.LastIndexOf ('@'))
+                               throw CreateFormatException ();
+
+                       this.user = address.Substring (0, idx).Trim ();
+                       if (user.Length == 0)
+                               throw CreateFormatException ();
+                       this.host = address.Substring (idx + 1).Trim ();
+                       if (host.Length == 0)
+                               throw CreateFormatException ();
                }
 
                #endregion // Constructors
 
-               #region Properties
+#region Properties
 
-               public string Address { 
+               public string Address {
                        get { return address; }
                }
 
                public string DisplayName {
-                       get { return displayName; }
+                       get {
+                               if (displayName == null)
+                                       return string.Empty;
+                               return displayName;
+                       }
                }
 
                public string Host {
-                       get { return Address.Substring (address.IndexOf ("@") + 1); }
+                       get { return host; }
                }
 
                public string User {
-                       get { return Address.Substring (0, address.IndexOf ("@")); }
+                       get { return user; }
                }
 
-               #endregion // Properties
+#endregion // Properties
 
-               #region Methods
+#region Methods
                
                public override bool Equals (object obj)
                {
-                       return Equals (obj as MailAddress);
-               }
+                       if (obj == null)
+                               return false;
 
-               bool Equals (MailAddress other)
-               {
-                       return other != null && Address == other.Address;
+                       return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
                }
 
                public override int GetHashCode ()
                {
-                       return address.GetHashCode ();
+                       return ToString ().GetHashCode ();
                }
 
                public override string ToString ()
                {
-                       StringBuilder sb = new StringBuilder ();
-                       if (DisplayName != null && DisplayName.Length > 0) {
-                               sb.Append ("\"");
-                               sb.Append (DisplayName);
-                               sb.Append ("\"");
-                               sb.Append (" ");\r
-                               sb.Append ("<");\r
-                               sb.Append (Address);\r
-                               sb.Append (">");\r
-                       }
-                       else {
-                               sb.Append (Address);
-                       }
+                       if (to_string != null)
+                               return to_string;
+
+                       if (!String.IsNullOrEmpty (displayName))
+                               to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
+                       else
+                               to_string = address;
+
+                       return to_string;
+               }
 
-                       return sb.ToString ();
+               private static FormatException CreateFormatException () {
+                       return new FormatException ("The specified string is not in the "
+                               + "form required for an e-mail address.");
                }
 
-               #endregion // Methods
+#endregion // Methods
        }
 }
 
-#endif // NET_2_0