Skip to content

Instantly share code, notes, and snippets.

@kntajus
Created January 31, 2013 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kntajus/4682933 to your computer and use it in GitHub Desktop.
Save kntajus/4682933 to your computer and use it in GitHub Desktop.
UK Postcode Implementation
namespace Diuturnal.Utility {
[Serializable]
public struct Postcode {
private const string ValidationString =
@"^\s*(?<out>[A-Z]{1,2}[0-9R][0-9A-Z]?) *(?<in>[0-9][A-Z-[CIKMOV]]{2})\s*$";
private static readonly Regex _validator =
new Regex(ValidationString);
public static readonly Postcode Empty =
new Postcode(string.Empty, string.Empty);
private string _outCode;
private string _inCode;
private Postcode(string outCode, string inCode) {
_outCode = outCode;
_inCode = inCode;
}
public override string ToString() {
if (0 == _outCode.Length) {
return string.Empty;
}
return _outCode + " " + _inCode;
}
public override bool Equals(object obj) {
if (!(obj is Postcode)) {
return false;
}
return (this == (Postcode) obj);
}
public override int GetHashCode() {
return this.ToString().GetHashCode();
}
public static bool operator ==(Postcode lhs, Postcode rhs) {
return (lhs.ToString() == rhs.ToString());
}
public static bool operator !=(Postcode lhs, Postcode rhs) {
return !(lhs == rhs);
}
public static Postcode Parse(string s) {
if (null == s) {
throw new ArgumentNullException();
}
string capitalised = s.ToUpper();
if (!_validator.IsMatch(capitalised)) {
throw new FormatException();
}
return new Postcode(_validator.Replace(capitalised, "${out}"),
_validator.Replace(capitalised, "${in}"));
}
public static bool TryParse(string s, out Postcode result) {
if (null == s) {
result = Postcode.Empty;
return false;
}
string capitalised = s.ToUpper();
if (!_validator.IsMatch(capitalised)) {
result = Postcode.Empty;
return false;
}
result = new Postcode(_validator.Replace(capitalised, "${out}"),
_validator.Replace(capitalised, "${in}"));
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment