// Parses the domain section of an address.  The domain may be in dot-atom format or surrounded by square
        // brackets in domain-literal format.
        // e.g. <*****@*****.**> or <user@[whatever I want]>
        //
        // Preconditions:
        // - data[index] is just inside of the angle brackets (if any).
        //
        // Postconditions:
        // - data[index] should refer to the '@' symbol
        // - returns the parsed domain, including any square brackets for domain-literals
        //
        // Throws a FormatException or returns false:
        // - For invalid un-escaped chars, including Unicode
        // - If the start of the data string is reached
        private static bool TryParseDomain(string data, ref int index, [NotNullWhen(true)] out string?domain, bool throwExceptionIfFail)
        {
            // Skip comments and whitespace
            if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
            {
                domain = default;
                return(false);
            }

            // Mark one end of the domain component
            int startingIndex = index;

            // Is the domain component in domain-literal format or dot-atom format?
            if (data[index] == MailBnfHelper.EndSquareBracket)
            {
                if (!DomainLiteralReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
                {
                    domain = default;
                    return(false);
                }
            }
            else
            {
                if (!DotAtomReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
                {
                    domain = default;
                    return(false);
                }
            }

            domain = data.Substring(index + 1, startingIndex - index);

            // Skip comments and whitespace
            if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
            {
                return(false);
            }

            if (!TryNormalizeOrThrow(domain, out domain, throwExceptionIfFail))
            {
                return(false);
            }

            return(true);
        }
        // Parses the local-part section of an address.  The local-part may be in dot-atom format or
        // quoted-string format. e.g. <user.name@domain> or <"user name"@domain>
        // We do not support the obsolete formats of user."name"@domain, "user".name@domain, or "user"."name"@domain.
        //
        // Preconditions:
        // - data[index + 1] is the '@' symbol
        //
        // Postconditions:
        // - data[index] should refer to the '<', if any, otherwise the next non-CFWS char.
        // - index == -1 if the beginning of the data string has been reached.
        // - returns the parsed local-part, including any bounding quotes around quoted-strings
        //
        // Throws a FormatException or false is returned:
        // - For invalid un-escaped chars, including Unicode
        // - If the final value of data[index] is not a valid character to precede the local-part
        private static bool TryParseLocalPart(string data, ref int index, bool expectAngleBracket,
                                              bool expectMultipleAddresses, [NotNullWhen(true)] out string?localPart, bool throwExceptionIfFail)
        {
            // Skip comments and whitespace
            if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
            {
                localPart = default;
                return(false);
            }

            // Mark the start of the local-part
            int startingIndex = index;

            // Is the local-part component in quoted-string format or dot-atom format?
            if (data[index] == MailBnfHelper.Quote)
            {
                if (!QuotedStringFormatReader.TryReadReverseQuoted(data, index, true, out index, throwExceptionIfFail))
                {
                    localPart = default;
                    return(false);
                }
            }
            else
            {
                if (!DotAtomReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
                {
                    localPart = default;
                    return(false);
                }

                // Check that the local-part is properly separated from the next component. It may be separated by a
                // comment, whitespace, an expected angle bracket, a quote for the display-name, or an expected comma
                // before the next address.
                if (index >= 0 &&
                    !(
                        MailBnfHelper.IsAllowedWhiteSpace(data[index]) ||  // < local@domain >
                        data[index] == MailBnfHelper.EndComment ||     // <(comment)local@domain>
                        (expectAngleBracket && data[index] == MailBnfHelper.StartAngleBracket) ||     // <local@domain>
                        (expectMultipleAddresses && data[index] == MailBnfHelper.Comma) // local@dom,local@dom
                                                                                        // Note: The following condition is more lax than the RFC.  This is done so we could support
                                                                                        // a common invalid formats as shown below.
                        || data[index] == MailBnfHelper.Quote                           // "display"local@domain
                        )
                    )
                {
                    if (throwExceptionIfFail)
                    {
                        throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                    }
                    else
                    {
                        localPart = default;
                        return(false);
                    }
                }
            }

            localPart = data.Substring(index + 1, startingIndex - index);

            if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
            {
                return(false);
            }

            if (!TryNormalizeOrThrow(localPart, out localPart, throwExceptionIfFail))
            {
                return(false);
            }

            return(true);
        }