IsHexEncoding() public static method

public static IsHexEncoding ( string pattern, int index ) : bool
pattern string
index int
return bool
Ejemplo n.º 1
0
        // userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
        private static bool ParseUser(ParserState state)
        {
            string        part = state.remaining;
            StringBuilder sb   = null;

            int index;

            for (index = 0; index < part.Length; index++)
            {
                char ch            = part [index];
                bool isEscapedChar = false;
                var  oldIndex      = index;

                if (ch == '%')
                {
                    if (!Uri.IsHexEncoding(part, index))
                    {
                        return(false);
                    }
                    ch = Uri.HexUnescape(part, ref index);
                    index--;
                    isEscapedChar = true;
                }

                if (!Char.IsLetterOrDigit(ch) && !IsUnreserved(ch) && !IsSubDelim(ch) && ch != ':')
                {
                    if (!isEscapedChar)
                    {
                        break;
                    }

                    ch    = '%';
                    index = oldIndex;
                }

                if (sb == null)
                {
                    sb = new StringBuilder();
                }
                sb.Append(ch);
            }

            if (index + 1 <= part.Length && part [index] == '@')
            {
                if (state.elements.scheme == Uri.UriSchemeFile)
                {
                    state.error = "Invalid URI: The hostname could not be parsed.";
                    return(false);
                }

                state.elements.user = sb == null ? "" : sb.ToString();
                state.remaining     = state.remaining.Substring(index + 1);
            }

            return(state.remaining.Length > 0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 解码汉字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UnEscape(this string str)
        {
            StringBuilder sb  = new StringBuilder();
            int           len = str.Length;
            int           i   = 0;

            while (i != len)
            {
                if (Uri.IsHexEncoding(str, i))
                {
                    sb.Append(Uri.HexUnescape(str, ref i));
                }
                else
                {
                    sb.Append(str[i++]);
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 3
0
        // userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
        private static bool ParseUser(ref ParserState state)
        {
            string        part = state.remaining;
            StringBuilder sb   = null;

            int index;

            for (index = 0; index < part.Length; index++)
            {
                char ch = part [index];

                if (ch == '%')
                {
                    if (!Uri.IsHexEncoding(part, index))
                    {
                        return(false);
                    }
                    ch = Uri.HexUnescape(part, ref index);
                }

                if (Char.IsLetterOrDigit(ch) || IsUnreserved(ch) || IsSubDelim(ch) || ch == ':')
                {
                    if (sb == null)
                    {
                        sb = new StringBuilder();
                    }
                    sb.Append(ch);
                }
                else
                {
                    break;
                }
            }

            if (index + 1 <= part.Length && part [index] == '@')
            {
                state.elements.user = sb == null ? "" : sb.ToString();
                state.remaining     = state.remaining.Substring(index + 1);
            }

            return(state.remaining.Length > 0);
        }