System.Uri.IsHexEncoding C# (CSharp) Method

IsHexEncoding() public static method

public static IsHexEncoding ( string pattern, int index ) : bool
pattern string
index int
return bool
        public static bool IsHexEncoding(string pattern, int index)
        {
            return
                (pattern.Length - index) >= 3 &&
                pattern[index] == '%' &&
                UriHelper.EscapedAscii(pattern[index + 1], pattern[index + 2]) != c_DummyChar;
        }

Usage Example

Example #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);
        }
All Usage Examples Of System.Uri::IsHexEncoding