System.Net.Mail.MailAddressParser.ParseDisplayName C# (CSharp) Method

ParseDisplayName() private static method

private static ParseDisplayName ( string data, int &index, bool expectMultipleAddresses ) : string
data string
index int
expectMultipleAddresses bool
return string
        private static string ParseDisplayName(string data, ref int index, bool expectMultipleAddresses)
        {
            string displayName;

            // Whatever is left over must be the display name. The display name should be a single word/atom or a 
            // quoted string, but for robustness we allow the quotes to be omitted, so long as we can find the comma 
            // separator before the next address.

            // Read the comment (if any).  If the display name is contained in quotes, the surrounding comments are 
            // omitted. Otherwise, mark this end of the comment so we can include it as part of the display name.
            int firstNonCommentIndex = WhitespaceReader.ReadCfwsReverse(data, index);

            // Check to see if there's a quoted-string display name
            if (firstNonCommentIndex >= 0 && data[firstNonCommentIndex] == MailBnfHelper.Quote)
            {
                // The preceding comment was not part of the display name.  Read just the quoted string.
                index = QuotedStringFormatReader.ReadReverseQuoted(data, firstNonCommentIndex, true);

                Debug.Assert(data[index + 1] == MailBnfHelper.Quote, "Mis-aligned index: " + index);

                // Do not include the bounding quotes on the display name
                int leftIndex = index + 2;
                displayName = data.Substring(leftIndex, firstNonCommentIndex - leftIndex);

                // Skip any CFWS after the display name
                index = WhitespaceReader.ReadCfwsReverse(data, index);

                // Check for completion. We are valid if we hit the end of the data string or if the rest of the data 
                // belongs to another address.
                if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma))
                {
                    // If there was still data, only a comma could have been the next valid character
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                }
            }
            else
            {
                // The comment (if any) should be part of the display name.
                int startingIndex = index;

                // Read until the dividing comma or the end of the line.
                index = QuotedStringFormatReader.ReadReverseUnQuoted(data, index, true, expectMultipleAddresses);

                Debug.Assert(index < 0 || data[index] == MailBnfHelper.Comma, "Mis-aligned index: " + index);

                // Do not include the Comma (if any), and because there were no bounding quotes, 
                // trim extra whitespace.
                displayName = data.SubstringTrim(index + 1, startingIndex - index);
            }
            return NormalizeOrThrow(displayName);
        }