Novell.Directory.Ldap.LdapUrl.decode C# (CSharp) Method

decode() public static method

Decodes a URL-encoded string. Any occurences of %HH are decoded to the hex value represented. However, this method does NOT decode "+" into " ".
MalformedURLException The URL could not be parsed. ///
public static decode ( System URLEncoded ) : System.String
URLEncoded System String to decode. /// ///
return System.String
        public static System.String decode(System.String URLEncoded)
        {
            int searchStart = 0;
            int fieldStart;

            fieldStart = URLEncoded.IndexOf("%", searchStart);
            // Return now if no encoded data
            if (fieldStart < 0)
            {
                return URLEncoded;
            }

            // Decode the %HH value and copy to new string buffer
            int fieldEnd = 0; // end of previous field
            int dataLen = URLEncoded.Length;

            System.Text.StringBuilder decoded = new System.Text.StringBuilder(dataLen);

            while (true)
            {
                if (fieldStart > (dataLen - 3))
                {
                    throw new System.UriFormatException("LdapUrl.decode: must be two hex characters following escape character '%'");
                }
                if (fieldStart < 0)
                    fieldStart = dataLen;
                // Copy to string buffer from end of last field to start of next
                decoded.Append(URLEncoded.Substring(fieldEnd, (fieldStart) - (fieldEnd)));
                fieldStart += 1;
                if (fieldStart >= dataLen)
                    break;
                fieldEnd = fieldStart + 2;
                try
                {
                    decoded.Append((char) System.Convert.ToInt32(URLEncoded.Substring(fieldStart, (fieldEnd) - (fieldStart)), 16));
                }
                catch (System.FormatException ex)
                {
                    throw new System.UriFormatException("LdapUrl.decode: error converting hex characters to integer \"" + ex.Message + "\"");
                }
                searchStart = fieldEnd;
                if (searchStart == dataLen)
                    break;
                fieldStart = URLEncoded.IndexOf("%", searchStart);
            }

            return (decoded.ToString());
        }