System.Uri.HexUnescape C# (CSharp) Method

HexUnescape() public static method

public static HexUnescape ( string pattern, int &index ) : char
pattern string
index int
return char
        public static char HexUnescape(string pattern, ref int index)
        {
            if ((index < 0) || (index >= pattern.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if ((pattern[index] == '%')
                && (pattern.Length - index >= 3))
            {
                char ret = UriHelper.EscapedAscii(pattern[index + 1], pattern[index + 2]);
                if (ret != c_DummyChar)
                {
                    index += 3;
                    return ret;
                }
            }
            return pattern[index++];
        }

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::HexUnescape