Exemple #1
0
        /// <summary>
        /// Creates a new instance of the <see cref="ReferenceDictionary{TKey, TValue}"/> class.
        /// </summary>
        /// <param name="initialCapacity">The initial capacity of the dictionary.</param>
        public ReferenceDictionary(int initialCapacity = DEFAULT_INITIAL_CAPACITY)
        {
            if (initialCapacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(initialCapacity));
            }

            m_slots  = new Slot[DataStructureUtil.nextPrime(initialCapacity)];
            m_chains = new int[m_slots.Length];
            m_chains.AsSpan().Fill(-1);
        }
Exemple #2
0
        /// <summary>
        /// Adds an element to the array.
        /// </summary>
        /// <param name="item">The element to add.</param>
        public void add(T item)
        {
            if (m_array == null)
            {
                _setCapacityInternal(4);
            }
            else if (m_array.Length == m_length)
            {
                int newCapacity = DataStructureUtil.getNextArraySize(m_length, m_length + 1);
                _setCapacityInternal(newCapacity);
            }

            m_array ![m_length++] = item;
Exemple #3
0
        /// <summary>
        /// Encodes a string for use as a URI. Any character other than a letter, number or a
        /// character in the whitelist will be percent-encoded as UTF-8.
        /// </summary>
        ///
        /// <param name="uri">The string to encode.</param>
        /// <param name="noEscapeChars">A string containing characters that are not letters or digits, that
        /// should not be escaped. If this is the empty string, all characters except letters and digits will be
        /// escaped. Only characters in the ASCII range (0-127) are checked against this string;
        /// characters outside this range are always escaped.</param>
        /// <param name="failOnInvalidSurrogate">If this is true, encoding fails if an invalid surrogate
        /// character is detected. Otherwise, invalid surrogates are replaced by '?' in the encoded
        /// string.</param>
        /// <param name="encodedURI">The encoded URI string.</param>
        ///
        /// <returns>True if the URI was encoded successfully, false if it is invalid.</returns>
        public static bool tryEncode(
            string uri, string noEscapeChars, bool failOnInvalidSurrogate, [NotNullWhen(true)] out string?encodedURI)
        {
            if (uri == null)
            {
                encodedURI = null;
                return(false);
            }

            char[] buffer = new char[uri.Length];
            int    bufPos = 0, bufLen = 0;

            bool error = false;

            for (int i = 0; i < uri.Length; i++)
            {
                char ch = uri[i];

                bool noEscape =
                    ((uint)(ch - '0') <= 9) ||
                    ((uint)(ch - 'A') <= 25) ||
                    ((uint)(ch - 'a') <= 25) ||
                    (ch <= 0x7F && noEscapeChars.Contains(ch));

                if (bufPos == bufLen)
                {
                    DataStructureUtil.expandArray(ref buffer);
                    bufLen = buffer.Length;
                }

                if (noEscape)
                {
                    // No percent encoding
                    buffer[bufPos++] = ch;
                    continue;
                }

                uint bytes            = 0;
                bool invalidSurrogate = false;

                if ((uint)(ch - 0xD800) < 0x400)
                {
                    // Check surrogate pairs
                    if (i == uri.Length - 1)
                    {
                        invalidSurrogate = true;
                    }
                    else
                    {
                        char trail = uri[i + 1];
                        if ((uint)(trail - 0xDC00) < 0x400)
                        {
                            bytes = _getUTF8Bytes(ch, trail);
                            i++;
                        }
                        else
                        {
                            invalidSurrogate = true;
                        }
                    }
                }
                else if ((uint)(ch - 0xDC00) < 0x400)
                {
                    invalidSurrogate = true;
                }
                else
                {
                    bytes = _getUTF8Bytes(ch);
                }

                if (invalidSurrogate)
                {
                    if (failOnInvalidSurrogate)
                    {
                        error = true;
                        break;
                    }
                    buffer[bufPos++] = '?';
                    continue;
                }

                do
                {
                    // Write the percent encoding for each byte
                    if (bufLen - bufPos < 3)
                    {
                        DataStructureUtil.expandArray(ref buffer, 3);
                        bufLen = buffer.Length;
                    }
                    buffer[bufPos] = '%';
                    byteToHex((byte)bytes, buffer.AsSpan(bufPos + 1));
                    bufPos += 3;
                    bytes >>= 8;
                } while (bytes != 0);
            }

            if (error)
            {
                encodedURI = null;
                return(false);
            }

            encodedURI = new string(buffer, 0, bufPos);
            return(true);
        }