TunnelVisionLabs.Net.UriTemplate/UriTemplatePart.cs
/// <summary>
/// Decodes text of a URI.
/// </summary>
/// <remarks>
/// <para>The URI is assumed to be formed by first using the UTF-8 encoding to obtain
/// a sequence of bytes, and then percent-encoding octets which are not allowed
/// in the URI syntax. This method decodes text from a URI which was encoded by
/// this process.</para>
/// </remarks>
/// <param name="text">The URI text.</param>
/// <returns>The decoded URI text.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="text"/> contains a <c>%</c> character which is not part of an percent-encoded triplet.</exception>
/// <exception cref="ArgumentException">If the byte sequence after decoding the percent-encoded triplets is not a valid UTF-8 byte sequence.</exception>
Decodes text of a URI.
Decodes text of a URI .
protected static string DecodeCharacters(string text)
        {
            if (text == null)
                throw new ArgumentNullException("text");
            byte[] sourceData = Encoding.UTF8.GetBytes(text);
            byte[] data = new byte[sourceData.Length];
            int length = 0;
            int position = 0;
            int index = -1;
            for (index = Array.IndexOf(sourceData, (byte)'%', index + 1); index >= 0; index = Array.IndexOf(sourceData, (byte)'%', index + 1))
            {
                while (position < index)
                {
                    data[length] = sourceData[position];
                    length++;
                    position++;
                }
                if (index > sourceData.Length - 3)
                    throw new ArgumentException("text contains a % character which is not part of a percent-encoded triplet");
                string hex = ((char)sourceData[index + 1]).ToString() + (char)sourceData[index + 2];
                byte value;
                if (!byte.TryParse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
                    throw new ArgumentException("text contains a % character which is not part of a percent-encoded triplet");
                data[length] = value;
                length++;
                position = index + 3;
            }
            while (position < sourceData.Length)
            {
                data[length] = sourceData[position];
                length++;
                position++;
            }
            return Encoding.UTF8.GetString(data, 0, length);
        }
protected static string DecodeCharacters ( string text ) { if ( text == null ) throw new ArgumentNullException ( " " ) ; byte [ ] sourceData = Encoding . UTF8 . GetBytes ( text ) ; byte [ ] data = new byte [ sourceData . Length ] ; int length = 0 ; int position = 0 ; int index = - 1 ; for ( index = Array . IndexOf ( sourceData , ( byte ) ' ' , index + 1 ) ; index >= 0 ; index = Array . IndexOf ( sourceData , ( byte ) ' ' , index + 1 ) ) { while ( position < index ) { data [ length ] = sourceData [ position ] ; length ++ ; position ++ ; } if ( index > sourceData . Length - 3 ) throw new ArgumentException ( " " ) ; string hex = ( ( char ) sourceData [ index + 1 ] ) . ToString ( ) + ( char ) sourceData [ index + 2 ] ; byte value ; if ( ! byte . TryParse ( hex , NumberStyles . AllowHexSpecifier , CultureInfo . InvariantCulture , out value ) ) throw new ArgumentException ( " " ) ; data [ length ] = value ; length ++ ; position = index + 3 ; } while ( position < sourceData . Length ) { data [ length ] = sourceData [ position ] ; length ++ ; position ++ ; } return Encoding . UTF8 . GetString ( data , 0 , length ) ; }
Decodes text of a URI.
Decodes text of a URI .
// the current position in sourceData // the index of the current % character in sourceData
The decoded URI text.
The decoded URI text .
If  is .
If is .
If  contains a %
If contains a %
If the byte sequence after decoding the percent-encoded triplets is not a valid UTF-8 byte sequence.
If the byte sequence after decoding the percent - encoded triplets is not a valid UTF - 8 byte sequence .
text
string
The URI text.
The URI text .
remarks
The URI is assumed to be formed by first using the UTF-8 encoding to obtain
a sequence of bytes, and then percent-encoding octets which are not allowed
in the URI syntax. This method decodes text from a URI which was encoded by
this process.
The URI is assumed to be formed by first using the UTF - 8 encoding to obtain a sequence of bytes and then percent - encoding octets which are not allowed in the URI syntax . This method decodes text from a URI which was encoded by this process .