// Parse a single MailAddress from the given string. // // Throws a FormatException or returns false if any part of the MailAddress is invalid. internal static bool TryParseAddress(string data, out ParseAddressInfo parsedAddress, bool throwExceptionIfFail) { int index = data.Length - 1; bool parseSuccess = TryParseAddress(data, false, ref index, out parsedAddress, throwExceptionIfFail); Debug.Assert(!parseSuccess || index == -1, "The index indicates that part of the address was not parsed: " + index); return(parseSuccess); }
// // Parse a single MailAddress, potentially from a list. // // Preconditions: // - Index must be within the bounds of the data string. // - The data string must not be null or empty // // Postconditions: // - Returns a valid MailAddress object parsed from the string // - For a single MailAddress index is set to -1 // - For a list data[index] is the comma separator or -1 if the end of the data string was reached. // // Throws a FormatException or false is returned if any part of the MailAddress is invalid. private static bool TryParseAddress(string data, bool expectMultipleAddresses, ref int index, out ParseAddressInfo parseAddressInfo, bool throwExceptionIfFail) { Debug.Assert(!string.IsNullOrEmpty(data)); Debug.Assert(index >= 0 && index < data.Length, "Index out of range: " + index + ", " + data.Length); // Parsed components to be assembled as a MailAddress later string?displayName; // Skip comments and whitespace if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail)) { parseAddressInfo = default; return(false); } // Do we expect angle brackets around the address? // e.g. ("display name" <user@domain>) bool expectAngleBracket = false; if (data[index] == MailBnfHelper.EndAngleBracket) { expectAngleBracket = true; index--; } if (!TryParseDomain(data, ref index, out string?domain, throwExceptionIfFail)) { parseAddressInfo = default; return(false); } // The next character after the domain must be the '@' symbol if (data[index] != MailBnfHelper.At) { if (throwExceptionIfFail) { throw new FormatException(SR.MailAddressInvalidFormat); } else { parseAddressInfo = default; return(false); } } // Skip the '@' symbol index--; if (!TryParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses, out string?localPart, throwExceptionIfFail)) { parseAddressInfo = default; return(false); } // Check for a matching angle bracket around the address if (expectAngleBracket) { if (index >= 0 && data[index] == MailBnfHelper.StartAngleBracket) { index--; // Skip the angle bracket // Skip whitespace, but leave comments, as they may be part of the display name. if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail)) { parseAddressInfo = default; return(false); } } else { // Mismatched angle brackets if (throwExceptionIfFail) { throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, (index >= 0 ? data[index] : MailBnfHelper.EndAngleBracket))); } else { parseAddressInfo = default; return(false); } } } // Is there anything left to parse? // There could still be a display name or another address if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma)) { if (!TryParseDisplayName(data, ref index, expectMultipleAddresses, out displayName, throwExceptionIfFail)) { parseAddressInfo = default; return(false); } } else { displayName = string.Empty; } parseAddressInfo = new ParseAddressInfo(displayName, localPart, domain); return(true); }