Skip to content

Commit

Permalink
Email address clean-up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Apr 16, 2018
1 parent 9320511 commit a4fe36a
Showing 1 changed file with 30 additions and 58 deletions.
88 changes: 30 additions & 58 deletions src/Qowaiv/EmailAddress.cs
Expand Up @@ -22,9 +22,7 @@ namespace Qowaiv
[TypeConverter(typeof(EmailAddressTypeConverter))]
public struct EmailAddress : ISerializable, IXmlSerializable, IJsonSerializable, IFormattable, IEquatable<EmailAddress>, IComparable, IComparable<EmailAddress>
{
/// <summary>
/// An email address must not exceed 254 characters.
/// </summary>
/// <summary>An email address must not exceed 254 characters.</summary>
/// <remarks>
/// https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address
/// </remarks>
Expand Down Expand Up @@ -65,26 +63,26 @@ public struct EmailAddress : ISerializable, IXmlSerializable, IJsonSerializable,
private string m_Value;

/// <summary>Gets the number of characters of email address.</summary>
public int Length { get { return IsEmptyOrUnknown() ? 0 : m_Value.Length; } }
public int Length => IsEmptyOrUnknown() ? 0 : m_Value.Length;

/// <summary>Gets the local part of the Email Address.</summary>
public string Local { get { return IsEmptyOrUnknown() ? string.Empty : m_Value.Substring(0, m_Value.IndexOf('@')); } }
public string Local => IsEmptyOrUnknown() ? string.Empty : m_Value.Substring(0, m_Value.IndexOf('@'));

/// <summary>Gets the domain part of the Email Address.</summary>
public string Domain { get { return IsEmptyOrUnknown() ? string.Empty : m_Value.Substring(m_Value.IndexOf('@') + 1); } }
public string Domain => IsEmptyOrUnknown() ? string.Empty : m_Value.Substring(m_Value.IndexOf('@') + 1);

#endregion

#region Methods

/// <summary>Returns true if the email address is empty, otherwise false.</summary>
public bool IsEmpty() { return m_Value == default(string); }
public bool IsEmpty() => m_Value == default(string);

/// <summary>Returns true if the email address is unknown, otherwise false.</summary>
public bool IsUnknown() { return m_Value == EmailAddress.Unknown.m_Value; }
public bool IsUnknown() => m_Value == Unknown.m_Value;

/// <summary>Returns true if the email address is empty or unknown, otherwise false.</summary>
public bool IsEmptyOrUnknown() { return IsEmpty() || IsUnknown(); }
public bool IsEmptyOrUnknown() => IsEmpty() || IsUnknown();

#endregion

Expand Down Expand Up @@ -112,7 +110,7 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
/// <remarks>
/// Returns null as no schema is required.
/// </remarks>
XmlSchema IXmlSerializable.GetSchema() { return null; }
XmlSchema IXmlSerializable.GetSchema() => null;

/// <summary>Reads the email address from an <see href="XmlReader"/>.</summary>
/// <remarks>
Expand Down Expand Up @@ -161,19 +159,19 @@ void IJsonSerializable.FromJson(string jsonString)
/// <param name="jsonInteger">
/// The JSON integer that represents the email address.
/// </param>
void IJsonSerializable.FromJson(Int64 jsonInteger) { throw new NotSupportedException(QowaivMessages.JsonSerialization_Int64NotSupported); }
void IJsonSerializable.FromJson(Int64 jsonInteger) => new NotSupportedException(QowaivMessages.JsonSerialization_Int64NotSupported);

/// <summary>Generates an email address from a JSON number representation.</summary>
/// <param name="jsonNumber">
/// The JSON number that represents the email address.
/// </param>
void IJsonSerializable.FromJson(Double jsonNumber) { throw new NotSupportedException(QowaivMessages.JsonSerialization_DoubleNotSupported); }
void IJsonSerializable.FromJson(Double jsonNumber) => throw new NotSupportedException(QowaivMessages.JsonSerialization_DoubleNotSupported);

/// <summary>Generates an email address from a JSON date representation.</summary>
/// <param name="jsonDate">
/// The JSON Date that represents the email address.
/// </param>
void IJsonSerializable.FromJson(DateTime jsonDate) { throw new NotSupportedException(QowaivMessages.JsonSerialization_DateTimeNotSupported); }
void IJsonSerializable.FromJson(DateTime jsonDate) => throw new NotSupportedException(QowaivMessages.JsonSerialization_DateTimeNotSupported);

/// <summary>Converts an email address into its JSON object representation.</summary>
object IJsonSerializable.ToJson()
Expand All @@ -198,28 +196,19 @@ private string DebuggerDisplay
}

/// <summary>Returns a <see cref="string"/> that represents the current email address.</summary>
public override string ToString()
{
return ToString(CultureInfo.CurrentCulture);
}
public override string ToString() => ToString(CultureInfo.CurrentCulture);

/// <summary>Returns a formatted <see cref="string"/> that represents the current email address.</summary>
/// <param name="format">
/// The format that this describes the formatting.
/// </param>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
public string ToString(string format) => ToString(format, CultureInfo.CurrentCulture);

/// <summary>Returns a formatted <see cref="string"/> that represents the current email address.</summary>
/// <param name="formatProvider">
/// The format provider.
/// </param>
public string ToString(IFormatProvider formatProvider)
{
return ToString("", formatProvider);
}
public string ToString(IFormatProvider formatProvider) => ToString("", formatProvider);

/// <summary>Returns a formatted <see cref="string"/> that represents the current email address.</summary>
/// <param name="format">
Expand All @@ -240,8 +229,7 @@ public string ToString(IFormatProvider formatProvider)
/// </remarks>
public string ToString(string format, IFormatProvider formatProvider)
{
string formatted;
if (StringFormatter.TryApplyCustomFormatter(format, this, formatProvider, out formatted))
if (StringFormatter.TryApplyCustomFormatter(format, this, formatProvider, out string formatted))
{
return formatted;
}
Expand Down Expand Up @@ -270,11 +258,11 @@ public string ToString(string format, IFormatProvider formatProvider)

/// <summary>Returns true if this instance and the other object are equal, otherwise false.</summary>
/// <param name="obj">An object to compare with.</param>
public override bool Equals(object obj) { return obj is EmailAddress && Equals((EmailAddress)obj); }
public override bool Equals(object obj) => obj is EmailAddress && Equals((EmailAddress)obj);

/// <summary>Returns true if this instance and the other <see cref="EmailAddress"/> are equal, otherwise false.</summary>
/// <param name="other">The <see cref="EmailAddress"/> to compare with.</param>
public bool Equals(EmailAddress other) { return m_Value == other.m_Value; }
public bool Equals(EmailAddress other) => m_Value == other.m_Value;

/// <summary>Returns the hash code for this email address.</summary>
/// <returns>
Expand All @@ -285,18 +273,12 @@ public string ToString(string format, IFormatProvider formatProvider)
/// <summary>Returns true if the left and right operand are not equal, otherwise false.</summary>
/// <param name="left">The left operand.</param>
/// <param name="right">The right operand</param>
public static bool operator ==(EmailAddress left, EmailAddress right)
{
return left.Equals(right);
}
public static bool operator ==(EmailAddress left, EmailAddress right) => left.Equals(right);

/// <summary>Returns true if the left and right operand are equal, otherwise false.</summary>
/// <param name="left">The left operand.</param>
/// <param name="right">The right operand</param>
public static bool operator !=(EmailAddress left, EmailAddress right)
{
return !(left == right);
}
public static bool operator !=(EmailAddress left, EmailAddress right) => !(left == right);

#endregion

Expand Down Expand Up @@ -348,7 +330,7 @@ public int CompareTo(object obj)
/// <summary>Casts an email address to a <see cref="string"/>.</summary>
public static explicit operator string(EmailAddress val) { return val.ToString(CultureInfo.CurrentCulture); }
/// <summary>Casts a <see cref="string"/> to a email address.</summary>
public static explicit operator EmailAddress(string str) { return EmailAddress.Parse(str, CultureInfo.CurrentCulture); }
public static explicit operator EmailAddress(string str) { return Parse(str, CultureInfo.CurrentCulture); }


#endregion
Expand All @@ -365,10 +347,7 @@ public int CompareTo(object obj)
/// <exception cref="FormatException">
/// s is not in the correct format.
/// </exception>
public static EmailAddress Parse(string s)
{
return Parse(s, CultureInfo.CurrentCulture);
}
public static EmailAddress Parse(string s) => Parse(s, CultureInfo.CurrentCulture);

/// <summary>Converts the string to an email address.</summary>
/// <param name="s">
Expand All @@ -385,8 +364,7 @@ public static EmailAddress Parse(string s)
/// </exception>
public static EmailAddress Parse(string s, IFormatProvider formatProvider)
{
EmailAddress val;
if (EmailAddress.TryParse(s, formatProvider, out val))
if (TryParse(s, formatProvider, out EmailAddress val))
{
return val;
}
Expand All @@ -400,16 +378,15 @@ public static EmailAddress Parse(string s, IFormatProvider formatProvider)
/// A string containing an email address to convert.
/// </param>
/// <returns>
/// The email address if the string was converted successfully, otherwise EmailAddress.Empty.
/// The email address if the string was converted successfully, otherwise Empty.
/// </returns>
public static EmailAddress TryParse(string s)
{
EmailAddress val;
if (EmailAddress.TryParse(s, out val))
if (TryParse(s, out EmailAddress val))
{
return val;
}
return EmailAddress.Empty;
return Empty;
}

/// <summary>Converts the string to an email address.
Expand All @@ -424,10 +401,7 @@ public static EmailAddress TryParse(string s)
/// <returns>
/// True if the string was converted successfully, otherwise false.
/// </returns>
public static bool TryParse(string s, out EmailAddress result)
{
return TryParse(s, CultureInfo.CurrentCulture, out result);
}
public static bool TryParse(string s, out EmailAddress result) => TryParse(s, CultureInfo.CurrentCulture, out result);

/// <summary>Converts the string to an email address.
/// A return value indicates whether the conversion succeeded.
Expand All @@ -448,15 +422,15 @@ public static bool TryParse(string s, out EmailAddress result)
Justification = "Email addresses are displayed in lowercase by default.")]
public static bool TryParse(string s, IFormatProvider formatProvider, out EmailAddress result)
{
result = EmailAddress.Empty;
result = Empty;
if (string.IsNullOrEmpty(s))
{
return true;
}
var culture = formatProvider as CultureInfo ?? CultureInfo.InvariantCulture;
if (Qowaiv.Unknown.IsUnknown(s, culture))
{
result = EmailAddress.Unknown;
result = Unknown;
return true;
}
if (IsValid(s, formatProvider))
Expand All @@ -472,10 +446,7 @@ public static bool TryParse(string s, IFormatProvider formatProvider, out EmailA
#region Validation

/// <summary>Returns true if the val represents a valid email address, otherwise false.</summary>
public static bool IsValid(string val)
{
return IsValid(val, CultureInfo.CurrentCulture);
}
public static bool IsValid(string val) => IsValid(val, CultureInfo.CurrentCulture);

/// <summary>Returns true if the val represents a valid email address, otherwise false.</summary>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "formatProvider",
Expand All @@ -486,6 +457,7 @@ public static bool IsValid(string val, IFormatProvider formatProvider)
&& val.Length <= MaxLength
&& Pattern.IsMatch(val);
}

#endregion
}
}

0 comments on commit a4fe36a

Please sign in to comment.