Exemplo n.º 1
0
 public void WordFinish(char currentSymbol)
 {
     if (PunctuationInSetnenceSetted == false)
     {
         SetWord();
     }
     CharBuffer.Add(currentSymbol);
     PunctuationInSetnenceSetted = true;
 }
Exemplo n.º 2
0
 public void Parse(char currentSymbol)
 {
     if (Shapes.ContainsKey(currentSymbol) == true)
     {
         Shape Action = Shapes[currentSymbol];
         Action(currentSymbol);
     }
     else
     {
         CharBuffer.Add(currentSymbol);
     }
 }
Exemplo n.º 3
0
        /// <summary>Parses an email address string.</summary>
        /// <returns>
        /// null if the string was not a valid email address.
        /// a stripped lowercased email address if valid.
        /// </returns>
        internal static string Parse(string s)
        {
            var str = new CharBuffer(s)
                      .Trim()
                      .RemoveQuotedPrefix()
                      .RemoveDisplayName()
                      .RemoveComment()
            ;

            if (str.Empty())
            {
                return(null);
            }

            // buffers.
            var local  = new CharBuffer(EmailAddress.MaxLength);
            var domain = new CharBuffer(EmailAddress.MaxLength);

            var mailto      = false;
            var noAt        = true;
            var prev        = default(char);
            var hasBrackets = false;
            var dot         = NotFound;

            var end = str.Length;

            for (var pos = 0; pos < end; pos++)
            {
                var ch = str[pos];

                if (TooLong(local, domain))
                {
                    return(null);
                }

                // Never a double dot.
                if (ch == Dot && prev == Dot)
                {
                    return(null);
                }

                // @
                if (ch == At)
                {
                    // No @ yet, and a not empty local part, and not predicated by a dot.
                    if (noAt && local.NotEmpty() && prev != Dot)
                    {
                        noAt = false;
                        local.Add(At);
                    }
                    else
                    {
                        return(null);
                    }
                }
                // Local part.
                else if (noAt)
                {
                    // If no MailTo: detected yet, we should remove it.
                    if (!mailto && ch == Colon && local.Equals(nameof(mailto), true))
                    {
                        local.Clear();
                        mailto = true;
                        continue;
                    }

                    // Don't start with a dot.
                    if (!IsValidLocal(ch) || ch == Dot && local.Empty())
                    {
                        return(null);
                    }
                    local.Add(ch);
                }
                // Domain part.
                else
                {
                    // Potentially an email address of the type local@[ip-address].
                    if (domain.Empty() && ch == '[' && str[end - 1] == ']')
                    {
                        hasBrackets = true;
                        end--;
                        continue;
                    }

                    // Don't start with a dash or a dot.
                    if (!IsValidDomain(ch) || (domain.Empty() && (ch == Dash || ch == Dot)))
                    {
                        return(null);
                    }

                    if (ch == Dot)
                    {
                        // No -.
                        if (prev == Dash)
                        {
                            return(null);
                        }
                        dot = domain.Length;
                    }
                    // No .-
                    else if (ch == Dash && prev == Dot)
                    {
                        return(null);
                    }
                    domain.AddLower(ch);
                }
                prev = ch;
            }

            if (noAt)
            {
                return(null);
            }

            // a valid extension is only applicable without brackets.
            if (!hasBrackets && domain.IsValidDomain(dot))
            {
                return(local.Add(domain));
            }

            // Validate The IP address.
            if (IPAddress.TryParse(domain, out IPAddress ip))
            {
                // as IPAddress parse is too forgiving.
                if (ip.AddressFamily == AddressFamily.InterNetwork && domain.Count(Dot) != 3)
                {
                    return(null);
                }
                return(local.Add('[').Add(ip.ToString()).Add(']'));
            }
            return(null);
        }