// IsHexEncoding // // Determines whether a substring has the URI hex encoding format of '%' // followed by 2 hexadecimal characters // // Inputs: // <argument> pattern // String to check // // <argument> index // Offset in <pattern> at which to check substring for hex encoding // // Assumes: // 0 <= <index> < <pattern>.Length // // Returns: // true if <pattern>[<index>] is hex encoded, else false // // Throws: // Nothing public static bool IsHexEncoding(string pattern, int index) { if ((pattern.Length - index) < 3) { return(false); } if ((pattern[index] == '%') && UriShim.EscapedAscii(pattern[index + 1], pattern[index + 2]) != c_DummyChar) { return(true); } return(false); }
// Transforms a character into its hexadecimal representation. public static string HexEscape(char character) { if (character > '\xff') { throw new ArgumentOutOfRangeException(nameof(character)); } char[] chars = new char[3]; int pos = 0; UriShim.EscapeAsciiChar(character, chars, ref pos); return(new string(chars)); }
// HexUnescape // // Converts a substring of the form "%XX" to the single character represented // by the hexadecimal value XX. If the substring s[Index] does not conform to // the hex encoding format then the character at s[Index] is returned // // Inputs: // <argument> pattern // String from which to read the hexadecimal encoded substring // // <argument> index // Offset within <pattern> from which to start reading the hexadecimal // encoded substring // // Outputs: // <argument> index // Incremented to the next character position within the string. This // may be EOS if this was the last character/encoding within <pattern> // // Returns: // Either the converted character if <pattern>[<index>] was hex encoded, or // the character at <pattern>[<index>] // // Throws: // ArgumentOutOfRangeException 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 = UriShim.EscapedAscii(pattern[index + 1], pattern[index + 2]); if (ret != c_DummyChar) { index += 3; return(ret); } } return(pattern[index++]); }