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

unescapeRDN() public static method

Returns the RDN after unescaping the characters requiring escaping. For example, for the rdn "cn=Acme\, Inc", the unescapeRDN method returns "cn=Acme, Inc". unescapeRDN unescapes the AttributeValue by removing the '\' when the next character fits the following: ',' '+' '"' '\' 'LESSTHAN' 'GREATERTHAN' ';' '#' if it comes at the beginning of the Attribute Name (without the '\'). ' ' (space) if it comes at the beginning or the end of the Attribute Name
public static unescapeRDN ( System rdn ) : System.String
rdn System The RDN to unescape. /// ///
return System.String
        public static System.String unescapeRDN(System.String rdn)
        {
            System.Text.StringBuilder unescaped = new System.Text.StringBuilder();
            int i = 0;

            while (i < rdn.Length && rdn[i] != '=')
            {
                i++; //advance until we find the separator =
            }
            if (i == rdn.Length)
            {
                throw new System.ArgumentException("Could not parse rdn: Attribute " + "type and name must be separated by an equal symbol, '='");
            }
            i++;
            //check if the first two chars are "\ " (slash space) or "\#"
            if ((rdn[i] == '\\') && (i + 1 < rdn.Length - 1) && ((rdn[i + 1] == ' ') || (rdn[i + 1] == '#')))
            {
                i++;
            }
            for (; i < rdn.Length; i++)
            {
                //if the current char is a slash, not the end char, and is followed
                // by a special char then...
                if ((rdn[i] == '\\') && (i != rdn.Length - 1))
                {
                    if ((rdn[i + 1] == ',') || (rdn[i + 1] == '+') || (rdn[i + 1] == '"') || (rdn[i + 1] == '\\') || (rdn[i + 1] == '<') || (rdn[i + 1] == '>') || (rdn[i + 1] == ';'))
                    {
                        //I'm not sure if I have to check for these special chars
                        continue;
                    }
                    //check if the last two chars are "\ "
                    else if ((rdn[i + 1] == ' ') && (i + 2 == rdn.Length))
                    {
                        //if the last char is a space
                        continue;
                    }
                }
                unescaped.Append(rdn[i]);
            }
            return unescaped.ToString();
        }