Novell.Directory.Ldap.LdapDN.escapeRDN C# (CSharp) Method

escapeRDN() public static method

Returns the RDN after escaping the characters requiring escaping. For example, for the rdn "cn=Acme, Inc", the escapeRDN method returns "cn=Acme\, Inc". escapeRDN escapes the AttributeValue by inserting '\' before the following chars: * ',' '+' '"' '\' 'LESSTHAN' 'GREATERTHAN' ';' '#' if it comes at the beginning of the string, and ' ' (space) if it comes at the beginning or the end of a string. Note that single-valued attributes can be used because of ambiguity. See RFC 2253
public static escapeRDN ( System rdn ) : System.String
rdn System The RDN to escape. /// ///
return System.String
        public static System.String escapeRDN(System.String rdn)
        {
            System.Text.StringBuilder escapedS = new System.Text.StringBuilder(rdn);
            int i = 0;

            while (i < escapedS.Length && escapedS[i] != '=')
            {
                i++; //advance until we find the separator =
            }
            if (i == escapedS.Length)
            {
                throw new System.ArgumentException("Could not parse RDN: Attribute " + "type and name must be separated by an equal symbol, '='");
            }

            i++;
            //check for a space or # at the beginning of a string.
            if ((escapedS[i] == ' ') || (escapedS[i] == '#'))
            {
                escapedS.Insert(i++, '\\');
            }

            //loop from second char to the second to last
            for (; i < escapedS.Length; i++)
            {
                if ((escapedS[i] == ',') || (escapedS[i] == '+') || (escapedS[i] == '"') || (escapedS[i] == '\\') || (escapedS[i] == '<') || (escapedS[i] == '>') || (escapedS[i] == ';'))
                {
                    escapedS.Insert(i++, '\\');
                }
            }

            //check last char for a space
            if (escapedS[escapedS.Length - 1] == ' ')
            {
                escapedS.Insert(escapedS.Length - 1, '\\');
            }
            return escapedS.ToString();
        }