private DN(RDNList rdnList, EscapeChars escapeChars) { this.escapeChars = escapeChars; rDNs = rdnList; GenerateHashCode(); }
private void ParseDN(string dnString) { // If the string has nothing in it, that's allowed. Just return an empty array. if (dnString.Length == 0) { rDNs = new RDNList(new RDN[0]); return; // That was easy... } // Break the DN down into its component RDNs. // Don't check the validity of the RDNs; just find the separators. ArrayList rawRDNs = new ArrayList(); ParserState state = ParserState.LookingForSeparator; StringBuilder rawRDN = new StringBuilder(); for (int position = 0; position < dnString.Length; ++position) { switch(state) { # region case ParserState.LookingForSeparator: case ParserState.LookingForSeparator: // If we find a separator character, we've hit the end of an RDN. // We'll store the RDN, and we'll check to see if the RDN is actually // valid later. if (dnString[position] == ',' || dnString[position] == ';') { rawRDNs.Add(rawRDN.ToString()); // Add the string to the list of raw RDNs rawRDN.Length = 0; // Clear the StringBuilder to prepare for the next RDN } else { // Add the character to our temporary RDN string rawRDN.Append(dnString[position]); // If we find an escape character, store character that follows it, // but don't consider it as a possible separator character. If the // string ends with an escape character, that's bad, and we should // throw an exception if (dnString[position] == '\\') { try { rawRDN.Append(dnString[++position]); } catch (IndexOutOfRangeException) { throw new ArgumentException("Invalid DN: DNs aren't allowed to end with an escape character.", dnString); } } // If we find a quote, we'll change state so that we look for the closing quote // and ignore any separator characters within. else if (dnString[position] == '"') { state = ParserState.InQuotedString; } } break; # endregion # region case ParserState.InQuotedString: case ParserState.InQuotedString: // Store the character rawRDN.Append(dnString[position]); // You're allowed to escape special characters in a quoted string, but not required // to. But if there's an escaped quote, we need to take special care to make sure // that we don't mistake that for the end of the quoted string. if (dnString[position] == '\\') { try { rawRDN.Append(dnString[++position]); } catch (IndexOutOfRangeException) { throw new ArgumentException("Invalid DN: DNs aren't allowed to end with an escape character.", dnString); } } else if (dnString[position] == '"') state = ParserState.LookingForSeparator; break; # endregion } } // Take the last RDN and add it to the list rawRDNs.Add(rawRDN.ToString()); // Check parser's end state if (state == ParserState.InQuotedString) throw new ArgumentException("Invalid DN: Unterminated quoted string.", dnString); RDN[] results = new RDN[rawRDNs.Count]; for (int i = 0; i < results.Length; i++) { results[i] = new RDN(rawRDNs[i].ToString()); } rDNs = new RDNList(results); }