internal virtual string PercentEncode(string value)
    {
        const string ReservedChars = @"`!@#$^&*()+=,:;'?/|\[] ";
        var          result        = new StringBuilder();

        if (string.IsNullOrWhiteSpace(value))
        {
            return(string.Empty);
        }
        var escapedValue = Uri.EscapeDataString(value);

        // Windows Phone doesn't escape all the ReservedChars properly, so we have to do it manually.
        foreach (char symbol in escapedValue)
        {
            if (ReservedChars.IndexOf(symbol) != -1)
            {
                result.Append('%' + String.Format("{0:X2}", (int)symbol).ToUpper());
            }
            else
            {
                result.Append(symbol);
            }
        }
        return(result.ToString());
    }
Exemple #2
0
        /// <summary>
        /// Url Encodes a value
        /// </summary>
        /// <param name="value">string to be encoded</param>
        /// <returns>UrlEncoded string</returns>
        public static string UrlEncode(string value)
        {
            const string ReservedChars   = @"`!@#$%^&*()_-+=.~,:;'?/|\[] ";
            const string UnReservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

            var result = new StringBuilder();

            if (string.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }

            foreach (var symbol in value)
            {
                if (UnReservedChars.IndexOf(symbol) != -1)
                {
                    result.Append(symbol);
                }
                else if (ReservedChars.IndexOf(symbol) != -1)
                {
                    result.Append('%' + String.Format("{0:X2}", (int)symbol).ToUpper());
                }
                else
                {
                    string symbolString = symbol.ToString();
                    var    encoded      = MSEncoder.UrlEncode(symbolString, Encoding.UTF8).ToUpper();

                    if (!string.IsNullOrEmpty(encoded))
                    {
                        result.Append(encoded);
                    }
                }
            }

            return(result.ToString());
        }
 public StringState()
 {
     _reservedChars = new ReservedChars();
 }
 public CharState()
 {
     _reservedChars = new ReservedChars();
 }