示例#1
0
 /// <summary>
 /// Creates a new instance of <see cref="PooledIntegerSet"/>.
 /// </summary>
 /// <param name="pool">A <see cref="DynamicArrayPool{Int32}"/> instance from which the memory
 /// for this set should be allocated.</param>
 /// <param name="capacity">The initial capacity to allocate for this set.</param>
 public PooledIntegerSet(DynamicArrayPool <int> pool, int capacity = 0)
 {
     m_pool  = pool;
     m_token = pool.allocate((capacity > 0) ? DataStructureUtil.nextPowerOf2(capacity - 1) : 4);
     m_pool.getSpan(m_token).Fill(EMPTY_SLOT);
     m_count = 0;
 }
示例#2
0
        public void nextPrimeTest(int num)
        {
            int nextPrime = DataStructureUtil.nextPrime(num);

            Assert.True(nextPrime > 1, $"Expected {nextPrime} to be > 1.");
            Assert.True(nextPrime >= num, $"Expected {nextPrime} to be >= {num}.");

            for (int i = 2; (long)i * i <= (long)nextPrime; i++)
            {
                Assert.True(nextPrime % i != 0, $"Expected {nextPrime} to be prime, but has has factor {i}.");
            }
        }
示例#3
0
        public void nextPowerOf2Test(int num)
        {
            int nextPowerOf2 = DataStructureUtil.nextPowerOf2(num);

            Assert.NotEqual(0, nextPowerOf2);
            Assert.True((nextPowerOf2 & (nextPowerOf2 - 1)) == 0, $"Expected {nextPowerOf2} to be a power of 2.");
            Assert.True(nextPowerOf2 >= num, $"Expected {nextPowerOf2} to be >= {num}");

            uint upperBound = (uint)Math.Max(num, 1) * 2;

            Assert.True((uint)nextPowerOf2 < upperBound, $"Expected {nextPowerOf2} to be < {upperBound}.");
        }
示例#4
0
        /// <summary>
        /// Setup cross sectional DSD.
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="crossDataSet">
        /// The list of components attached to cross-sectional data set.
        /// </param>
        /// <param name="crossGroup">
        /// The list of components attached to cross-sectional group.
        /// </param>
        /// <param name="crossSection">
        /// The list of components attached to cross-sectional section.
        /// </param>
        /// <param name="crossObs">
        /// The list of components attached to cross-sectional observation level
        /// </param>
        /// <param name="crossSectionalMeasures">
        /// The cross sectional measures.
        /// </param>
        /// <param name="measureCodelistRepresentation">
        /// The SDMX v2.0 measure dimension codelist representation; otherwise null
        /// </param>
        /// <returns>
        /// The <see cref="IDataStructureMutableObject"/>.
        /// </returns>
        private static IDataStructureMutableObject SetupCrossSectionalDsd(
            IDataStructureMutableObject parent,
            ICollection <string> crossDataSet,
            ICollection <string> crossGroup,
            ICollection <string> crossSection,
            ICollection <string> crossObs,
            IEnumerable <ICrossSectionalMeasureMutableObject> crossSectionalMeasures,
            IStructureReference measureCodelistRepresentation)
        {
            if (
                parent.Dimensions.All(
                    o =>
                    o.FrequencyDimension || o.TimeDimension || o.MeasureDimension || crossDataSet.Contains(o.Id) || crossGroup.Contains(o.Id) || crossSection.Contains(o.Id) || crossObs.Contains(o.Id)))
            {
                if (parent.AttributeList == null ||
                    parent.AttributeList.Attributes.All(o => crossDataSet.Contains(o.Id) || crossGroup.Contains(o.Id) || crossSection.Contains(o.Id) || crossObs.Contains(o.Id)))
                {
                    ICrossSectionalDataStructureMutableObject crossDsd = _crossDsdBuilder.Build(parent);
                    crossDsd.CrossSectionalAttachDataSet.AddAll(crossDataSet);
                    crossDsd.CrossSectionalAttachSection.AddAll(crossSection);
                    crossDsd.CrossSectionalAttachGroup.AddAll(crossGroup);
                    crossDsd.CrossSectionalAttachObservation.AddAll(crossObs);
                    IDimensionMutableObject measure = crossDsd.Dimensions.FirstOrDefault(o => o.MeasureDimension);
                    if (measure != null)
                    {
                        foreach (ICrossSectionalMeasureMutableObject crossSectionalMeasureMutableObject in crossSectionalMeasures)
                        {
                            crossSectionalMeasureMutableObject.MeasureDimension = measure.Id;
                            crossDsd.CrossSectionalMeasures.Add(crossSectionalMeasureMutableObject);
                        }

                        if (measureCodelistRepresentation == null)
                        {
                            DataStructureUtil.ConvertMeasureRepresentation(crossDsd);
                        }
                    }

                    return(crossDsd);
                }
            }

            return(parent);
        }
示例#5
0
        /// <summary>
        /// Writes the character or surrogate pair for the given code point into the buffer.
        /// </summary>
        /// <param name="buffer">The buffer into which to write the character(s). This
        /// may be reallocated if there is not enough space.</param>
        /// <param name="position">The position in <paramref name="buffer"/> into which to
        /// write the character(s). The number of characters written will be added.</param>
        /// <param name="value">A Unicode code point value.</param>
        private static void _writeCodePoint(ref char[] buffer, ref int position, int value)
        {
            if (buffer.Length - position < 2)
            {
                DataStructureUtil.expandArray(ref buffer, 2);
            }

            if (value > 0xFFFF)
            {
                // Split into surrogate pairs.
                int offsetValue = value - 0x10000;
                buffer[position]     = (char)(0xD800 | (offsetValue >> 10));
                buffer[position + 1] = (char)(0xDC00 | (offsetValue & 0x3FF));
                position            += 2;
            }
            else
            {
                buffer[position] = (char)value;
                position++;
            }
        }
示例#6
0
        /// <summary>
        /// Reads an attribute value and returns it.
        /// </summary>
        private string _readAttributeValue()
        {
            char quote = m_str[m_pos];

            if (quote != '"' && quote != '\'')
            {
                throw _error(ErrorCode.XML_PARSER_ELEMENT_MALFORMED);
            }

            m_pos++;

            ReadOnlySpan <char> span        = m_str.AsSpan(m_pos);
            int  charsLeft                  = m_str.Length - m_pos;
            char newline                    = s_newLineChar;
            ReadOnlySpan <char> searchChars = stackalloc char[] { quote, '&', '<', newline };

            bool mayHaveEntities = false;

            while (true)
            {
                int index = span.IndexOfAny(searchChars);
                if (index == -1)
                {
                    throw _error(ErrorCode.XML_PARSER_UNTERMINATED_ATTR);
                }

                char charAtIndex = span[index];
                if (charAtIndex == quote)
                {
                    span = span.Slice(index);
                    break;
                }
                else if (charAtIndex == '&')
                {
                    span            = span.Slice(index);
                    mayHaveEntities = true;
                    break;
                }
                else if (charAtIndex == newline)
                {
                    m_curLine++;
                    span = span.Slice(index + 1);
                }
                else
                {
                    // Attributes cannot contain '<'.
                    throw _error(ErrorCode.XML_PARSER_ELEMENT_MALFORMED);
                }
            }

            int charsRead = charsLeft - span.Length;

            if (!mayHaveEntities)
            {
                int startPos = m_pos;
                m_pos += charsRead + 1;     // +1 for the closing quote
                return(m_str.Substring(startPos, charsRead));
            }

            char[] textBuffer = m_buffer;

            if (textBuffer.Length < charsRead)
            {
                DataStructureUtil.resizeArray(ref textBuffer, textBuffer.Length, charsRead);
            }

            m_str.CopyTo(m_pos, textBuffer, 0, charsRead);
            m_pos += charsRead;
            int textBufPos = charsRead;

            while (true)
            {
                char ch = m_str[m_pos];
                if (ch == '&')
                {
                    int entityCode = _readEntity();
                    _writeCodePoint(ref textBuffer, ref textBufPos, entityCode);
                }
                else if (ch == quote)
                {
                    break;
                }
                else if (ch == '<')
                {
                    throw _error(ErrorCode.XML_PARSER_ELEMENT_MALFORMED);
                }
                else if (ch == newline)
                {
                    m_curLine++;
                    m_pos++;
                }

                span = m_str.AsSpan(m_pos);
                int nextIndex = span.IndexOfAny(searchChars);

                if (nextIndex == -1)
                {
                    throw _error(ErrorCode.XML_PARSER_UNTERMINATED_ATTR);
                }

                if (textBuffer.Length - textBufPos < nextIndex)
                {
                    DataStructureUtil.resizeArray(ref textBuffer, textBufPos, textBufPos + nextIndex);
                }

                span.Slice(0, nextIndex).CopyTo(textBuffer.AsSpan(textBufPos));
                textBufPos += nextIndex;

                m_pos += nextIndex;
            }

            m_pos++;    // For closing quote
            m_buffer = textBuffer;

            return((textBufPos == 0) ? "" : new string(textBuffer, 0, textBufPos));
        }
示例#7
0
        /// <summary>
        /// Reads a text node and adds it as a child to the element currently being processed.
        /// </summary>
        private void _readText()
        {
            ReadOnlySpan <char> span        = m_str.AsSpan(m_pos);
            int  charsLeft                  = m_str.Length - m_pos;
            char newline                    = s_newLineChar;
            ReadOnlySpan <char> searchChars = stackalloc char[3] {
                '<', '&', newline
            };

            bool mayHaveEntities = false;

            while (!span.IsEmpty)
            {
                int  index       = span.IndexOfAny(searchChars);
                char charAtIndex = (index == -1) ? '\0' : span[index];

                if (charAtIndex == '<')
                {
                    span = span.Slice(index);
                    break;
                }
                else if (charAtIndex == '&')
                {
                    span            = span.Slice(index);
                    mayHaveEntities = true;
                    break;
                }
                else if (charAtIndex == newline)
                {
                    m_curLine++;
                    span = span.Slice(index + 1);
                }
                else
                {
                    span = default;
                }
            }

            int    charsRead = charsLeft - span.Length;
            string text;

            if (!mayHaveEntities)
            {
                text = ((m_parserFlags & FLAG_IGNORE_SPACE) != 0)
                    ? XMLHelper.stripWhitespace(m_str, m_pos, charsRead)
                    : m_str.Substring(m_pos, charsRead);

                if (text.Length != 0)
                {
                    m_nodeStack.add(ASXML.createTextNode(text));
                }

                m_pos += charsRead;
                return;
            }

            char[] textBuffer = m_buffer;
            if (charsRead > textBuffer.Length)
            {
                DataStructureUtil.resizeArray(ref textBuffer, textBuffer.Length, charsRead);
            }

            m_str.CopyTo(m_pos, textBuffer, 0, charsRead);
            int textBufPos = charsRead;

            m_pos += charsRead;

            while (true)
            {
                char ch = m_str[m_pos];
                if (ch == '&')
                {
                    int entityCode = _readEntity();
                    _writeCodePoint(ref textBuffer, ref textBufPos, entityCode);
                }
                else if (ch == '<')
                {
                    break;
                }
                else if (ch == newline)
                {
                    m_curLine++;
                    m_pos++;
                }

                span = m_str.AsSpan(m_pos);
                int nextIndex   = span.IndexOfAny(searchChars);
                int charsToCopy = (nextIndex == -1) ? span.Length : nextIndex;

                if (textBuffer.Length - textBufPos < charsToCopy)
                {
                    DataStructureUtil.resizeArray(ref textBuffer, textBufPos, textBufPos + charsToCopy);
                }

                span.Slice(0, charsToCopy).CopyTo(textBuffer.AsSpan(textBufPos));
                textBufPos += charsToCopy;
                m_pos      += charsToCopy;

                if (nextIndex == -1)
                {
                    break;
                }
            }

            m_buffer = textBuffer;

            if (textBufPos == 0)
            {
                return;
            }

            text = ((m_parserFlags & FLAG_IGNORE_SPACE) != 0)
                ? XMLHelper.stripWhitespace(textBuffer, 0, textBufPos)
                : new string(textBuffer, 0, textBufPos);

            if (text.Length != 0)
            {
                m_nodeStack.add(ASXML.createTextNode(text));
            }
        }
示例#8
0
        public static string escape(string str = "undefined")
        {
            if (str == null)
            {
                return("null");
            }

            int strlen = str.Length;

            char[] buffer = new char[strlen];
            int    bufPos = 0, bufLen = 0;

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

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

                if (noEncode)
                {
                    // No percent encoding
                    if (bufPos == bufLen)
                    {
                        DataStructureUtil.expandArray(ref buffer);
                        bufLen = buffer.Length;
                    }
                    buffer[bufPos++] = ch;
                    continue;
                }

                if (ch > 0xFF)
                {
                    if (bufLen - bufPos < 6)
                    {
                        DataStructureUtil.expandArray(ref buffer, 6);
                        bufLen = buffer.Length;
                    }

                    var bufferSpan = buffer.AsSpan(6);
                    bufferSpan[0] = '%';
                    bufferSpan[1] = 'u';
                    URIUtil.byteToHex((byte)(ch >> 8), bufferSpan.Slice(2));
                    URIUtil.byteToHex((byte)ch, bufferSpan.Slice(4));
                    bufPos += 6;
                }
                else
                {
                    if (bufLen - bufPos < 3)
                    {
                        DataStructureUtil.expandArray(ref buffer, 3);
                        bufLen = buffer.Length;
                    }
                    buffer[bufPos] = '%';
                    URIUtil.byteToHex((byte)ch, buffer.AsSpan(bufPos + 1));
                    bufPos += 3;
                }
            }

            return(new string(buffer, 0, bufPos));
        }