Ejemplo n.º 1
0
 public static string Format(ZipStruct value, FormatAttrib param)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     return(value.Text);
 }
Ejemplo n.º 2
0
            public static bool TryParse(string text, ParseAttrib param, out ZipStruct value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = EmptyZipStruct; return(false);
                }
                // check param
                Func <CountryID> countryIdDelegate;
                var countryId = ((param != null) && ((countryIdDelegate = param.CountryID) != null) ? countryIdDelegate() : CountryID.Usa);
                int textLength;

                if ((countryId & CountryID.Canada) == CountryID.Canada)
                {
                    // canada/generic parsing
                    text       = StringEx.ExtractString.ExtractAlphaDigit(text);
                    textLength = text.Length;
                    if ((textLength == 6) &&
                        (char.IsLetter(text[0])) && (char.IsDigit(text[1])) && (char.IsLetter(text[2])) &&
                        (char.IsDigit(text[3])) && (char.IsLetter(text[4])) && (char.IsDigit(text[5])))
                    {
                        var zip  = text.Substring(0, 3);
                        var zip2 = text.Substring(3);
                        value = new CanadaZipStruct {
                            Text = zip + " " + zip2, Zip = zip, Zip2 = zip2
                        }; return(true);
                    }
                }
                if ((countryId & CountryID.Usa) == CountryID.Usa)
                {
                    // usa/generic parsing
                    text       = StringEx.ExtractString.ExtractDigit(text);
                    textLength = text.Length;
                    if ((textLength >= 7) && (textLength <= 9))
                    {
                        var zip5 = text.Substring(0, 5);
                        var zip4 = text.Substring(5).PadLeft(4, '0');
                        value = new UsaZipStruct {
                            Text = zip5 + "-" + zip4, Zip5 = zip5, Zip4 = zip4
                        }; return(true);
                    }
                    else if ((textLength >= 3) && (textLength <= 5))
                    {
                        var zip5 = text.PadLeft(5, '0');
                        value = new UsaZipStruct {
                            Text = zip5, Zip5 = zip5
                        }; return(true);
                    }
                }
                if ((countryId & CountryID.None) == CountryID.None)
                {
                    // accept all
                    value = new ZipStruct {
                        Text = text
                    }; return(true);
                }
                value = EmptyZipStruct; return(false);
            }