public void RemoveAt(Cindex cindex, int length)
 {
     while (length > 0)
     {
         length = RemoveAtPartial(cindex, length);
         UpdateChunksToCindex(cindex + length);
     }
 }
        public void AddAt(string text, Cindex cindex)
        {
            int           chunkIndex = GetChunkIndexByCindex(cindex);
            DocumentChunk chunk      = chunks[chunkIndex];

            chunk.InsertText(text, cindex);
            updatedToChunkIndex = Math.Min(updatedToChunkIndex, chunkIndex);
            CheckChunkLength(chunkIndex, chunk);
        }
        private string SubStringWithConcat(int startChunkIndex, int endChunkIndex, Cindex from, Cindex to)
        {
            string subString = chunks[startChunkIndex].SubStringFromCharIndex(from);

            for (int i = startChunkIndex + 1; i < endChunkIndex; i++)
            {
                subString += chunks[i].Text;
            }
            subString += chunks[endChunkIndex].SubStringToCharIndex(to);
            return(subString);
        }
        private int RemoveAtPartial(Cindex cindex, int length)
        {
            int           chunkIndex      = GetChunkIndexByCindex(cindex);
            DocumentChunk chunk           = chunks[chunkIndex];
            int           availableLength = Math.Min(length, chunk.End + 1 - cindex);

            if (availableLength == 0)
            {
                return(0);
            }
            chunk.RemoveText(cindex, availableLength);
            updatedToChunkIndex = chunkIndex;
            CheckChunkLength(chunkIndex, chunk);
            return(length - availableLength);
        }
 private int GetChunkIndexByCindex(Cindex cindex)
 {
     UpdateChunksToCindex(cindex);
     for (int chunkIndex = 0; chunkIndex < chunks.Count; chunkIndex++)
     {
         if (chunks[chunkIndex].Contains(cindex))
         {
             return(chunkIndex);
         }
     }
     if (cindex == Length)
     {
         return(chunks.Count - 1);
     }
     throw new Exception(String.Format("Cindex {0} not found in DocumentChunkCollection of length {1}.", cindex, Length));
 }
/*		private void Display()
 *              {
 *                      Console.WriteLine("==========================");
 *                      foreach(DocumentChunk chunk in chunks)
 *                      {
 *                              Console.WriteLine(chunk);
 *                      }
 *              } */

        public string SubString(Cindex from, Cindex to)
        {
            if (to < from)
            {
                throw new Exception(String.Format("Document 'to' ({0}) is less than 'from' ({1}).", to, from));
            }
            int startChunkIndex = GetChunkIndexByCindex(from);
            int endChunkIndex   = GetChunkIndexByCindex(to);

            if (startChunkIndex == endChunkIndex)
            {
                return(chunks[startChunkIndex].SubStringByCharIndex(from, to));
            }
            if (endChunkIndex - startChunkIndex < 3)
            {
                return(SubStringWithConcat(startChunkIndex, endChunkIndex, from, to));
            }
            else
            {
                return(SubStringWithStringBuilder(startChunkIndex, endChunkIndex, from, to));
            }
        }
        private string SubStringWithStringBuilder(int startChunkIndex, int endChunkIndex, Cindex from, Cindex to)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(chunks[startChunkIndex].SubStringFromCharIndex(from));
            for (int i = startChunkIndex + 1; i < endChunkIndex; i++)
            {
                stringBuilder.Append(chunks[i].Text);
            }
            stringBuilder.Append(chunks[endChunkIndex].SubStringToCharIndex(to));
            return(stringBuilder.ToString());
        }