示例#1
0
        /// <summary>
        /// Creates a string segment from a range of a string.
        /// </summary>
        public static StringSegment Subsegment(this string value, int index, int count)
        {
            Contract.RequiresNotNull(value);
            Contract.Requires(Range.IsValid(index, count, value.Length));

            return(new StringSegment(value, index, count));
        }
示例#2
0
        /// <summary>
        /// Creates a string segment from a range of a string.
        /// </summary>
        public static StringSegment Subsegment(this string value, int index, int count)
        {
            Contract.Requires(value != null);
            Contract.Requires(Range.IsValid(index, count, value.Length));
            Contract.Ensures(Contract.Result <StringSegment>().Length == count);

            return(new StringSegment(value, index, count));
        }
示例#3
0
        /// <summary>
        /// Creates a segment from a range of a string.
        /// </summary>
        public StringSegment(string value, int index, int length)
        {
            Contract.Requires(value != null);
            Contract.Requires(Range.IsValid(index, length, value.Length));

            m_value = value;
            m_index = index;
            Length  = length;
        }
示例#4
0
        /// <summary>
        /// Creates a segment from a range of a StringBuilder.
        /// </summary>
        public StringBuilderSegment(StringBuilder value, int index, int length)
        {
            Contract.RequiresNotNull(value);
            Contract.Requires(Range.IsValid(index, length, value.Length));

            m_value = value;
            m_index = index;
            Length  = length;
        }
示例#5
0
        /// <summary>
        /// Creates a segment from a range of an array.
        /// </summary>
        public CharArraySegment(char[] value, int index, int length)
        {
            Contract.Requires(value != null);
            Contract.Requires(Range.IsValid(index, length, value.Length));

            m_value  = value;
            m_index  = index;
            m_length = length;
        }
示例#6
0
        /// <summary>
        /// Copy the content of this segment to a byte buffer, assuming the segment only contains ASCII characters.
        /// </summary>
        public void CopyAs8Bit(byte[] buffer, int index)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(Range.IsValid(index, Length, buffer.Length));

            int end = m_index + Length;

            for (int i = m_index; i < end; i++)
            {
                char ch = m_value[i];
                buffer[index++] = unchecked ((byte)ch);
            }
        }
示例#7
0
        /// <summary>
        /// Copy the content of this segment to a byte buffer, assuming the segment only contains ASCII characters.
        /// </summary>
        public void CopyAs16Bit(byte[] buffer, int index)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(Range.IsValid(index, Length, buffer.Length));

            int end = m_index + Length;

            for (int i = m_index; i < end; i++)
            {
                char ch = m_value[i];
                buffer[index++] = (byte)((ch >> 8) & 0xff);
                buffer[index++] = (byte)((ch >> 0) & 0xff);
            }
        }
示例#8
0
        /// <summary>
        /// Compares this segment to a 16-bit characters drawn from a byte array starting at some index
        /// </summary>
        public bool Equals16Bit(byte[] buffer, int index)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(Range.IsValid(index, Length, buffer.Length));

            int end = m_index + Length;

            for (int i = m_index; i < end; i++)
            {
                var storedCh = (char)((buffer[index++] << 8) | buffer[index++]);
                if (storedCh != m_value[i])
                {
                    return(false);
                }
            }

            return(true);
        }
示例#9
0
        /// <summary>
        /// Compares this segment to a 8-bit character array starting at some index
        /// </summary>
        public bool Equals8Bit(byte[] buffer, int index)
        {
            Contract.RequiresNotNull(buffer);
            Contract.Requires(Range.IsValid(index, Length, buffer.Length));

            int end = m_index + m_length;

            for (int i = m_index; i < end; i++)
            {
                var storedCh = (char)buffer[index++];
                if (storedCh != m_value[i])
                {
                    return(false);
                }
            }

            return(true);
        }
示例#10
0
        /// <summary>
        /// Returns a sub segment of an existing segment.
        /// </summary>
        public StringSegment Subsegment(int index, int length)
        {
            Contract.Requires(Range.IsValid(index, length, Length));

            return(new StringSegment(m_value, m_index + index, length));
        }